Sign in with Apple의 ID 토큰을 가져옵니다.

동작을 확인하기 위해 앱을 구축하지 않고 토큰의 끝에서 Sign in with Apple ID 토큰을 손에 쥐었을 때의 노트다.

차리다


App ID 만들기


siwa1
Certificates, Identifiers & Profiles에서 Identifiers+를 눌러 App ID를 만듭니다.
siwa2
App IDs 확인
siwa3
Sign in with Apple을 확인한 후 저장합니다.

서비스 ID 만들기


siwa4
Certificates, Identifiers & Profiles에서 Identifiers+를 눌러 서비스 ID를 만듭니다.
siwa5
Sign in with Apple을 확인한 후 Configure를 선택합니다.Identifier의 값을 기록합니다.
siwa5
Domains 및 Subdomains에서 지정example.com하고 Returns URLs에서 지정https://example.com합니다.
example.com에 로그인한 계정의 개인 정보는 날아갈 수 있으니 건너뛰기 싫으면 날아갈 수 있는 URL을 지정해 주세요.

Keys 만들기


siwa6
Certificates, Identifiers & Profiles에서 Keys+를 눌러 Keys를 생성합니다.
siwa7
Sign in with Apple을 확인한 후 Configure를 선택합니다.
siwa8
Primary App ID에서 방금 만든 App ID를 선택합니다.
siwa9
버튼을 다운로드합니다.표시된 키 ID를 기록합니다.
다운로드한 키는 확장자xxx.p8, 재명명xxx.pem이다.

로그인 페이지 준비


로컬에서 다음 HTML 파일을 준비합니다.
<html>
    <head>
        <meta name="appleid-signin-client-id" content="xxxxx">
        <meta name="appleid-signin-scope" content="name email">
        <meta name="appleid-signin-redirect-uri" content="https://example.com">
        <meta name="appleid-signin-state" content="aaa">
        <meta name="appleid-signin-nonce" content="bbb">
        <meta name="appleid-signin-use-popup" content="false">
    </head>
    <body>
        <div id="appleid-signin" data-color="black" data-border="true" data-type="sign in"></div>
        <script type="text/javascript" src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
    </body>
</html>
Applleid-signin-client id에서 서비스 ID의 Identifier 값을 지정하십시오.
이번엔 아이디 토큰만 따면 돼, state와 nonce만 따면 돼.
서비스 ID의 Domains 및 Subdomains에 https://example.com 이외의 값을 지정하면 URL을 덮어씁니다.

jwt-cli 설치


토큰 엔드포인트는 JWT의 비밀이 필요하므로 설치jwt-cli합니다.

시험해 보다


Google Chrome으로 만든 HTML을 엽니다.POST에서 만든 정보를 보고 싶어서 개발자 도구의 네트워크 탭을 미리 엽니다.
siwa10
Sign in with Apple 버튼을 선택하면 Apple ID의 로그인 화면으로 이동합니다.
siwa11
로그인
siwa12
Continuehttps://example.com를 선택했을 때state,code,id_token POST를 진행합니다.
POST 이후의 codeid_token 값을 기록합니다.
siwa13

포스터의 아이디 토큰을 보도록 하겠습니다.


$ jwt decode eyJxxxx.xxxxx.xxxxx

Token header
------------
{
  "alg": "RS256",
  "kid": "xxxxx"
}

Token claims
------------
{
  "aud": "xxxxx",
  "auth_time": 1634526055,
  "c_hash": "xxxxx",
  "email": "[email protected]",
  "email_verified": "true",
  "exp": 1634612455,
  "iat": 1634526055,
  "iss": "https://appleid.apple.com",
  "nonce": "bbb",
  "nonce_supported": true,
  "sub": "xxx"
}

라이센스 코드 및 ID 토큰 교환


비밀 생성


Creating the Client SecretGenerate and Validate Tokens의 단계에 따라 기밀을 생성합니다.
jwt-cli를 사용하여 제작할 때 다음과 같다.
jwt encode -A ES256 -k xxxxx -i xxxxx -a https://appleid.apple.com -s xxxxx -e --secret @/xxxx.pem
옵션에 다음을 지정하십시오.
  • -k: Keys を作成에 표시된 Key ID
  • -i: https://developer.apple.com/account/#!/membership/에 표시된 팀 ID
  • -s: Service ID を作成에 표시된 Identifier
  • --secret: Keys を作成 다운로드한 키 경로(jwt-cli는 확장자로 키의 형식을 판단하기 때문에 확장자는 p8이 아닌pem이어야 함)
  • 비밀이 생길 수 있으면curl로 영패 단점을 공격합니다.
    curl -X POST "https://appleid.apple.com/auth/token" \
      -H 'content-type: application/x-www-form-urlencoded' \
      -d 'client_id=xxx' \
      -d 'client_secret=eyJxxxx.xxxxx.xxxxx' \
      -d 'code=xxx' \
      -d 'grant_type=authorization_code' \
      -d 'redirect_uri=https://example.com'
    
    옵션에 다음을 지정하십시오.
  • -client_id:Service ID を作成에 표시된 Identifier(비밀 제작 시 -s와 동일
  • client_시크릿:シークレットの生成에서 제작된 비밀
  • 코드: https://example.com의 POST 값code
  • redirect_uri: 로그인 페이지의 HTML에 지정된 값appleid-signin-redirect-uri
  • 다음 응답이 반환되었으므로 ID 토큰을 디코딩해 보십시오.
    {"access_token":"xxxxx","token_type":"Bearer","expires_in":3600,"refresh_token":"xxxxx","id_token":"eyJxxxx.xxxx.xxxx"}
    
    $ jwt decode eyJxxxx.xxxxx.xxxxx
    
    Token header
    ------------
    {
      "alg": "RS256",
      "kid": "xxxxx"
    }
    
    Token claims
    ------------
    {
      "aud": "xxxxx",
      "auth_time": 1634526055,
      "c_hash": "xxxxx",
      "email": "[email protected]",
      "email_verified": "true",
      "exp": 1634612455,
      "iat": 1634526055,
      "iss": "https://appleid.apple.com",
      "nonce": "bbb",
      "nonce_supported": true,
      "sub": "xxx"
    }
    
    디코딩되었습니다.

    참고 자료

  • Apple Developer Documentation | Configuring Your Webpage for Sign in with Apple
  • Apple Developer Documentation | Generate and Validate Tokens
  • 좋은 웹페이지 즐겨찾기