5.11. ポリシー評価 API
JavaScript を使用してルールベースのポリシーを作成する場合、Red Hat build of Keycloak は、パーミッションを付与するかどうかを判断するために役立つ情報を提供する評価用 API を提供します。
この API は、以下のような情報へのアクセスを提供するいくつかのインターフェイスで設定されています。
- 評価されるパーミッション (要求されているリソースとスコープの両方)。
- 要求されるリソースに関連付けられた属性
- ランタイム環境と、実行コンテキストに関連付けられたその他の属性
- グループメンバーシップおよびロールなどのユーザーに関する情報
メインインターフェイスは org.keycloak.authorization.policy.evaluation.Evaluation で、以下のコントラクトを定義します。
public interface Evaluation { /** * Returns the {@link ResourcePermission} to be evaluated. * * @return the permission to be evaluated */ ResourcePermission getPermission(); /** * Returns the {@link EvaluationContext}. Which provides access to the whole evaluation runtime context. * * @return the evaluation context */ EvaluationContext getContext(); /** * Returns a {@link Realm} that can be used by policies to query information. * * @return a {@link Realm} instance */ Realm getRealm(); /** * Grants the requested permission to the caller. */ void grant(); /** * Denies the requested permission. */ void deny(); }
認証リクエストを処理する際に、Red Hat build of Keycloak はポリシーを評価する前に Evaluation
インスタンスを作成します。その後、このインスタンスは各ポリシーに渡され、アクセスが GRANT または DENY かどうかが判断されます。
ポリシーは、Evaluation
インスタンスで grant()
または deny()
メソッドを呼び出してこれを決定します。デフォルトでは、評価
インスタンスの状態は拒否されます。つまり、ポリシー評価エンジンに対してパーミッションを付与する必要があることを示す grant()
メソッドを明示的に呼び出す必要があります。
関連情報
5.11.1. 評価コンテキスト
評価コンテキストは、評価時にポリシーに有用な情報を提供します。
public interface EvaluationContext { /** * Returns the {@link Identity} that represents an entity (person or non-person) to which the permissions must be granted, or not. * * @return the identity to which the permissions must be granted, or not */ Identity getIdentity(); /** * Returns all attributes within the current execution and runtime environment. * * @return the attributes within the current execution and runtime environment */ Attributes getAttributes(); }
このインターフェイスから、ポリシーは以下を取得できます。
-
認証された
ID
- 実行コンテキストおよびランタイム環境に関する情報
Identity
は、認可要求と共に送信された OAuth2 アクセストークンに基づいてビルドされ、このコンストラクトは元のトークンから抽出されたすべての要求にアクセスできます。たとえば、プロトコルマッパー を使用して OAuth2 アクセストークンにカスタム要求を組み込む場合は、ポリシーからこの要求にアクセスし、これを使用して条件を構築することもできます。
EvaluationContext
は、実行環境とランタイム環境の両方に関連する属性へのアクセスも提供します。ここでは、いくつかの組み込み属性のみがあります。
名前 | 説明 | 種類 |
---|---|---|
kc.time.date_time | 現在の日時 |
文字列。 |
kc.client.network.ip_address | クライアントの IPv4 アドレス | 文字列 |
kc.client.network.host | クライアントのホスト名 | 文字列 |
kc.client.id | クライアント ID | 文字列 |
kc.client.user_agent | User-AgentHTTP ヘッダーの値 | String[] |
kc.realm.name | レルムの名前 | 文字列 |