はじめに
前回の記事 で、 auth0-cli
を使うと、 Client Credentials Flow
での Access Token 取得は普通にできることがわかりました。
こちらの記事 では、SPA からの Authorization Code Flow
での認証もできていたと思います。
では単発 CLI から Authorization Code Flow
の Access Token 取りたかったらどうすれば良いのだろう?となったので、その結果を書いてみます。
(イメージは、Azure でいう az
みたいなイメージですね。絶対できるはずだと思って調べてました)
auth0-cli における CLI Login Testing [Generic]
の存在
前回の記事 で、 見落としていたのですかね...
実は auth0 test token
に以下の選択肢があります。 (一番下の選択肢を見逃してたし、貼り逃してた)
auth0 test login
の後に、 Create a new client to use for testing the login
をすると作られるんですね、理解。
% auth0 test token
Client ID: [Use arrows to move, type to filter, ? for more help]
> Create a new client to use for testing the login
Default App [Regular Web Application] ([<- のCLIENT ID])
Auth0 Practice API (Test Application) [Machine to Machine] ([<- のCLIENT ID])
Auth0 Practice SPA [Single Page Web Application] ([<- のCLIENT ID])
CLI Login Testing [Generic] ([<- のCLIENT ID]) <-- これ
この CLI Login Testing [Generic]
を選択すると次のようになります。
% auth0 test token
Client ID: CLI Login Testing [Generic] (***************)
Audience: [Use arrows to move, type to filter, ? for more help]
> Auth0 Management API ([<- のAudience])
Auth0 Practice API ([<- のAudience])
ここで Auth0 Practice API
を選択。
% auth0 test token
Client ID: CLI Login Testing [Generic] ((***************))
Audience: Auth0 Practice API ([<- のAudience])
▸ Domain : [Auth0 Tenant ID]
▸ Client ID : [CLIENT ID]
▸ Type : Generic
? Scopes [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]
> [ ] read:email
[ ] create:email
[ ] update:email
[ ] delete:email
[ ] read:order
[ ] create:order
[ ] update:order
このように Scope の選択画面になります。
(ここは別途検証予定なのもあり) 適当に入力して Enter
します。
? Scopes read:email, create:email, update:email, delete:email, read:order, create:order, update:order, delete:order
▸ A browser window needs to be opened to complete this client's login flow.
▸ Once login is complete, you can return to the CLI to view user profile information and tokens.
Do you wish to proceed? (y/N)
y
を入力。
Do you wish to proceed? Yes
Waiting for login flow to complete... done
=== ************* token for CLI Login Testing
TOKEN TYPE Bearer
EXPIRES IN 1440 minute(s)
ACCESS TOKEN [Access Token] <-- これ
こんな感じで Access Token が取れます。
これを使って localhost:8080
の API に投げ込んでみます。
curl --request GET --url http://localhost:8080/authorized --header 'authorization: Bearer [Access Token]'
{"message":"Secured Resource"}
ちゃんと動きますね。
JWT の中身を見てみる
JWT.io に Access Token を貼り付けてみるとこんな感じです。
{
"iss": "https://***************.us.auth0.com/",
"sub": "google-oauth2|107491931981912591082",
"aud": "https://auth0.com/***************/api/auth0-practice-api",
"iat": 1737957063,
"exp": 1738043463,
"scope": "read:email create:email update:email delete:email read:order create:order update:order delete:order",
"azp": "***************"
}
ちゃんと Scope 入ってますね。
これを使えば受け側の認可は普通にできそうです。
ただ、実際には認証時点で特定の条件に合致しない場合は弾くってことができると思っていて、次回それについて調べていこうかなと思ってます。