필요성
"Yogit" 프로젝트를 진행하면서 Apple 로그인을 구현기로 했다.
iOS 기반의 앱 프로젝트로, 다른 서비스도 마찬가지이긴 하지만 사용자끼리의 모임을 갖는 서비스 특성상 서버내에서 투명한 사용자 인증과정의 필요성이 더 요구되었다.

https://developer.apple.com/documentation/authenticationservices/asauthorizationappleidcredential
💡 Identity Token
A JSON Web Token (JWT) that securely communicates information about the user to the app.
- identity Token을 통해 API Server측에서 중요한 유저 정보를 얻을 수 있다.
(apple 고유 계정 id, email 등)
- 생성 후 만료시간 : 10분

- Identity Token 검증 방법
Identity Token 서명 검증용 public key를 요청, 응답 받은 뒤
서명 검증을 완료해야 고유 account id를 획득할 수 있다.
https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/verifying_a_user

To verify the identity token, your app server must:
- Verify the JWS E256 signature using the server’s public key
- Verify the nonce for the authentication
(nonce : 암호화 통신에서 한 번만 사용할 수 있는 임의의 숫자)
- Verify that the iss field contains https://appleid.apple.com
- Verify that the aud field is the developer’s client_id
- Verify that the time is earlier than the exp value of the token
- public key 발급 방법
key 각각의 kid, alg 중 Identity Token header에 포함된 kid, alg와 일치하는 key를 사용하면된다.
https://developer.apple.com/documentation/sign_in_with_apple/fetch_apple_s_public_key_for_verifying_token_signature
GET https://appleid.apple.com/auth/keys

- Identity Token 디코딩
https://jwt.io/
암호화된 알고리즘은 'RS256' 즉, SHA-256을 사용하는 RSA(비대칭키 암호화방식)이기 때문에 n(modulus), e(exponent)로 공개키를 구성한다.
즉, n, e 값을 통해 public key를 생성한뒤 public key로 Identity Token의 서명(signature)을 검증하면 된다.

- 구현
💡 Authorization Code
A token that the app uses to interact with the server.
- identityToken은 생성 후 10분 뒤 만료되기 때문에 identityToken가지고는 APP측에서 session을 유지하기 어렵다.
- 그래서 authorizationCode를 통해 APP측에서 session을 유지시킬 수 있는 token을 Apple ID Server로 부터 발급받아야 한다.
- 생성 후 만료시간 : 5분
Reference
https://hwannny.tistory.com/71