Skip to content

Commit a3b2269

Browse files
Deprecate has_additional_scopes_key
and instead use only get_additional_scopes_key As Per @kjnilsson suggestion
1 parent 22aa517 commit a3b2269

File tree

3 files changed

+52
-41
lines changed

3 files changed

+52
-41
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ expiry_timestamp(#auth_user{impl = DecodedTokenFun}) ->
124124
authenticate(_, AuthProps0) ->
125125
AuthProps = to_map(AuthProps0),
126126
Token = token_from_context(AuthProps),
127-
127+
128128
case check_token(Token) of
129129
%% avoid logging the token
130130
{error, _} = E -> E;
@@ -247,10 +247,10 @@ post_process_payload_with_scope_alias_in_extra_scopes_source(ResourceServerId, P
247247
ExtraScopesField = rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId),
248248
case ExtraScopesField of
249249
%% nothing to inject
250-
undefined -> Payload;
251-
_ ->
250+
{error, not_found} -> Payload;
251+
{ok, ExtraScopes} ->
252252
ScopeMappings = rabbit_oauth2_config:get_scope_aliases(ResourceServerId),
253-
post_process_payload_with_scope_alias_field_named(Payload, ExtraScopesField, ScopeMappings)
253+
post_process_payload_with_scope_alias_field_named(Payload, ExtraScopes, ScopeMappings)
254254
end.
255255

256256

@@ -281,38 +281,42 @@ post_process_payload_with_scope_alias_field_named(Payload, FieldName, ScopeAlias
281281

282282
-spec does_include_complex_claim_field(ResourceServerId :: binary(), Payload :: map()) -> boolean().
283283
does_include_complex_claim_field(ResourceServerId, Payload) when is_map(Payload) ->
284-
case rabbit_oauth2_config:has_additional_scopes_key(ResourceServerId) of
285-
true -> maps:is_key(rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId), Payload);
286-
false -> false
284+
case rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId) of
285+
{ok, ScopeKey} -> maps:is_key(ScopeKey, Payload);
286+
{error, not_found} -> false
287287
end.
288288

289289
-spec post_process_payload_with_complex_claim(ResourceServerId :: binary(), Payload :: map()) -> map().
290290
post_process_payload_with_complex_claim(ResourceServerId, Payload) ->
291-
ComplexClaim = maps:get(rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId), Payload),
292-
AdditionalScopes =
293-
case ComplexClaim of
294-
L when is_list(L) -> L;
295-
M when is_map(M) ->
296-
case maps:get(ResourceServerId, M, undefined) of
297-
undefined -> [];
298-
Ks when is_list(Ks) ->
299-
[erlang:iolist_to_binary([ResourceServerId, <<".">>, K]) || K <- Ks];
300-
ClaimBin when is_binary(ClaimBin) ->
301-
UnprefixedClaims = binary:split(ClaimBin, <<" ">>, [global, trim_all]),
302-
[erlang:iolist_to_binary([ResourceServerId, <<".">>, K]) || K <- UnprefixedClaims];
303-
_ -> []
304-
end;
305-
Bin when is_binary(Bin) ->
306-
binary:split(Bin, <<" ">>, [global, trim_all]);
307-
_ -> []
308-
end,
309-
310-
case AdditionalScopes of
311-
[] -> Payload;
312-
_ ->
313-
ExistingScopes = maps:get(?SCOPE_JWT_FIELD, Payload, []),
314-
maps:put(?SCOPE_JWT_FIELD, AdditionalScopes ++ ExistingScopes, Payload)
315-
end.
291+
case rabbit_oauth2_config:get_additional_scopes_key(ResourceServerId) of
292+
{ok, ScopesKey} ->
293+
ComplexClaim = maps:get(ScopesKey, Payload),
294+
AdditionalScopes =
295+
case ComplexClaim of
296+
L when is_list(L) -> L;
297+
M when is_map(M) ->
298+
case maps:get(ResourceServerId, M, undefined) of
299+
undefined -> [];
300+
Ks when is_list(Ks) ->
301+
[erlang:iolist_to_binary([ResourceServerId, <<".">>, K]) || K <- Ks];
302+
ClaimBin when is_binary(ClaimBin) ->
303+
UnprefixedClaims = binary:split(ClaimBin, <<" ">>, [global, trim_all]),
304+
[erlang:iolist_to_binary([ResourceServerId, <<".">>, K]) || K <- UnprefixedClaims];
305+
_ -> []
306+
end;
307+
Bin when is_binary(Bin) ->
308+
binary:split(Bin, <<" ">>, [global, trim_all]);
309+
_ -> []
310+
end,
311+
312+
case AdditionalScopes of
313+
[] -> Payload;
314+
_ ->
315+
ExistingScopes = maps:get(?SCOPE_JWT_FIELD, Payload, []),
316+
maps:put(?SCOPE_JWT_FIELD, AdditionalScopes ++ ExistingScopes, Payload)
317+
end;
318+
{error, not_found} -> Payload
319+
end.
316320

