Skip to content

Commit 043520f

Browse files
WIP Refactor code
before implementing oidc endpoints parameters
1 parent 2afa0f4 commit 043520f

File tree

8 files changed

+233
-286
lines changed

8 files changed

+233
-286
lines changed

deps/rabbitmq_auth_backend_oauth2/priv/schema/rabbitmq_auth_backend_oauth2.schema

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,19 @@
158158
"rabbitmq_auth_backend_oauth2.authorization_endpoint",
159159
[{datatype, string}, {validators, ["uri", "https_uri"]}]}.
160160

161-
%% auth_oauth2.authorization_endpoint = https://a.com/authorize
162-
%% auth_oauth2.authorization_endpoint.params.resource = ${resource_id}
163-
%% auth_oauth2.authorization_endpoint.params.audience = ${resource_id}
161+
%% auth_oauth2.authorization_endpoint_params.audience
162+
%% auth_oauth2.resource_servers.rabbitmq.authorization_endpoint_params.audience
163+
%% auth_oauth2.resource_servers.rabbitmq.token_endpoint_params.audience
164+
%% auth_oauth2.resource_servers.rabbitmq.jkws_uri_params.appId =
164165

165166
{mapping,
166-
"auth_oauth2.authorization_endpoint.params.$param",
167-
"rabbitmq_auth_backend_oauth2.authorization_endpoint.req_params",
167+
"auth_oauth2.authorization_endpoint_params.$param",
168+
"rabbitmq_auth_backend_oauth2.oauth_providers",
168169
[{datatype, string}]}.
169170

