Skip to content

Commit 4c98e21

Browse files
Make end_session_endpoint configurable
1 parent 7d64d3d commit 4c98e21

File tree

9 files changed

+254
-82
lines changed

9 files changed

+254
-82
lines changed

deps/oauth2_client/include/oauth2_client.hrl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
-define(RESPONSE_ISSUER, <<"issuer">>).
4242
-define(RESPONSE_TOKEN_ENDPOINT, <<"token_endpoint">>).
4343
-define(RESPONSE_AUTHORIZATION_ENDPOINT, <<"authorization_endpoint">>).
44+
-define(RESPONSE_END_SESSION_ENDPOINT, <<"end_session_endpoint">>).
4445
-define(RESPONSE_JWKS_URI, <<"jwks_uri">>).
4546
-define(RESPONSE_TLS_OPTIONS, <<"ssl_options">>).
4647

@@ -51,6 +52,7 @@
5152
issuer :: option(uri_string:uri_string()),
5253
token_endpoint :: option(uri_string:uri_string()),
5354
authorization_endpoint :: option(uri_string:uri_string()),
55+
end_session_endpoint :: option(uri_string:uri_string()),
5456
jwks_uri :: option(uri_string:uri_string()),
5557
ssl_options :: option(list())
5658
}).

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-module(oauth2_client).
88
-export([get_access_token/2, get_expiration_time/1,
99
refresh_access_token/2,
10-
get_oauth_provider/1, get_oauth_provider/2,
10+
get_oauth_provider/1, get_oauth_provider/2,
1111
extract_ssl_options_as_list/1
1212
]).
1313

@@ -104,14 +104,20 @@ do_update_oauth_provider_endpoints_configuration(OAuthProvider) ->
104104
case OAuthProvider#oauth_provider.token_endpoint of
105105
undefined ->
106106
do_nothing;
107-
TokenEndPoint ->
108-
application:set_env(rabbitmq_auth_backend_oauth2, token_endpoint, TokenEndPoint)
107+
TokenEndpoint ->
108+
application:set_env(rabbitmq_auth_backend_oauth2, token_endpoint, TokenEndpoint)
109109
end,
110110
case OAuthProvider#oauth_provider.authorization_endpoint of
111111
undefined ->
112112
do_nothing;
113-
AuthzEndPoint ->
114-
application:set_env(rabbitmq_auth_backend_oauth2, authorization_endpoint, AuthzEndPoint)
113+
AuthzEndpoint ->
114+
application:set_env(rabbitmq_auth_backend_oauth2, authorization_endpoint, AuthzEndpoint)
115+
end,
116+
case OAuthProvider#oauth_provider.end_session_endpoint of
117+
undefined ->
118+
do_nothing;
119+
EndSessionEndpoint ->
120+
application:set_env(rabbitmq_auth_backend_oauth2, end_session_endpoint, EndSessionEndpoint)
115121
end,
116122
List = application:get_env(rabbitmq_auth_backend_oauth2, key_config, []),
117123
ModifiedList = case OAuthProvider#oauth_provider.jwks_uri of
@@ -127,17 +133,21 @@ do_update_oauth_provider_endpoints_configuration(OAuthProviderId, OAuthProvider)
127133
LookupProviderPropList = maps:get(OAuthProviderId, OAuthProviders),
128134
ModifiedList0 = case OAuthProvider#oauth_provider.token_endpoint of
129135
undefined -> LookupProviderPropList;
130-
TokenEndPoint -> [{token_endpoint, TokenEndPoint} | LookupProviderPropList]
136+
TokenEndpoint -> [{token_endpoint, TokenEndpoint} | LookupProviderPropList]
131137
end,
132138
ModifiedList1 = case OAuthProvider#oauth_provider.authorization_endpoint of
133139
undefined -> ModifiedList0;
134-
AuthzEndPoint -> [{authorization_endpoint, AuthzEndPoint} | ModifiedList0]
140+
AuthzEndpoint -> [{authorization_endpoint, AuthzEndpoint} | ModifiedList0]
135141
end,
136-
ModifiedList2 = case OAuthProvider#oauth_provider.jwks_uri of
142+
ModifiedList2 = case OAuthProvider#oauth_provider.end_session_endpoint of
137143
undefined -> ModifiedList1;
138-
JwksEndPoint -> [{jwks_uri, JwksEndPoint} | ModifiedList1]
144+
EndSessionEndpoint -> [{end_session_endpoint, EndSessionEndpoint} | ModifiedList1]
139145
end,
140-
ModifiedOAuthProviders = maps:put(OAuthProviderId, ModifiedList2, OAuthProviders),
146+
ModifiedList3 = case OAuthProvider#oauth_provider.jwks_uri of
147+
undefined -> ModifiedList2;
148+
JwksEndPoint -> [{jwks_uri, JwksEndPoint} | ModifiedList2]
149+
end,
150+
ModifiedOAuthProviders = maps:put(OAuthProviderId, ModifiedList3, OAuthProviders),
141151
application:set_env(rabbitmq_auth_backend_oauth2, oauth_providers, ModifiedOAuthProviders),
142152
rabbit_log:debug("Replacing oauth_providers ~p", [ ModifiedOAuthProviders]),
143153
OAuthProvider.
@@ -179,7 +189,7 @@ get_oauth_provider(ListOfRequiredAttributes) ->
179189
{ok, DefaultOauthProvider} ->
180190
rabbit_log:debug("Using default_oauth_provider ~p", [DefaultOauthProvider]),
181191
get_oauth_provider(DefaultOauthProvider, ListOfRequiredAttributes)
182-
end.
192+
end.
183193