317321
-spec post_process_payload_in_keycloak_format(Payload :: map()) -> map().
318322
%% keycloak token format: https://github.com/rabbitmq/rabbitmq-auth-backend-oauth2/issues/36

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_config.erl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,22 @@ has_additional_scopes_key(TopResourceServerId, ResourceServerId) when ResourceSe
232232
_ -> true
233233
end.
234234

235-
-spec get_additional_scopes_key() -> binary() | undefined.
236-
get_additional_scopes_key() -> application:get_env(?APP, extra_scopes_source, undefined).
235+
-spec get_additional_scopes_key() -> {ok, binary()} | {error, not_found}.
236+
get_additional_scopes_key() ->
237+
case application:get_env(?APP, extra_scopes_source, undefined) of
238+
undefined -> {error, not_found};
239+
ScopeKey -> {ok, ScopeKey}
240+
end.
237241

238-
-spec get_additional_scopes_key(binary()) -> binary() | undefined .
242+
-spec get_additional_scopes_key(binary()) -> {ok, binary()} | {error, not_found}.
239243
get_additional_scopes_key(ResourceServerId) -> get_additional_scopes_key(get_default_resource_server_id(), ResourceServerId).
240244
get_additional_scopes_key(TopResourceServerId, ResourceServerId) when ResourceServerId =:= TopResourceServerId -> get_additional_scopes_key();
241245
get_additional_scopes_key(TopResourceServerId, ResourceServerId) when ResourceServerId =/= TopResourceServerId ->
242-
proplists:get_value(extra_scopes_source, maps:get(ResourceServerId, application:get_env(?APP, resource_servers, #{}), []),
243-
get_additional_scopes_key()).
246+
case proplists:get_value(extra_scopes_source, maps:get(ResourceServerId, application:get_env(?APP, resource_servers, #{}), [])) of
247+
undefined -> get_additional_scopes_key();
248+
<<>> -> get_additional_scopes_key();
249+
ScopeKey -> {ok, ScopeKey}
250+
end.
244251

245252

246253
-spec get_scope_prefix() -> binary().

deps/rabbitmq_auth_backend_oauth2/test/rabbit_oauth2_config_SUITE.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,13 +511,13 @@ get_key_config(_Config) ->
511511
?assertEqual(<<"https://oauth-for-rabbitmq1">>, proplists:get_value(jwks_url, KeyConfig)).
512512

513513
get_additional_scopes_key(_Config) ->
514-
?assertEqual(<<"roles">>, rabbit_oauth2_config:get_additional_scopes_key()),
515-
?assertEqual(<<"extra-scope-1">>, rabbit_oauth2_config:get_additional_scopes_key(<<"rabbitmq1">> )),
514+
?assertEqual({ok, <<"roles">>}, rabbit_oauth2_config:get_additional_scopes_key()),
515+
?assertEqual({ok, <<"extra-scope-1">>}, rabbit_oauth2_config:get_additional_scopes_key(<<"rabbitmq1">> )),
516516
?assertEqual(rabbit_oauth2_config:get_additional_scopes_key(), rabbit_oauth2_config:get_additional_scopes_key(<<"rabbitmq2">>)),
517-
?assertEqual(<<"roles">>, rabbit_oauth2_config:get_additional_scopes_key(?RABBITMQ)).
517+
?assertEqual({ok, <<"roles">>}, rabbit_oauth2_config:get_additional_scopes_key(?RABBITMQ)).
518518

519519
get_additional_scopes_key_when_not_defined(_Config) ->
520-
?assertEqual(undefined, rabbit_oauth2_config:get_additional_scopes_key()),
520+
?assertEqual({error, not_found}, rabbit_oauth2_config:get_additional_scopes_key()),
521521
?assertEqual(rabbit_oauth2_config:get_additional_scopes_key(), rabbit_oauth2_config:get_additional_scopes_key(<<"rabbitmq2">>)).
522522

523523
is_verify_aud(_Config) ->

0 commit comments

Comments
 (0)