@@ -1017,66 +1017,187 @@ idpyoidc\.server\.configure module
10171017 :show-inheritance:
10181018
10191019
1020- ==============
1020+ ====================
10211021Resource Indicators
1022- ==============
1023- There are two possible ways to configure Resource Indicators in OIDC-OP, globally and per-client.
1024- For the first case the configuration is passed in the Authorization or Access Token endpoint arguments throught the
1025- `resource_indicators ` dictionary.
1026-
1027- If present, the resource indicators configuration should contain a `policy ` dictionary
1028- that defines the behaviour of the specific endpoint. The policy
1029- is mapped to a dictionary with the keys `callable ` (mandatory), which must be a
1030- python callable or a string that represents the path to a python callable, and
1031- `kwargs ` (optional), which must be a dict of key-value arguments that will be
1032- passed to the callable.
1033-
1034- The resource indicators configuration may also contain a `resource_servers_per_client `
1035- dictionary that defines a mapping between oidc-op registered clients with key the equivalent `client id ` and resources to whom this client
1036- is eligible to request access.
1037-
1038- "resource_indicators":{
1039- "policy": {
1040- "callable": validate_authorization_resource_indicators_policy,
1041- "kwargs": {
1042- "resource_servers_per_client": {
1043- "CLIENT_1": ["RESOURCE_1"],
1044- "CLIENT_2": ["RESOURCE_1", "RESOURCE_2"]
1022+ ====================
1023+
1024+ There are two ways to configure resource indicators: per-endpoint (the
1025+ default for every client) and per-client (overriding that default for
1026+ one client). They apply to the authorization, token
1027+ (``authorization_code ``, ``client_credentials ``, ``token_exchange ``) and
1028+ introspection endpoints.
1029+
1030+ ---------------------------
1031+ enable_resource_indicators
1032+ ---------------------------
1033+
1034+ Each endpoint's ``kwargs `` accepts an ``enable_resource_indicators ``
1035+ boolean, default ``False ``. Without it, any ``resource `` parameter a
1036+ client sends is ignored outright - never validated, never reflected in
1037+ the issued token - and the token's ``aud `` falls back to the default
1038+ described below, exactly as if the client had sent no ``resource `` at
1039+ all.
1040+
1041+ -------------------
1042+ resource_indicators
1043+ -------------------
1044+
1045+ The endpoint-level default policy, used for any client with no
1046+ ``resource_indicators `` entry of its own. ``policy `` maps to a dict with
1047+ ``function `` (mandatory - a callable, or an importable dotted-path
1048+ string) and ``kwargs `` (optional)::
1049+
1050+ "resource_indicators": {
1051+ "policy": {
1052+ "function": validate_resource_indicators_policy,
1053+ "kwargs": {
1054+ "resource_servers_per_client": {
1055+ "CLIENT_1": ["RESOURCE_1"],
1056+ "CLIENT_2": ["RESOURCE_1", "RESOURCE_2"],
1057+ },
10451058 },
1046- },
10471059 },
1048- },
10491060 }
10501061
1051- For the per-client configuration a similar configuration scheme should be present in the client's
1052- metadata under the `resource_indicators ` key with slight difference. The `policy ` mapping should be set a value for a
1053- key `authorization_code ` or `access_token ` in order to indicate the endpoint that this resource indicators policy is reffered to.
1054- In addition, the `resource_servers_per_client ` value is a list of the permitted resources.
1062+ ``resource_servers_per_client `` is either a flat list of resource
1063+ identifiers valid for every client, or a ``{client_id: [resource, ...]} ``
1064+ mapping for per-client restriction. A client may always additionally
1065+ request itself as a resource, even without an explicit entry. If any
1066+ requested resource falls outside the permitted set, the entire request
1067+ is rejected with ``invalid_target `` - never narrowed to the valid subset.
10551068
1056- For example::
1069+ --------------------
1070+ Per-client override
1071+ --------------------
10571072
1058- "resource_indicators":{
1059- "authorization_code": {
1060- "policy": {
1061- "callable": validate_authorization_resource_indicators_policy,
1062- "kwargs": {
1063- "resource_servers_per_client": ["RESOURCE_1"],
1064- },
1065- },
1066- },
1067- },
1068- }
1073+ A client database entry may carry its own ``resource_indicators ``,
1074+ overriding the endpoint-level default for that client only. It takes a
1075+ ``policy `` dict directly, the same shape as the endpoint-level default
1076+ above::
10691077
1070- The policy callable accepts a specific argument list and must return the altered token
1071- request or raise an exception.
1078+ endpoint_context.cdb["client_1"]["resource_indicators"] = {
1079+ "policy": {
1080+ "function": validate_resource_indicators_policy,
1081+ "kwargs": {"resource_servers_per_client": ["RESOURCE_1"]},
1082+ },
1083+ }
10721084
1073- For example::
1085+ --------------------
1086+ The policy function
1087+ --------------------
1088+
1089+ A policy function is called as ``fn(request, context, **kwargs) ``:
1090+
1091+ - ``request `` - the parsed token or authorization request (a ``Message ``
1092+ or dict), including whatever the client sent (``client_id ``,
1093+ ``resource ``, ``scope ``, ...). The function may read it, and may
1094+ mutate it in place - the engine uses this ``request `` object
1095+ afterwards, so changes (e.g. narrowing ``request["resource"] `` or
1096+ ``request["scope"] ``) take effect.
1097+ - ``context `` - the endpoint's ``EndpointContext ``, giving access to
1098+ ``context.cdb `` (the client database) and the rest of the server's
1099+ runtime state.
1100+ - ``**kwargs `` - whatever is in this policy's own ``kwargs `` dict from
1101+ configuration (``resource_servers_per_client `` above, or anything
1102+ else a custom function declares and expects to be configured with).
1103+
1104+ It must return either the (possibly modified) ``request `` to let
1105+ processing continue normally, or an error response
1106+ (``TokenErrorResponse ``, or ``AuthorizationErrorResponse `` at the
1107+ authorization endpoint) to abort the request with that error::
10741108
10751109 def validate_resource_indicators_policy(request, context, **kwargs):
10761110 if some_condition in request:
1077- return TokenErrorResponse(
1078- error="invalid_request ", error_description="Some error occured "
1111+ return TokenErrorResponse(
1112+ error="invalid_target ", error_description="Some error occurred "
10791113 )
1080-
10811114 return request
10821115
1116+ -----------------
1117+ Default audience
1118+ -----------------
1119+
1120+ Every issued access token gets an ``aud `` claim regardless of
1121+ ``enable_resource_indicators ``. When the client doesn't request a
1122+ resource, or resource indicators aren't enabled for that request,
1123+ ``aud `` defaults to the requesting client's own ``client_id `` - it is
1124+ never left empty (RFC 9068 Section 3 requires a default resource
1125+ indicator, not just permits one). A client-supplied ``resource `` is
1126+ also never trusted into ``aud `` unless validated by this policy.
1127+
1128+
1129+ ==================
1130+ Audience Policies
1131+ ==================
1132+
1133+ A second hook, run after resource-indicator validation and immediately
1134+ before a token is minted, for computing or restricting the final
1135+ ``aud `` beyond what resource indicators alone determine.
1136+
1137+ -------------------------
1138+ enable_audience_policies
1139+ -------------------------
1140+
1141+ Same pattern: an endpoint's ``kwargs `` needs
1142+ ``enable_audience_policies: True `` or ``audience_policies `` is never
1143+ consulted.
1144+
1145+ ------------------
1146+ audience_policies
1147+ ------------------
1148+
1149+ A mapping from ``client_id `` to a list of policy specs, with ``"" `` as
1150+ the default for any client without an entry of its own::
1151+
1152+ "audience_policies": {
1153+ "client_1": [{"function": my_audience_policy, "kwargs": {...}}],
1154+ "": [{"function": default_audience_policy, "kwargs": {...}}],
1155+ }
1156+
1157+ ----------------------------
1158+ The audience-policy function
1159+ ----------------------------
1160+
1161+ A policy function is called as
1162+ ``fn(request, context, client_id, client_info, audience, grant, **kwargs) ``:
1163+
1164+ - ``request `` - the parsed token or authorization request. May be read and
1165+ mutated in place, same as for a resource-indicators policy function.
1166+ - ``context `` - the endpoint's ``EndpointContext ``.
1167+ - ``client_id `` - the requesting client's id (also present in ``request ``,
1168+ passed separately for convenience).
1169+ - ``client_info `` - that client's entry in ``context.cdb ``.
1170+ - ``audience `` - read-only input: the intended audience of the token so
1171+ far. Contains ``request.get("resource") `` (or whatever
1172+ ``resource_indicators `` narrowed it to, if that ran first) at the
1173+ authorization and client_credentials endpoints; the existing grant's or
1174+ token's own ``resources `` at the refresh and introspection endpoints,
1175+ since ``RefreshAccessTokenRequest ``/``TokenIntrospectionRequest `` have no
1176+ ``resource `` field to read in the first place. Reassigning or mutating it
1177+ has no effect - to change the audience, write to ``request `` or ``grant ``
1178+ instead, as in the examples below.
1179+ - ``grant `` - the session grant the token is being minted from.
1180+ - ``**kwargs `` - this policy's own ``kwargs `` from configuration.
1181+
1182+ **Unlike a resource-indicators policy function, the return value is
1183+ ignored. ** To abort the request, set ``request["error"] `` and
1184+ ``request["error_description"] `` directly; anything else (returning
1185+ ``None ``, returning the request, returning nothing) just lets processing
1186+ continue::
1187+
1188+ def require_resource_present(request, context, client_id, client_info, audience, grant, **kwargs):
1189+ if not request.get("resource"):
1190+ request["error"] = "audience_policy_error"
1191+ request["error_description"] = "No resource in the request"
1192+ # no explicit return - continuing is the default when no error is set
1193+
1194+ That one only restricts. A function can just as well compute/extend the
1195+ audience instead, by mutating ``grant.resources `` in place - this one adds
1196+ the client's own id to whatever resources were already there::
1197+
1198+ def add_self_as_audience(request, context, client_id, client_info, audience, grant, **kwargs):
1199+ resources = grant.resources or []
1200+ if client_id not in resources:
1201+ resources.append(client_id)
1202+ grant.resources = resources
1203+
0 commit comments