Skip to content

Commit

Permalink
refactor: JwtOIDCProvider, OauthOIDCHelper 코드 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
ki-met-hoon committed Mar 20, 2024
1 parent 0adebc6 commit 4461050
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Claims> getOIDCTokenJws(String token, String modulus, String exponent, String iss, String aud) {

public Jws<Claims> 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()
Expand All @@ -55,9 +51,8 @@ public Jws<Claims> 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(),
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -34,6 +34,7 @@ public OIDCDecodePayload getKakaoOIDCDecodePayload(String token) {
token,
kakaoProperties.getBaseUrl(),
kakaoProperties.getRestApiKey(),
oidcPublicKeysResponse);
oidcPublicKeysResponse
);
}
}

0 comments on commit 4461050

Please sign in to comment.