Skip to content

Instantly share code, notes, and snippets.

@awa2
Created November 9, 2018 06:32
Show Gist options
  • Save awa2/ada1b2d0d14179da0b359b877e4c136f to your computer and use it in GitHub Desktop.
Save awa2/ada1b2d0d14179da0b359b877e4c136f to your computer and use it in GitHub Desktop.

JWTについて

JWTの基本フォーマット

{base64 Encoded Header}.{base6 Encoded Claims}.{Signature}

Header

{
  "alg": "RS256",
  "typ": "JWT"
}

これに基づき、JSON Web Signatures(JWS)(RFC 7515)により署名される。 ※なお、このalgパラメータで指定するJSON Web Algorithms(JWA)(RFC 7518)は、使える暗号サブセットが決まっていることもあり、脆弱性となりやすい。 また、alg=noneを指定可能であり、その場合、非常に脆弱となる。

Claims

Google APIで利用する場合は、次の通り。

{
  "iss": "xxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com",
  "scope": "https://www.googleapis.com/auth/drive",
  "aud": "https://www.googleapis.com/oauth2/v3/token",
  "exp": 1433469193,
  "iat": 1433465593
}

iss(Issuer),aud(Audience),exp(Expiration Time),iat(Issued At)が本来のパラメータである。
他にもsub(Subject),jti(JWT ID),nbf(Not Before)などが登録できる。

JWTでアクセストークンを取得する

{base64 Encoded Header}.{base6 Encoded Claims}.{Signature}

Headerのalgで指定されたアルゴリズムで署名する。秘密鍵はGoogle Cloud Platformから払い出されたものを利用する。

{
  "assertion": YOUR_JWT,
  "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer"
}

これをPOSTすると、アクセストークンを取得することができる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment