Skip to content

Commit 9b4dfbe

Browse files
Use opaque signing key to validate incoming token
1 parent 2c9ddc7 commit 9b4dfbe

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
-export([get_access_token/2, get_expiration_time/1,
99
refresh_access_token/2,
1010
introspect_token/1,sign_token/1,
11+
get_opaque_token_signing_key/0,get_opaque_token_signing_key/1,
1112
get_oauth_provider/1, get_oauth_provider/2,
1213
get_openid_configuration/2,
1314
build_openid_discovery_endpoint/3,
@@ -51,7 +52,7 @@ refresh_access_token(OAuthProvider, Request) ->
5152
parse_access_token_response(Response).
5253

5354
-spec introspect_token(binary()) ->
54-
{ok, binary()} |
55+
{ok, map()} |
5556
{error, unsuccessful_access_token_response() | any()}.
5657
introspect_token(Token) ->
5758
case build_introspection_request() of
@@ -75,6 +76,7 @@ introspect_token(Token) ->
7576
{error, _} = Error -> Error
7677
end.
7778

79+
-spec sign_token(map()) -> {ok, binary()} | {error, any()}.
7880
sign_token(TokenPayload) ->
7981
case get_opaque_token_signing_key() of
8082
{error, _} = Error -> Error;
@@ -429,6 +431,15 @@ get_opaque_token_signing_key() ->
429431
parse_signing_key_configuration(Map)
430432
end.
431433

434+
-spec get_opaque_token_signing_key(string()|binary()) -> {ok, signing_key()} | {error, any()}.
435+
get_opaque_token_signing_key(KeyId) ->
436+
Map = get_env(opaque_token_signing_key),
437+
case maps:get(id, Map, undefined) of
438+
undefined -> {error, missing_opaque_token_signing_key};
439+
KeyId -> parse_signing_key_configuration(Map);
440+
_ -> {error, missing_opaque_token_signing_key}
441+
end.
442+
432443
parse_signing_key_configuration(Map) ->
433444
SK0 = case maps:get(id, Map, undefined) of
434445
undefined -> {error, missing_signing_key_id};

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_provider.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,15 @@ get_signing_keys(OauthProviderId) ->
146146
end.
147147

148148
get_signing_key(KeyId) ->
149-
maps:get(KeyId, get_signing_keys(root), undefined).
149+
case maps:get(KeyId, get_signing_keys(root), undefined) of
150+
undefined -> oauth2_client:get_opaque_signing_key(KeyId);
151+
V -> V
152+
end.
150153
get_signing_key(KeyId, OAuthProviderId) ->
151-
maps:get(KeyId, get_signing_keys(OAuthProviderId), undefined).
154+
case maps:get(KeyId, get_signing_keys(OAuthProviderId), undefined) of
155+
undefined -> oauth2_client:get_opaque_signing_key(KeyId);
156+
V -> V
157+
end.
152158

153159
-spec get_default_key(oauth_provider_id()) -> binary() | undefined.
154160
get_default_key(root) ->

deps/rabbitmq_management/src/rabbit_mgmt_wm_oauth_introspect.erl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ do_it(ReqData, Context) ->
4949
rabbit_log:error("Failed to introspect token due to ~p", [Reason]),
5050
rabbit_mgmt_util:not_authorised(Reason, ReqData, Context);
5151
{ok, JwtPayload} ->
52-
rabbit_log:debug("Got token payload : ~p", [JwtPayload]),
53-
case oauth2_client:issue_jwt_token(JwtPayload) of
52+
case oauth2_client:sign_token(JwtPayload) of
5453
{ok, JWT} ->
5554
rabbit_mgmt_util:reply(JWT, ReqData, Context);
5655
{error, Reason} ->

deps/rabbitmq_management/test/rabbit_mgmt_wm_auth_SUITE.erl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,11 @@ init_per_testcase(Testcase, Config) when Testcase =:= introspect_opaque_token_re
710710
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
711711
[rabbitmq_auth_backend_oauth2, key_config, [{cacertfile, CaCertFile}]]),
712712

713-
rabbit_ct_helpers:testcase_started(Config, Testcase).
714-
713+
rabbit_ct_helpers:testcase_started(Config, Testcase);
714+
715+
init_per_testcase(Testcase, Config) ->
716+
Config.
717+
715718
end_per_testcase(Testcase, Config) when Testcase =:= introspect_opaque_token_returns_active_jwt_token orelse
716719
Testcase =:= introspect_opaque_token_returns_inactive_jwt_token orelse
717720
Testcase =:= introspect_opaque_token_returns_401_from_auth_server ->
@@ -721,8 +724,11 @@ end_per_testcase(Testcase, Config) when Testcase =:= introspect_opaque_token_ret
721724
[rabbitmq_auth_backend_oauth2, introspection_client_id]),
722725
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
723726
[rabbitmq_auth_backend_oauth2, introspection_client_secret]),
727+
Config;
728+
729+
end_per_testcase(Testcase, Config) ->
724730
Config.
725-
731+
726732
start_broker(Config) ->
727733
Setup0 = rabbit_ct_broker_helpers:setup_steps(),
728734
Setup1 = rabbit_ct_client_helpers:setup_steps(),
@@ -949,9 +955,9 @@ should_return_mgt_oauth_resource_a_with_token_endpoint_params_1(Config) ->
949955
introspect_opaque_token_returns_active_jwt_token(Config) ->
950956
{ok, {{_HTTP, 200, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [
951957
{"authorization", "bearer active"}], []),
952-
JSON = rabbit_json:decode(rabbit_data_coercion:to_binary(ResBody)),
953-
?assertEqual(true, maps:get(<<"active">>, JSON)),
954-
?assertEqual("rabbitmq.tag:administrator", maps:get(<<"scope">>, JSON)).
958+
959+
Split = binary:split(rabbit_data_coercion:to_binary(ResBody), <<".">>),
960+
ct:log("split: ~p", [Split]).
955961

956962
introspect_opaque_token_returns_inactive_jwt_token(Config) ->
957963
{ok, {{_HTTP, 401, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [

0 commit comments

Comments
 (0)