Skip to content

Commit 975317f

Browse files
Final simplification of function
1 parent 37d20ef commit 975317f

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
get_scope/1, set_scope/2,
2929
resolve_resource_server/1]).
3030

31-
-import(rabbit_oauth2_rar, [extract_scopes_from_rich_auth_request/2, has_rich_auth_request_scopes/1]).
31+
-import(rabbit_oauth2_rar, [extract_scopes_from_rich_auth_request/2]).
3232

3333
-import(rabbit_oauth2_scope, [filter_matching_scope_prefix_and_drop_it/2]).
3434

@@ -241,24 +241,19 @@ extract_scopes_from_scope_claim(Payload) ->
241241
ResourceServer :: resource_server(), DecodedToken :: decoded_jwt_token()) -> map().
242242
normalize_token_scope(ResourceServer, Payload) ->
243243

244-
Payload2 = extract_scopes_using_scope_aliases(ResourceServer,
244+
Payload1 = extract_scopes_from_rich_auth_request(ResourceServer,
245+
extract_scopes_using_scope_aliases(ResourceServer,
245246
extract_scopes_from_additional_scopes_key(ResourceServer,
246-
extract_scopes_from_scope_claim(Payload))),
247-
248-
Payload3 = case has_rich_auth_request_scopes(Payload2) of
249-
true -> extract_scopes_from_rich_auth_request(ResourceServer, Payload2);
250-
false -> Payload2
251-
end,
247+
extract_scopes_from_scope_claim(Payload)))),
252248

253249
FilteredScopes = filter_matching_scope_prefix_and_drop_it(
254-
get_scope(Payload3), ResourceServer#resource_server.scope_prefix),
255-
set_scope(FilteredScopes, Payload3).
256-
250+
get_scope(Payload1), ResourceServer#resource_server.scope_prefix),
251+
set_scope(FilteredScopes, Payload1).
257252

258253
-spec extract_scopes_using_scope_aliases(
259254
ResourceServer :: resource_server(), Payload :: map()) -> map().
260255
extract_scopes_using_scope_aliases(
261-
#resource_server{scope_aliases = ScopeAliasMapping} = ResourceServer, Payload)
256+
#resource_server{scope_aliases = ScopeAliasMapping}, Payload)
262257
when is_map(ScopeAliasMapping) ->
263258
Scopes0 = get_scope(Payload),
264259
Scopes = rabbit_data_coercion:to_list_of_binaries(Scopes0),

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_rar.erl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-include("oauth2.hrl").
1212
-import(uaa_jwt, [get_scope/1, set_scope/2]).
1313

14-
-export([extract_scopes_from_rich_auth_request/2, has_rich_auth_request_scopes/1]).
14+
-export([extract_scopes_from_rich_auth_request/2]).
1515

1616
-define(AUTHORIZATION_DETAILS_CLAIM, <<"authorization_details">>).
1717
-define(RAR_ACTIONS_FIELD, <<"actions">>).
@@ -44,15 +44,12 @@
4444
<<"management">>,
4545
<<"policymaker">> ]).
4646

47-
-spec has_rich_auth_request_scopes(Payload::map()) -> boolean().
48-
has_rich_auth_request_scopes(Payload) ->
49-
maps:is_key(?AUTHORIZATION_DETAILS_CLAIM, Payload).
50-
5147
-spec extract_scopes_from_rich_auth_request(ResourceServer :: resource_server(),
5248
Payload :: map()) -> map().
5349
%% https://oauth.net/2/rich-authorization-requests/
5450
extract_scopes_from_rich_auth_request(ResourceServer,
55-
#{?AUTHORIZATION_DETAILS_CLAIM := Permissions} = Payload) ->
51+
#{?AUTHORIZATION_DETAILS_CLAIM := Permissions} = Payload)
52+
when is_list(Permissions) ->
5653
ResourceServerType = ResourceServer#resource_server.resource_server_type,
5754

5855
FilteredPermissionsByType = lists:filter(fun(P) ->
@@ -61,7 +58,8 @@ extract_scopes_from_rich_auth_request(ResourceServer,
6158
ResourceServer#resource_server.id, FilteredPermissionsByType),
6259

6360
ExistingScopes = get_scope(Payload),
64-
set_scope(AdditionalScopes ++ ExistingScopes, Payload).
61+
set_scope(AdditionalScopes ++ ExistingScopes, Payload);
62+
extract_scopes_from_rich_auth_request(_, Payload) -> Payload.
6563

6664
put_location_attribute(Attribute, Map) ->
6765
put_attribute(binary:split(Attribute, <<":">>, [global, trim_all]), Map).

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_scope.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ parse_resource_pattern(Pattern, Permission) ->
9696
-spec filter_matching_scope_prefix_and_drop_it(list(), binary()|list()) -> list().
9797
filter_matching_scope_prefix_and_drop_it(Scopes, <<"">>) -> Scopes;
9898
filter_matching_scope_prefix_and_drop_it(Scopes, PrefixPattern) ->
99+
99100
PatternLength = byte_size(PrefixPattern),
100101
lists:filtermap(
101102
fun(ScopeEl) ->

0 commit comments

Comments
 (0)