ID トークンへのクレームの追加

はじめに


本記事では、クレームを ID トークンに追加する方法について説明します。

クレームの追加方法


クレームの追加は /auth/authorization/issue API により行います。本記事では次のクレームを追加します。
項目
"name"
"Test User"
"email"
"testuser01@example.com"
"email_verified"
true


クレームの追加には claims パラメーターを利用します。リクエストは以下のようなかたちになります。(一部折り返しています)

curl -s -X POST https://api.authlete.com/api/auth/authorization/issue \
-u '<API Key>:<API Secret>' \
-H 'Content-Type: application/json' \
-d '{ "ticket": "<Ticket>", "subject": "testuser01",
 "claims": "{\"name\": \"Test User\",
  \"email\": \"testuser01@example.com\",
  \"email_verified\": true}"
 }'

これにより Authlete は、追加されたクレームを含む ID トークンを発行します。以下はその ID トークンのペイロードの内容です。

{
  "name": "Test User",
  "email": "testuser01@example.com",
  "email_verified": true,
  "iss": "https://as.example.com",
  "sub": "testuser01",
  "aud": [
    "12898884596863"
  ],
  "exp": 1559137301,
  "iat": 1559050901,
  "nonce": "n-0S6_WzA2Mj"
}


"sub" クレームに独自の値を指定する方法


既定では、Authlete は "sub" クレームに "subject" パラメーターの値を用います。同クレームに独自の値を指定したい場合には、API リクエスト時に、"claims" パラメーターではなく専用の "sub" パラメーターを利用してください。以下に例を示します。

curl -s -X POST https://api.authlete.com/api/auth/authorization/issue \
-u '<API Key>:<API Secret>' \
-H 'Content-Type: application/json' \
-d '{ "ticket": "<Ticket>", "subject": "testuser01",
 "claims": "{\"name\": \"Test User\",
  \"email\": \"testuser01@example.com\",
  \"email_verified\": true}",
  "sub": "1234567890"
 }'

生成される ID トークンには、以下のように、独自の "sub" クレームの値が含まれるようになります。

{
  "name": "Test User",
  "email": "testuser01@example.com",
  "email_verified": true,
  "iss": "https://as.example.com",
  "sub": "1234567890",
  "aud": [
    "126863743267133" ],
  "exp": 1688804473,
  "iat": 1688718073,
  "nonce": "n-0S6_WzA2Mj"
}

How did we do with this article?