170-
{translation, "rabbitmq_auth_backend_oauth2.authorization_endpoint.req_params",
171+
{translation, "rabbitmq_auth_backend_oauth2.authorization_endpoint_params",
171172
fun(Conf) ->
172-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.authorization_endpoint.req_params", Conf),
173-
rabbit_oauth2_schema:translate_endpoint_req_params(Settings)
173+
rabbit_oauth2_schema:translate_authorization_endpoint_params(Conf)
174174
end}.
175175

176176
{mapping,
@@ -326,6 +326,7 @@
326326
[{datatype, string}]
327327
}.
328328

329+
329330
{mapping,
330331
"auth_oauth2.resource_servers.$name.scope_prefix",
331332
"rabbitmq_auth_backend_oauth2.resource_servers",

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,36 +221,39 @@ post_process_payload(ResourceServerId, Payload) when is_map(Payload) ->
221221
Payload4.
222222

223223

224-
-spec post_process_payload_with_scope_aliases(ResourceServerId :: binary(), Payload :: map()) -> map().
224+
-spec post_process_payload_with_scope_aliases(
225+
ResourceServer :: rabbit_oauth2_config:resource_server(), Payload :: map()) -> map().
225226
%% This is for those hopeless environments where the token structure is so out of
226227
%% messaging team's control that even the extra scopes field is no longer an option.
227228
%%
228229
%% This assumes that scopes can be random values that do not follow the RabbitMQ
229230
%% convention, or any other convention, in any way. They are just random client role IDs.
230231
%% See rabbitmq/rabbitmq-server#4588 for details.
231-
post_process_payload_with_scope_aliases(ResourceServerId, Payload) ->
232+
post_process_payload_with_scope_aliases(ResourceServer, Payload) ->
232233
%% try JWT scope field value for alias
233-
Payload1 = post_process_payload_with_scope_alias_in_scope_field(ResourceServerId, Payload),
234+
Payload1 = post_process_payload_with_scope_alias_in_scope_field(ResourceServer, Payload),
234235
%% try the configurable 'extra_scopes_source' field value for alias
235-
post_process_payload_with_scope_alias_in_extra_scopes_source(ResourceServerId, Payload1).
236+
post_process_payload_with_scope_alias_in_extra_scopes_source(ResourceServer, Payload1).
236237

237238

238-
-spec post_process_payload_with_scope_alias_in_scope_field(ResourceServerId :: binary(), Payload :: map()) -> map().
239+
-spec post_process_payload_with_scope_alias_in_scope_field(
240+
ResourceServer :: rabbit_oauth2_config:resource_server(), Payload :: map()) -> map().
239241
%% First attempt: use the value in the 'scope' field for alias
240-
post_process_payload_with_scope_alias_in_scope_field(ResourceServerId, Payload) ->
241-
ScopeMappings = rabbit_oauth2_config:get_scope_aliases(ResourceServerId),
242+
post_process_payload_with_scope_alias_in_scope_field(ResourceServer, Payload) ->
243+
ScopeMappings = ResourceServer#resource_server.scope_aliases,
242244
post_process_payload_with_scope_alias_field_named(Payload, ?SCOPE_JWT_FIELD, ScopeMappings).
243245

244246

245-
-spec post_process_payload_with_scope_alias_in_extra_scopes_source(ResourceServerId :: binary(), Payload :: map()) -> map().
247+
-spec post_process_payload_with_scope_alias_in_extra_scopes_source(
248+
ResourceServer :: rabbit_oauth2_config:resource_server(), Payload :: map()) -> map().
246249
%% Second attempt: use the value in the configurable 'extra scopes source' field for alias
247-
post_process_payload_with_scope_alias_in_extra_scopes_source(ResourceServerId, Payload) ->
248-
ExtraScopesField = rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId),
250+
post_process_payload_with_scope_alias_in_extra_scopes_source(ResourceServer, Payload) ->
251+
ExtraScopesField = ResourceServer#resource_server.additional_scopes_key,
249252
case ExtraScopesField of
250253
%% nothing to inject
251254
{error, not_found} -> Payload;
252255
{ok, ExtraScopes} ->
253-
ScopeMappings = rabbit_oauth2_config:get_scope_aliases(ResourceServerId),
256+
ScopeMappings = ResourceServer#resource_server.scope_aliases,
254257
post_process_payload_with_scope_alias_field_named(Payload, ExtraScopes, ScopeMappings)
255258
end.
256259

@@ -280,16 +283,19 @@ post_process_payload_with_scope_alias_field_named(Payload, FieldName, ScopeAlias
280283
maps:put(?SCOPE_JWT_FIELD, ExpandedScopes, Payload).
281284

282285

283-
-spec does_include_complex_claim_field(ResourceServerId :: binary(), Payload :: map()) -> boolean().
284-
does_include_complex_claim_field(ResourceServerId, Payload) when is_map(Payload) ->
285-
case rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId) of
286+
-spec does_include_complex_claim_field(
287+
ResourceServer :: rabbit_oauth2_config:resource_server(), Payload :: map()) -> boolean().
288+
does_include_complex_claim_field(ResourceServer, Payload) when is_map(Payload) ->
289+
case ResourceServer#resource_server.additional_scopes_key of
286290
{ok, ScopeKey} -> maps:is_key(ScopeKey, Payload);
287291
{error, not_found} -> false
288292
end.
289293

290-
-spec post_process_payload_with_complex_claim(ResourceServerId :: binary(), Payload :: map()) -> map().
291-
post_process_payload_with_complex_claim(ResourceServerId, Payload) ->
292-
case rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId) of
294+
-spec post_process_payload_with_complex_claim(
295+
ResourceServer :: rabbit_oauth2_config:resource_server(), Payload :: map()) -> map().
296+
post_process_payload_with_complex_claim(ResourceServer, Payload) ->
297+
ResourceServerId = ResourceServer#resource_server.id,
298+
case ResourceServer#resource_server.additional_scopes_key of
293299
{ok, ScopesKey} ->
294300
ComplexClaim = maps:get(ScopesKey, Payload),
295301
AdditionalScopes =
@@ -479,10 +485,12 @@ is_recognized_permission(#{?ACTIONS_FIELD := _, ?LOCATIONS_FIELD:= _ , ?TYPE_FIE
479485
is_recognized_permission(_, _) -> false.
480486

481487

482-
-spec post_process_payload_in_rich_auth_request_format(ResourceServerId :: binary(), Payload :: map()) -> map().
488+
-spec post_process_payload_in_rich_auth_request_format(ResourceServer :: resource_server(),
489+
Payload :: map()) -> map().
483490
%% https://oauth.net/2/rich-authorization-requests/
484-
post_process_payload_in_rich_auth_request_format(ResourceServerId, #{<<"authorization_details">> := Permissions} = Payload) ->
485-
ResourceServerType = rabbit_oauth2_config:get_resource_server_type(ResourceServerId),
491+
post_process_payload_in_rich_auth_request_format(ResourceServer,
492+
#{<<"authorization_details">> := Permissions} = Payload) ->
493+
ResourceServerType = ResourceServer#resource_server.resource_server_type,
486494

487495
FilteredPermissionsByType = lists:filter(fun(P) ->
488496
is_recognized_permission(P, ResourceServerType) end, Permissions),

0 commit comments

Comments
 (0)