From 44610509ff42cfdba34663f6e23787bcacfab5b7 Mon Sep 17 00:00:00 2001 From: KimSehoon Date: Wed, 20 Mar 2024 22:18:51 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20JwtOIDCProvider,=20OauthOIDCHelper?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/token/JwtOIDCProvider.java | 32 +++++++++++-------- .../support/token/OauthOIDCHelper.java | 5 +-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/example/pnuunivmiryangcampus/support/token/JwtOIDCProvider.java b/src/main/java/com/example/pnuunivmiryangcampus/support/token/JwtOIDCProvider.java index 730e5d1..018ee24 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/support/token/JwtOIDCProvider.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/support/token/JwtOIDCProvider.java @@ -1,6 +1,7 @@ package com.example.pnuunivmiryangcampus.support.token; import com.example.pnuunivmiryangcampus.auth.OIDCDecodePayload; +import com.example.pnuunivmiryangcampus.auth.OIDCPublicKeyDto; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jws; @@ -22,27 +23,22 @@ public class JwtOIDCProvider { public String getKidFromTokenHeader(String token) { - String KID = "kid"; - String[] splitToken = token.split("\\."); - String header = splitToken[0]; - - byte[] decodeJson = Base64.getDecoder().decode(header); - String decodeHeader = new String(decodeJson); + String encodedHeader = getEncodedHeader(token); + String decodedHeader = getDecodedHeader(encodedHeader); try { - JSONObject jsonObject = new JSONObject(decodeHeader); + JSONObject jsonObject = new JSONObject(decodedHeader); return jsonObject.get(KID).toString(); } catch (JSONException e) { return e.toString(); } } - public Jws getOIDCTokenJws(String token, String modulus, String exponent, String iss, String aud) { - + public Jws getOIDCTokenJws(String token, OIDCPublicKeyDto oidcPublicKeyDto, String iss, String aud) { try { return Jwts.parser() - .verifyWith(getRSAPublicKey(modulus, exponent)) + .verifyWith(getRSAPublicKey(oidcPublicKeyDto.n(), oidcPublicKeyDto.e())) .requireAudience(aud) .requireIssuer(iss) .build() @@ -55,9 +51,8 @@ public Jws getOIDCTokenJws(String token, String modulus, String exponent } } - public OIDCDecodePayload getOIDCTokenBody(String token, String modulus, String exponent, String iss, String aud) { - - Claims payload = getOIDCTokenJws(token, modulus, exponent, iss, aud).getPayload(); + public OIDCDecodePayload getOIDCTokenBody(String token, OIDCPublicKeyDto oidcPublicKeyDto, String iss, String aud) { + Claims payload = getOIDCTokenJws(token, oidcPublicKeyDto, iss, aud).getPayload(); return new OIDCDecodePayload( payload.getIssuer(), @@ -66,8 +61,17 @@ public OIDCDecodePayload getOIDCTokenBody(String token, String modulus, String e payload.get("email", String.class)); } - private PublicKey getRSAPublicKey(String modulus, String exponent) throws NoSuchAlgorithmException, InvalidKeySpecException { + private static String getDecodedHeader(String encodedHeader) { + byte[] decodedHeaderBytes = Base64.getDecoder().decode(encodedHeader); + return new String(decodedHeaderBytes); + } + private static String getEncodedHeader(String token) { + String[] splitToken = token.split("\\."); + return splitToken[0]; + } + + private PublicKey getRSAPublicKey(String modulus, String exponent) throws NoSuchAlgorithmException, InvalidKeySpecException { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] decodeN = Base64.getUrlDecoder().decode(modulus); byte[] decodeE = Base64.getUrlDecoder().decode(exponent); diff --git a/src/main/java/com/example/pnuunivmiryangcampus/support/token/OauthOIDCHelper.java b/src/main/java/com/example/pnuunivmiryangcampus/support/token/OauthOIDCHelper.java index aaa1238..9c44241 100644 --- a/src/main/java/com/example/pnuunivmiryangcampus/support/token/OauthOIDCHelper.java +++ b/src/main/java/com/example/pnuunivmiryangcampus/support/token/OauthOIDCHelper.java @@ -24,7 +24,7 @@ private OIDCDecodePayload getPayloadFromIdToken(String token, String iss, String .findFirst() .orElseThrow(); - return jwtOIDCProvider.getOIDCTokenBody(token, oidcPublicKeyDto.n(), oidcPublicKeyDto.e(), iss, aud); + return jwtOIDCProvider.getOIDCTokenBody(token, oidcPublicKeyDto, iss, aud); } public OIDCDecodePayload getKakaoOIDCDecodePayload(String token) { @@ -34,6 +34,7 @@ public OIDCDecodePayload getKakaoOIDCDecodePayload(String token) { token, kakaoProperties.getBaseUrl(), kakaoProperties.getRestApiKey(), - oidcPublicKeysResponse); + oidcPublicKeysResponse + ); } }