🅰️

エラー: Authorization server not configured with default connection を回避する

に公開

エラーの概要

Auth0 で、Multi-factor Auth や machine to machine application で token を取得したいとなった場合に、API POST /oauth/token にリクエストするとAuthorization server not configured with default connection. というエラーメッセージが返ってくることがある。

この場合、Default Directory を設定するという解決方法があるが、同一テナント内で複数のデータベースを使い分けている場合 には Default Directory の設定では解決できない。
Default Directory の設定以外での対応方法を記録しておく。

解決策: API にリクエストする際に、realm にデータベースの表示名を指定する

Auth0 の Ruby クライアント ruby-auth0 から login_with_resource_owner を呼び出して token を取得する場合を想定する。

app(main):001:0:> auth0_client = Auth0Client.new(client_id: ENV['AUTH)_CLIENT_ID'], client_secret: ENV['AUTH0_CLIENT_SECRET'], domain: ENV['AUTH0_DOMAIN'])
app(main):002:0:> auth0_client.login_with_resource_owner('username', 'password')
(app):2:in '<main>': {"error":"server_error","error_description":"Authorization server not configured with default connection."} (Auth0::ServerError)

login_with_resource_owner の定義は以下のようになっている。
https://github.com/auth0/ruby-auth0/blob/aed1d67f9c9213b99a13f371e10ef8feebe7fee0/lib/auth0/api/authentication_endpoints.rb#L141-L182

このメソッドを呼び出して token を取得するとき、キーワード引数: realm に対し、認証したい User の情報が含まれているデータベースの接続名を指定することで解決できる。
成功すれば、各種 token の入った struct が返ってくる。

app(main):001:0:> auth0_client = Auth0Client.new(client_id: ENV['AUTH)_CLIENT_ID'], client_secret: ENV['AUTH0_CLIENT_SECRET'], domain: ENV['AUTH0_DOMAIN'])
app(main):002:0:> auth0_client.login_with_resource_owner('username', 'password', realm: 'MainAccounts-mine')
:
#<struct Auth0::AccessToken
 access_token=
  "eyJhbG...",
 expires_in=86400,
 refresh_token=nil,
 id_token=
  "eyJhbG...">

realm について言及されているドキュメント

「Resource Owner Password Flow」 のページに realm support の項があり、接続先を切り替えられるような説明があった。
https://auth0.com/docs/get-started/authentication-and-authorization-flow/resource-owner-password-flow#realm-support

「Call Your API Using Resource Owner Password Flow」のページにも同様に説明があった。上から読んでいくと、Default Directory についての話が先に登場するが、Configure realm support の項に接続を切り替えられるというような説明がある。

尚、ここで言及されている grant_type: http://auth0.com/oauth/grant-type/password-realm は、ruby-auth0 の login_with_resource_owner メソッド内では realm の指定があればこの grant_type を自動的に使うようになっている。
https://auth0.com/docs/get-started/authentication-and-authorization-flow/resource-owner-password-flow/call-your-api-using-resource-owner-password-flow#configure-realm-support

auth0 community には、Authorization server not configured with default connection. のエラーの解消法として realm に言及しているページがあった。
https://community.auth0.com/t/attempt-to-get-token-with-ropg-returns-error-authorization-server-not-configured-with-default-connection/114452

まとめ

API にリクエストをして Authorization server not configured with default connection. というエラーメッセージを受け取った場合には、Default Directory を指定して回避する方法もあるが、複数のデータベースを使い分けている場合は realm にデータベースの接続名を指定してリクエストすることで回避ができる。

realm を指定してリクエストする場合は grant_type: http://auth0.com/oauth/grant-type/password-realm を指定する必要があるが、Ruby クライアントの login_with_resource_owner を利用している場合は realm を指定すると内部で自動的に指定されるため、意識する必要はない。

あしたのチーム Tech Blog

Discussion