184194
get_oauth_provider_from_keyconfig(ListOfRequiredAttributes) ->
185195
OAuthProvider = lookup_oauth_provider_from_keyconfig(),
@@ -206,7 +216,7 @@ get_oauth_provider_from_keyconfig(ListOfRequiredAttributes) ->
206216
{ok, OAuthProvider2};
207217
_ = Attrs->
208218
{error, {missing_oauth_provider_attributes, Attrs}}
209-
end;
219+
end;
210220
{error, _} = Error3 -> Error3
211221
end
212222
end.
@@ -253,7 +263,7 @@ get_oauth_provider(OAuth2ProviderId, ListOfRequiredAttributes) when is_binary(OA
253263
{ok, OAuthProvider2};
254264
_ = Attrs->
255265
{error, {missing_oauth_provider_attributes, Attrs}}
256-
end;
266+
end;
257267
{error, _} = Error3 -> Error3
258268
end
259269
end
@@ -285,11 +295,15 @@ find_missing_attributes(#oauth_provider{} = OAuthProvider, RequiredAttributes) -
285295
lookup_oauth_provider_from_keyconfig() ->
286296
Issuer = application:get_env(rabbitmq_auth_backend_oauth2, issuer, undefined),
287297
TokenEndpoint = application:get_env(rabbitmq_auth_backend_oauth2, token_endpoint, undefined),
298+
AuthorizationEndpoint = application:get_env(rabbitmq_auth_backend_oauth2, authorization_endpoint, undefined),
299+
EndSessionEndpoint = application:get_env(rabbitmq_auth_backend_oauth2, end_session_endpoint, undefined),
288300
Map = maps:from_list(application:get_env(rabbitmq_auth_backend_oauth2, key_config, [])),
289301
#oauth_provider{
290302
issuer = Issuer,
291303
jwks_uri = maps:get(jwks_url, Map, undefined), %% jwks_url not uri . _url is the legacy name
292304
token_endpoint = TokenEndpoint,
305+
authorization_endpoint = AuthorizationEndpoint,
306+
end_session_endpoint = EndSessionEndpoint,
293307
ssl_options = extract_ssl_options_as_list(Map)
294308
}.
295309

@@ -447,6 +461,7 @@ map_to_oauth_provider(Map) when is_map(Map) ->
447461
issuer = maps:get(?RESPONSE_ISSUER, Map),
448462
token_endpoint = maps:get(?RESPONSE_TOKEN_ENDPOINT, Map, undefined),
449463
authorization_endpoint = maps:get(?RESPONSE_AUTHORIZATION_ENDPOINT, Map, undefined),
464+
end_session_endpoint = maps:get(?RESPONSE_END_SESSION_ENDPOINT, Map, undefined),
450465
jwks_uri = maps:get(?RESPONSE_JWKS_URI, Map, undefined)
451466
};
452467

