Skip to content

Commit 7987c48

Browse files
MarcialRosalesmergify[bot]
authored andcommitted
Make scopes optional for oauth2 authentication
(cherry picked from commit 84e8d17) (cherry picked from commit f26358c)
1 parent a236bf7 commit 7987c48

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ post_process_payload_in_rich_auth_request_format(#{<<"authorization_details">> :
496496

497497

498498

499-
validate_payload(#{?SCOPE_JWT_FIELD := _Scope } = DecodedToken) ->
499+
validate_payload(DecodedToken) ->
500500
ResourceServerEnv = application:get_env(?APP, ?RESOURCE_SERVER_ID, <<>>),
501501
ResourceServerId = rabbit_data_coercion:to_binary(ResourceServerEnv),
502502
ScopePrefix = application:get_env(?APP, ?SCOPE_PREFIX, <<ResourceServerId/binary, ".">>),
@@ -507,6 +507,11 @@ validate_payload(#{?SCOPE_JWT_FIELD := Scope, ?AUD_JWT_FIELD := Aud} = DecodedTo
507507
ok -> {ok, DecodedToken#{?SCOPE_JWT_FIELD => filter_scopes(Scope, ScopePrefix)}};
508508
{error, Err} -> {refused, {invalid_aud, Err}}
509509
end;
510+
validate_payload(#{?AUD_JWT_FIELD := Aud} = DecodedToken, ResourceServerId, _ScopePrefix) ->
511+
case check_aud(Aud, ResourceServerId) of
512+
ok -> {ok, DecodedToken};
513+
{error, Err} -> {refused, {invalid_aud, Err}}
514+
end;
510515
validate_payload(#{?SCOPE_JWT_FIELD := Scope} = DecodedToken, _ResourceServerId, ScopePrefix) ->
511516
case application:get_env(?APP, ?VERIFY_AUD, true) of
512517
true -> {error, {badarg, {aud_field_is_missing}}};
@@ -534,7 +539,8 @@ check_aud(Aud, ResourceServerId) ->
534539

535540
%%--------------------------------------------------------------------
536541

537-
get_scopes(#{?SCOPE_JWT_FIELD := Scope}) -> Scope.
542+
get_scopes(#{?SCOPE_JWT_FIELD := Scope}) -> Scope;
543+
get_scopes(#{}) -> [].
538544

539545
-spec get_expanded_scopes(map(), #resource{}) -> [binary()].
540546
get_expanded_scopes(Token, #resource{virtual_host = VHost}) ->

deps/rabbitmq_auth_backend_oauth2/test/rabbit_auth_backend_oauth2_test_util.erl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ token_with_scopes_and_expiration(Scopes, Expiration) ->
8484
<<"aud">> => [<<"rabbitmq">>],
8585
<<"scope">> => Scopes}.
8686

87+
token_without_scopes() ->
88+
%% expiration is a timestamp with precision in seconds
89+
#{
90+
<<"kid">> => <<"token-key">>,
91+
<<"iss">> => <<"unit_test">>,
92+
<<"foo">> => <<"bar">>,
93+
<<"aud">> => [<<"rabbitmq">>]
94+
}.
95+
8796
fixture_token() ->
8897
fixture_token([]).
8998

deps/rabbitmq_auth_backend_oauth2/test/scope_SUITE.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ all() ->
2020
permission_resource,
2121
permission_topic
2222
].
23-
23+
2424
variable_expansion(_Config) ->
2525
Scenarios = [
2626
{ "Emtpy Scopes",

deps/rabbitmq_auth_backend_oauth2/test/unit_SUITE.erl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ all() ->
1919
test_validate_payload_resource_server_id_mismatch,
2020
test_validate_payload_with_scope_prefix,
2121
test_validate_payload,
22+
test_validate_payload_without_scope,
2223
test_validate_payload_when_verify_aud_false,
2324
test_successful_access_with_a_token,
25+
test_successful_authentication_without_scopes,
26+
test_successful_authorization_without_scopes,
27+
test_unsuccessful_access_without_scopes,
2428
test_successful_access_with_a_token_with_variables_in_scopes,
2529
test_successful_access_with_a_parsed_token,
2630
test_successful_access_with_a_token_that_has_tag_scopes,
@@ -609,6 +613,30 @@ post_process_payload_with_complex_claim_authorization(Authorization) ->
609613
{true, Payload} = uaa_jwt_jwt:decode_and_verify(Jwk, EncodedToken),
610614
rabbit_auth_backend_oauth2:post_process_payload(Payload).
611615

616+
test_successful_authentication_without_scopes(_) ->
617+
Jwk = ?UTIL_MOD:fixture_jwk(),
618+
UaaEnv = [{signing_keys, #{<<"token-key">> => {map, Jwk}}}],
619+
application:set_env(rabbitmq_auth_backend_oauth2, key_config, UaaEnv),
620+
application:set_env(rabbitmq_auth_backend_oauth2, resource_server_id, <<"rabbitmq">>),
621+
622+
Username = <<"username">>,
623+
Token = ?UTIL_MOD:sign_token_hs(?UTIL_MOD:token_with_sub(?UTIL_MOD:fixture_token(), Username), Jwk),
624+
625+
{ok, #auth_user{username = Username} } =
626+
rabbit_auth_backend_oauth2:user_login_authentication(Username, [{password, Token}]).
627+
628+
test_successful_authorization_without_scopes(_) ->
629+
Jwk = ?UTIL_MOD:fixture_jwk(),
630+
UaaEnv = [{signing_keys, #{<<"token-key">> => {map, Jwk}}}],
631+
application:set_env(rabbitmq_auth_backend_oauth2, key_config, UaaEnv),
632+
application:set_env(rabbitmq_auth_backend_oauth2, resource_server_id, <<"rabbitmq">>),
633+
634+
Username = <<"username">>,
635+
Token = ?UTIL_MOD:sign_token_hs(?UTIL_MOD:token_with_sub(?UTIL_MOD:fixture_token(), Username), Jwk),
636+
637+
{ok, _ } =
638+
rabbit_auth_backend_oauth2:user_login_authorization(Username, [{password, Token}]).
639+
612640
test_successful_access_with_a_token(_) ->
613641
%% Generate a token with JOSE
614642
%% Check authorization with the token
@@ -980,6 +1008,21 @@ test_unsuccessful_access_with_a_bogus_token(_) ->
9801008
?assertMatch({refused, _, _},
9811009
rabbit_auth_backend_oauth2:user_login_authentication(Username, [{password, <<"not a token">>}])).
9821010

1011+
test_unsuccessful_access_without_scopes(_) ->
1012+
Username = <<"username">>,
1013+
application:set_env(rabbitmq_auth_backend_oauth2, resource_server_id, <<"rabbitmq">>),
1014+
1015+
Jwk = ?UTIL_MOD:fixture_jwk(),
1016+
Token = ?UTIL_MOD:sign_token_hs(?UTIL_MOD:token_with_sub(?UTIL_MOD:token_without_scopes(), Username), Jwk),
1017+
UaaEnv = [{signing_keys, #{<<"token-key">> => {map, Jwk}}}],
1018+
application:set_env(rabbitmq_auth_backend_oauth2, key_config, UaaEnv),
1019+
1020+
{ok, #auth_user{username = Username, tags = [], impl = CredentialsFun } = AuthUser} =
1021+
rabbit_auth_backend_oauth2:user_login_authentication(Username, [{password, Token}]),
1022+
1023+
ct:log("authuser ~p ~p ", [AuthUser, CredentialsFun()]),
1024+
assert_vhost_access_denied(AuthUser, <<"vhost">>).
1025+
9831026
test_restricted_vhost_access_with_a_valid_token(_) ->
9841027
Username = <<"username">>,
9851028
application:set_env(rabbitmq_auth_backend_oauth2, resource_server_id, <<"rabbitmq">>),
@@ -1277,6 +1320,12 @@ test_validate_payload(_) ->
12771320
<<"scope">> => [<<"bar">>, <<"other.third">>]}},
12781321
rabbit_auth_backend_oauth2:validate_payload(KnownResourceServerId, ?RESOURCE_SERVER_ID, ?DEFAULT_SCOPE_PREFIX)).
12791322

1323+
test_validate_payload_without_scope(_) ->
1324+
KnownResourceServerId = #{<<"aud">> => [?RESOURCE_SERVER_ID]
1325+
},
1326+
?assertEqual({ok, #{<<"aud">> => [?RESOURCE_SERVER_ID] }},
1327+
rabbit_auth_backend_oauth2:validate_payload(KnownResourceServerId, ?RESOURCE_SERVER_ID, ?DEFAULT_SCOPE_PREFIX)).
1328+
12801329
test_validate_payload_when_verify_aud_false(_) ->
12811330
WithoutAud = #{
12821331
<<"scope">> => [<<"foo">>, <<"rabbitmq.bar">>,

0 commit comments

Comments
 (0)