0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Auth0 アプリケーションメタデータによるAPIアクセス制御方法 メモ

Posted at

やりたいこと

  • クライアント(アプリケーション)別に呼び出し可能なAPIを定義し、認可制御を行う。

    auth0_api_access.png

設定方法

アプリケーション

  • Application -> Advanced Settings -> Application Metadataに呼び出し可能なAPIを定義する。
    • Key:allowed_api_list
    • Value:事前定義したAPIのIdentifier(複数指定したい場合は、カンマ区切りで入力する)

Actions

  • Loginフローにcheck_apiというActionを定義する。

  • 以下のコードを記述する。

    exports.onExecutePostLogin = async (event, api) => {
      // クライアントの呼び出し可能APIリストを取得する。
      const api_list = event.client.metadata.allowed_api_list.split(',');
      // 認可リクエストに指定されたAPI(audience)がAPIリストに含まれているか確認する。
      if(!api_list.includes(event.request.query['audience'])){
        api.access.deny("This client not allowed");
      }
    };
    

動作確認

  • 正常系確認

    1. 認可エンドポイントへアクセスする。

      GET /authorize?state=aobraoenbaoer134r493yhvahro&redirect_uri=http://localhost:3000&client_id={client_id}&response_type=code&scope=test&audience={Appication MetadataのAllowed APIリストに定義したAPI identifier} HTTP/1.1
      Host: {YOUR_TENANT_ID}
      

      audienceにアプリケーションメタデータのallowed_api_listに定義したIdentifierを設定する。

    2. ログインする。(クエリに認可コードが付与され、リダイレクトURIにリダイレクトされる)

  • 異常系確認

    1. 認可エンドポイントへアクセスする。

      GET /authorize?state=aobraoenbaoer134r493yhvahro&redirect_uri=http://localhost:3000&client_id={client_id}&response_type=code&scope=test&audience={Appication MetadataのAllowed APIリストに定義していないAPI identifier} HTTP/1.1
      Host: {YOUR_TENANT_ID}
      

      audienceにアプリケーションメタデータのallowed_api_listに定義していないIdentifierを設定する。

    2. ログインする。(クエリにerror=access_denied&error_description=This%20client%20not%20allowedが付与され、リダイレクトURIにリダイレクトされる)

参考情報

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?