@@ -455,6 +470,7 @@ map_to_oauth_provider(PropList) when is_list(PropList) ->
455470
issuer = proplists:get_value(issuer, PropList),
456471
token_endpoint = proplists:get_value(token_endpoint, PropList),
457472
authorization_endpoint = proplists:get_value(authorization_endpoint, PropList, undefined),
473+
end_session_endpoint = proplists:get_value(end_session_endpoint, PropList, undefined),
458474
jwks_uri = proplists:get_value(jwks_uri, PropList, undefined),
459475
ssl_options = extract_ssl_options_as_list(maps:from_list(proplists:get_value(https, PropList, [])))
460476
}.

deps/oauth2_client/test/system_SUITE.erl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
{issuer, build_issuer("http") },
101101
{authorization_endpoint, <<"http://localhost:8000/authorize">>},
102102
{token_endpoint, build_token_endpoint_uri("http")},
103+
{end_session_endpoint, <<"http://localhost:8000/logout">>},
103104
{jwks_uri, build_jwks_uri("http")}
104105
]}
105106
]
@@ -117,6 +118,7 @@
117118
{issuer, build_issuer("https") },
118119
{authorization_endpoint, <<"https://localhost:8000/authorize">>},
119120
{token_endpoint, build_token_endpoint_uri("https")},
121+
{end_session_endpoint, <<"http://localhost:8000/logout">>},
120122
{jwks_uri, build_jwks_uri("https")}
121123
]}
122124
]
@@ -262,6 +264,8 @@ configure_all_oauth_provider_settings(Config) ->
262264
application:set_env(rabbitmq_auth_backend_oauth2, issuer, OAuthProvider#oauth_provider.issuer),
263265
application:set_env(rabbitmq_auth_backend_oauth2, oauth_providers, OAuthProviders),
264266
application:set_env(rabbitmq_auth_backend_oauth2, token_endpoint, OAuthProvider#oauth_provider.token_endpoint),
267+
application:set_env(rabbitmq_auth_backend_oauth2, end_sessione_endpoint, OAuthProvider#oauth_provider.end_session_endpoint),
268+
application:set_env(rabbitmq_auth_backend_oauth2, authorization_endpoint, OAuthProvider#oauth_provider.authorization_endpoint),
265269
KeyConfig = [ { jwks_url, OAuthProvider#oauth_provider.jwks_uri } ] ++
266270
case OAuthProvider#oauth_provider.ssl_options of
267271
undefined ->
@@ -313,6 +317,8 @@ end_per_testcase(_, Config) ->
313317
application:unset_env(rabbitmq_auth_backend_oauth2, oauth_providers),
314318
application:unset_env(rabbitmq_auth_backend_oauth2, issuer),
315319
application:unset_env(rabbitmq_auth_backend_oauth2, token_endpoint),
320+
application:unset_env(rabbitmq_auth_backend_oauth2, authorization_endpoint),
321+
application:unset_env(rabbitmq_auth_backend_oauth2, end_session_endpoint),
316322
application:unset_env(rabbitmq_auth_backend_oauth2, key_config),
317323
case ?config(group, Config) of
318324
http_up ->
@@ -417,11 +423,19 @@ get_oauth_provider_given_oauth_provider_id(Config) ->
417423
= lookup_expectation(get_openid_configuration, Config),
418424

419425
ct:log("get_oauth_provider ~p", [?config(oauth_provider_id, Config)]),
420-
{ok, #oauth_provider{issuer = Issuer, token_endpoint = TokenEndPoint, jwks_uri = Jwks_uri}} =
421-
oauth2_client:get_oauth_provider(?config(oauth_provider_id, Config), [issuer, token_endpoint, jwks_uri]),
426+
{ok, #oauth_provider{
427+
issuer = Issuer,
428+
token_endpoint = TokenEndPoint,
429+
authorization_endpoint = AuthorizationEndpoint,
430+
end_session_endpoint = EndSessionEndpoint,
431+
jwks_uri = Jwks_uri}} =
432+
oauth2_client:get_oauth_provider(?config(oauth_provider_id, Config),
433+
[issuer, token_endpoint, jwks_uri, authorization_endpoint, end_session_endpoint]),
422434

423435
?assertEqual(proplists:get_value(issuer, JsonPayload), Issuer),
424436
?assertEqual(proplists:get_value(token_endpoint, JsonPayload), TokenEndPoint),
437+
?assertEqual(proplists:get_value(authorization_endpoint, JsonPayload), AuthorizationEndpoint),
438+
?assertEqual(proplists:get_value(end_session_endpoint, JsonPayload), EndSessionEndpoint),
425439
?assertEqual(proplists:get_value(jwks_uri, JsonPayload), Jwks_uri).
426440

427441

deps/rabbitmq_auth_backend_oauth2/priv/schema/rabbitmq_auth_backend_oauth2.schema

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@
163163
"rabbitmq_auth_backend_oauth2.key_config.jwks_url",
164164
[{datatype, string}, {validators, ["uri", "https_uri"]}]}.
165165

166+
{mapping,
167+
"auth_oauth2.end_session_endpoint",
168+
"rabbitmq_auth_backend_oauth2.end_session_endpoint",
169+
[{datatype, string}, {validators, ["uri", "https_uri"]}]}.
170+
171+
{mapping,
172+
"auth_oauth2.authorization_endpoint",
173+
"rabbitmq_auth_backend_oauth2.authorization_endpoint",
174+
[{datatype, string}, {validators, ["uri", "https_uri"]}]}.
175+
166176
{mapping,
167177
"auth_oauth2.https.peer_verification",
168178
"rabbitmq_auth_backend_oauth2.key_config.peer_verification",
@@ -240,6 +250,16 @@
240250
[{datatype, string}, {validators, ["uri", "https_uri"]}]
241251
}.
242252

253+
{mapping,
254+
"auth_oauth2.oauth_providers.$name.end_session_endpoint",
255+
"rabbitmq_auth_backend_oauth2.oauth_providers",
256+
[{datatype, string}, {validators, ["uri", "https_uri"]}]}.
257+
258+
{mapping,
259+
"auth_oauth2.oauth_providers.$name.authorization_endpoint",
260+
"rabbitmq_auth_backend_oauth2.oauth_providers",
261+
[{datatype, string}, {validators, ["uri", "https_uri"]}]}.
262+
243263
{mapping,
244264
"auth_oauth2.oauth_providers.$name.https.verify",
245265
"rabbitmq_auth_backend_oauth2.oauth_providers",

deps/rabbitmq_auth_backend_oauth2/test/config_schema_SUITE_data/rabbitmq_auth_backend_oauth2.snippets

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@
129129
auth_oauth2.oauth_providers.uaa.issuer = https://uaa
130130
auth_oauth2.oauth_providers.keycloak.token_endpoint = https://keycloak/token
131131
auth_oauth2.oauth_providers.keycloak.jwks_uri = https://keycloak/keys
132+
auth_oauth2.oauth_providers.keycloak.authorization_endpoint = https://keycloak/authorize
133+
auth_oauth2.oauth_providers.keycloak.end_session_endpoint = https://keycloak/logout
132134
auth_oauth2.oauth_providers.keycloak.https.cacertfile = /mnt/certs/ca_certificate.pem
133135
auth_oauth2.oauth_providers.keycloak.https.verify = verify_none",
134136
[
@@ -149,8 +151,10 @@
149151
{verify, verify_none},
150152
{cacertfile, "/mnt/certs/ca_certificate.pem"}
151153
]},
154+
{end_session_endpoint, <<"https://keycloak/logout">>},
155+
{authorization_endpoint, <<"https://keycloak/authorize">>},
152156
{token_endpoint, <<"https://keycloak/token">>},
153-
{jwks_uri, <<"https://keycloak/keys">>}
157+
{jwks_uri, <<"https://keycloak/keys">>}
154158
]
155159

156160
}

deps/rabbitmq_management/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ define PROJECT_APP_EXTRA_KEYS
2222
endef
2323

2424
DEPS = rabbit_common rabbit amqp_client cowboy cowlib rabbitmq_web_dispatch rabbitmq_management_agent oauth2_client
25-
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers proper
25+
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers proper amqp10_client
2626
LOCAL_DEPS += ranch ssl crypto public_key
2727

2828
# FIXME: Add Ranch as a BUILD_DEPS to be sure the correct version is picked.

deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ function oauth_initialize_user_manager(resource_server) {
103103
audience: resource_server.id, // required by oauth0
104104
},
105105
};
106+
if (resource_server.end_session_endpoint != "") {
107+
oidcSettings.metadataSeed = {
108+
end_session_endpoint: resource_server.end_session_endpoint
109+
}
110+
}
106111
if (resource_server.oauth_client_secret != "") {
107112
oidcSettings.client_secret = resource_server.oauth_client_secret;
108113
}

0 commit comments

Comments
 (0)