2626import lombok .extern .slf4j .Slf4j ;
2727
2828import org .springframework .beans .factory .annotation .Value ;
29+ import org .springframework .security .oauth2 .jwt .Jwt ;
2930import org .springframework .security .oauth2 .jwt .JwtDecoder ;
31+ import org .springframework .security .oauth2 .jwt .JwtException ;
3032import org .springframework .stereotype .Service ;
3133import org .springframework .transaction .annotation .Transactional ;
3234
@@ -47,8 +49,8 @@ public class AuthService {
4749 private final JwtDecoder appleJwtDecoder ;
4850 private final NicknameGenerator nicknameGenerator ;
4951
50- // @Value("${apple.client-id}")
51- // private String appleClientId;
52+ @ Value ("${apple.client-id}" )
53+ private String appleClientId ;
5254
5355 @ Value ("${google.android-id}" )
5456 private String googleAndroidId ;
@@ -224,10 +226,35 @@ private String getEmailFromIdToken(String idToken, PlatformType platformType, Lo
224226
225227 } catch (GeneralSecurityException | IOException e ) {
226228 throw BusinessException .of (AuthErrorCode .GOOGLE_LOGIN_ERROR );
229+ } catch (Exception e ) {
230+ throw BusinessException .of (AuthErrorCode .INVALID_TOKEN );
227231 }
228232 }
229233 case APPLE -> {
230- //TODO Apple 구현 예정
234+ String clientId = appleClientId ;
235+
236+ try {
237+ Jwt jwt = appleJwtDecoder .decode (idToken );
238+
239+ if (!"https://appleid.apple.com" .equals (jwt .getIssuer ().toString ())) {
240+ throw BusinessException .of (AuthErrorCode .INVALID_TOKEN );
241+ }
242+
243+ String aud = jwt .getAudience ().get (0 );
244+ if (!aud .equals (clientId )) {
245+ throw BusinessException .of (AuthErrorCode .INVALID_TOKEN );
246+ }
247+
248+ Object emailObject = jwt .getClaims ().get ("email" );
249+ if (emailObject == null ) {
250+ throw BusinessException .of (AuthErrorCode .NOT_EXISTS_EMAIL );
251+ }
252+ return emailObject .toString ();
253+ } catch (JwtException e ) {
254+ throw BusinessException .of (AuthErrorCode .INVALID_TOKEN );
255+ } catch (Exception e ) {
256+ throw BusinessException .of (AuthErrorCode .APPLE_LOGIN_ERROR );
257+ }
231258 }
232259 }
233260 return null ;
0 commit comments