1212
1313-include_lib (" oauth2_client.hrl" ).
1414-import (oauth2_client , [
15- build_openid_discovery_endpoint /3 ]).
15+ build_openid_discovery_endpoint /3
16+ ]).
1617
1718-compile (export_all ).
1819
@@ -35,10 +36,12 @@ groups() ->
3536[
3637
3738 {with_all_oauth_provider_settings , [], [
38- {group , verify_get_oauth_provider }
39+ {group , verify_get_oauth_provider },
40+ jwks_uri_takes_precedence_over_jwks_url ,
41+ jwks_url_is_used_in_absense_of_jwks_uri
3942 ]},
4043 {without_all_oauth_providers_settings , [], [
41- {group , verify_get_oauth_provider }
44+ {group , verify_get_oauth_provider }
4245 ]},
4346 {verify_openid_configuration , [], [
4447 get_openid_configuration ,
@@ -57,7 +60,7 @@ groups() ->
5760 expiration_time_in_token
5861 ]},
5962 {verify_get_oauth_provider , [], [
60- get_oauth_provider ,
63+ get_oauth_provider ,
6164 {with_default_oauth_provider , [], [
6265 get_oauth_provider
6366 ]},
@@ -78,10 +81,16 @@ groups() ->
7881
7982init_per_suite (Config ) ->
8083 [
81- {denies_access_token , [ {token_endpoint , denies_access_token_expectation ()} ]},
82- {auth_server_error , [ {token_endpoint , auth_server_error_when_access_token_request_expectation ()} ]},
83- {non_json_payload , [ {token_endpoint , non_json_payload_when_access_token_request_expectation ()} ]},
84- {grants_refresh_token , [ {token_endpoint , grants_refresh_token_expectation ()} ]}
84+ {jwks_url , build_jwks_uri (" https" , " /certs4url" )},
85+ {jwks_uri , build_jwks_uri (" https" )},
86+ {denies_access_token , [
87+ {token_endpoint , denies_access_token_expectation ()} ]},
88+ {auth_server_error , [
89+ {token_endpoint , auth_server_error_when_access_token_request_expectation ()} ]},
90+ {non_json_payload , [
91+ {token_endpoint , non_json_payload_when_access_token_request_expectation ()} ]},
92+ {grants_refresh_token , [
93+ {token_endpoint , grants_refresh_token_expectation ()} ]}
8594 | Config ].
8695
8796end_per_suite (Config ) ->
@@ -95,7 +104,7 @@ init_per_group(https, Config) ->
95104 CertsDir = ? config (rmq_certsdir , Config0 ),
96105 CaCertFile = filename :join ([CertsDir , " testca" , " cacert.pem" ]),
97106 WrongCaCertFile = filename :join ([CertsDir , " server" , " server.pem" ]),
98- [{group , https },
107+ [{group , https },
99108 {oauth_provider_id , <<" uaa" >>},
100109 {oauth_provider , build_https_oauth_provider (<<" uaa" >>, CaCertFile )},
101110 {oauth_provider_with_issuer , keep_only_issuer_and_ssl_options (
@@ -198,21 +207,38 @@ configure_all_oauth_provider_settings(Config) ->
198207 OAuthProvider # oauth_provider .end_session_endpoint ),
199208 application :set_env (rabbitmq_auth_backend_oauth2 , authorization_endpoint ,
200209 OAuthProvider # oauth_provider .authorization_endpoint ),
201- KeyConfig = [ { jwks_url , OAuthProvider # oauth_provider . jwks_uri } ] ++
210+ KeyConfig0 =
202211 case OAuthProvider # oauth_provider .ssl_options of
203212 undefined ->
204213 [];
205214 _ ->
206215 [ {peer_verification , proplists :get_value (verify ,
207216 OAuthProvider # oauth_provider .ssl_options ) },
208- {cacertfile , proplists :get_value (cacertfile ,
217+ {cacertfile , proplists :get_value (cacertfile ,
209218 OAuthProvider # oauth_provider .ssl_options ) }
210219 ]
211220 end ,
221+ KeyConfig =
222+ case ? config (jwks_uri_type_of_config , Config ) of
223+ undefined ->
224+ application :set_env (rabbitmq_auth_backend_oauth2 , jwks_uri ,
225+ OAuthProvider # oauth_provider .jwks_uri ),
226+ KeyConfig0 ;
227+ only_jwks_uri ->
228+ application :set_env (rabbitmq_auth_backend_oauth2 , jwks_uri ,
229+ OAuthProvider # oauth_provider .jwks_uri ),
230+ KeyConfig0 ;
231+ only_jwks_url ->
232+ [ { jwks_url , ? config (jwks_url , Config ) } | KeyConfig0 ];
233+ both ->
234+ application :set_env (rabbitmq_auth_backend_oauth2 , jwks_uri ,
235+ OAuthProvider # oauth_provider .jwks_uri ),
236+ [ { jwks_url , ? config (jwks_url , Config ) } | KeyConfig0 ]
237+ end ,
212238 application :set_env (rabbitmq_auth_backend_oauth2 , key_config , KeyConfig ).
213239
214240configure_minimum_oauth_provider_settings (Config ) ->
215- OAuthProvider = ? config (oauth_provider_with_issuer , Config ),
241+ OAuthProvider = ? config (oauth_provider , Config ),
216242 OAuthProviders = #{ ? config (oauth_provider_id , Config ) =>
217243 oauth_provider_to_proplist (OAuthProvider ) },
218244 application :set_env (rabbitmq_auth_backend_oauth2 , oauth_providers ,
@@ -232,9 +258,18 @@ configure_minimum_oauth_provider_settings(Config) ->
232258 end ,
233259 application :set_env (rabbitmq_auth_backend_oauth2 , key_config , KeyConfig ).
234260
235- init_per_testcase (TestCase , Config ) ->
261+ init_per_testcase (TestCase , Config0 ) ->
236262 application :set_env (rabbitmq_auth_backend_oauth2 , use_global_locks , false ),
237263
264+ Config = [case TestCase of
265+ jwks_url_is_used_in_absense_of_jwks_uri ->
266+ {jwks_uri_type_of_config , only_jwks_url };
267+ jwks_uri_takes_precedence_over_jwks_url ->
268+ {jwks_uri_type_of_config , both };
269+ _ ->
270+ {jwks_uri_type_of_config , only_jwks_uri }
271+ end | Config0 ],
272+
238273 case ? config (with_all_oauth_provider_settings , Config ) of
239274 false -> configure_minimum_oauth_provider_settings (Config );
240275 true -> configure_all_oauth_provider_settings (Config );
@@ -248,6 +283,9 @@ init_per_testcase(TestCase, Config) ->
248283 https ->
249284 start_https_oauth_server (? AUTH_PORT , ? config (rmq_certsdir , Config ),
250285 ListOfExpectations );
286+ without_all_oauth_providers_settings ->
287+ start_https_oauth_server (? AUTH_PORT , ? config (rmq_certsdir , Config ),
288+ ListOfExpectations );
251289 _ ->
252290 do_nothing
253291 end ,
@@ -256,13 +294,16 @@ init_per_testcase(TestCase, Config) ->
256294end_per_testcase (_ , Config ) ->
257295 application :unset_env (rabbitmq_auth_backend_oauth2 , oauth_providers ),
258296 application :unset_env (rabbitmq_auth_backend_oauth2 , issuer ),
297+ application :unset_env (rabbitmq_auth_backend_oauth2 , jwks_uri ),
259298 application :unset_env (rabbitmq_auth_backend_oauth2 , token_endpoint ),
260299 application :unset_env (rabbitmq_auth_backend_oauth2 , authorization_endpoint ),
261300 application :unset_env (rabbitmq_auth_backend_oauth2 , end_session_endpoint ),
262301 application :unset_env (rabbitmq_auth_backend_oauth2 , key_config ),
263302 case ? config (group , Config ) of
264303 https ->
265304 stop_https_auth_server ();
305+ without_all_oauth_providers_settings ->
306+ stop_https_auth_server ();
266307 _ ->
267308 do_nothing
268309 end ,
@@ -466,16 +507,15 @@ ssl_connection_error(Config) ->
466507 {error , {failed_connect , _ } } = oauth2_client :get_access_token (
467508 ? config (oauth_provider_with_wrong_ca , Config ), build_access_token_request (Parameters )).
468509
469- verify_get_oauth_provider_returns_oauth_provider_from_key_config () ->
510+ verify_get_oauth_provider_returns_root_oauth_provider () ->
470511 {ok , # oauth_provider {id = Id ,
471512 issuer = Issuer ,
472513 token_endpoint = TokenEndPoint ,
473514 jwks_uri = Jwks_uri }} =
474515 oauth2_client :get_oauth_provider ([issuer , token_endpoint , jwks_uri ]),
475- ExpectedIssuer = application :get_env (rabbitmq_auth_backend_oauth2 , issuer , undefined ),
476- ExpectedTokenEndPoint = application :get_env (rabbitmq_auth_backend_oauth2 , token_endpoint , undefined ),
477- ExpectedJwks_uri = proplists :get_value (jwks_url ,
478- application :get_env (rabbitmq_auth_backend_oauth2 , key_config , [])),
516+ ExpectedIssuer = get_env (issuer ),
517+ ExpectedTokenEndPoint = get_env (token_endpoint ),
518+ ExpectedJwks_uri = get_env (jwks_uri ),
479519 ? assertEqual (root , Id ),
480520 ? assertEqual (ExpectedIssuer , Issuer ),
481521 ? assertEqual (ExpectedTokenEndPoint , TokenEndPoint ),
@@ -492,9 +532,9 @@ verify_get_oauth_provider_returns_default_oauth_provider(DefaultOAuthProviderId)
492532get_oauth_provider (Config ) ->
493533 case ? config (with_all_oauth_provider_settings , Config ) of
494534 true ->
495- case application : get_env (rabbitmq_auth_backend_oauth2 , default_oauth_provider , undefined ) of
535+ case get_env (default_oauth_provider ) of
496536 undefined ->
497- verify_get_oauth_provider_returns_oauth_provider_from_key_config ();
537+ verify_get_oauth_provider_returns_root_oauth_provider ();
498538 DefaultOAuthProviderId ->
499539 verify_get_oauth_provider_returns_default_oauth_provider (DefaultOAuthProviderId )
500540 end ;
@@ -525,8 +565,7 @@ get_oauth_provider_given_oauth_provider_id(Config) ->
525565 [issuer , token_endpoint , jwks_uri , authorization_endpoint ,
526566 end_session_endpoint ]),
527567
528- OAuthProviders = application :get_env (rabbitmq_auth_backend_oauth2 ,
529- oauth_providers , #{}),
568+ OAuthProviders = get_env (oauth_providers , #{}),
530569 ExpectedProvider = maps :get (Id , OAuthProviders , []),
531570 ? assertEqual (proplists :get_value (issuer , ExpectedProvider ),
532571 Issuer ),
@@ -564,6 +603,17 @@ get_oauth_provider_given_oauth_provider_id(Config) ->
564603 Jwks_uri )
565604 end .
566605
606+ jwks_url_is_used_in_absense_of_jwks_uri (Config ) ->
607+ {ok , # oauth_provider {
608+ jwks_uri = Jwks_uri }} = oauth2_client :get_oauth_provider ([jwks_uri ]),
609+ ? assertEqual (
610+ proplists :get_value (jwks_url , get_env (key_config , []), undefined ),
611+ Jwks_uri ).
612+
613+ jwks_uri_takes_precedence_over_jwks_url (Config ) ->
614+ {ok , # oauth_provider {
615+ jwks_uri = Jwks_uri }} = oauth2_client :get_oauth_provider ([jwks_uri ]),
616+ ? assertEqual (get_env (jwks_uri ), Jwks_uri ).
567617
568618
569619% %% HELPERS
@@ -584,10 +634,13 @@ build_token_endpoint_uri(Scheme) ->
584634 path => " /token" }).
585635
586636build_jwks_uri (Scheme ) ->
637+ build_jwks_uri (Scheme , " /certs" ).
638+
639+ build_jwks_uri (Scheme , Path ) ->
587640 uri_string :recompose (#{scheme => Scheme ,
588641 host => " localhost" ,
589642 port => rabbit_data_coercion :to_integer (? AUTH_PORT ),
590- path => " /certs " }).
643+ path => Path }).
591644
592645build_access_token_request (Request ) ->
593646 # access_token_request {
@@ -623,11 +676,11 @@ oauth_provider_to_proplist(#oauth_provider{
623676 authorization_endpoint = AuthorizationEndpoint ,
624677 ssl_options = SslOptions ,
625678 jwks_uri = Jwks_uri }) ->
626- [ { issuer , Issuer },
679+ [ { issuer , Issuer },
627680 {token_endpoint , TokenEndpoint },
628681 {end_session_endpoint , EndSessionEndpoint },
629682 {authorization_endpoint , AuthorizationEndpoint },
630- { https ,
683+ {https ,
631684 case SslOptions of
632685 undefined -> [];
633686 Value -> Value
@@ -677,6 +730,11 @@ token(ExpiresIn) ->
677730 EncodedToken .
678731
679732
733+ get_env (Par ) ->
734+ application :get_env (rabbitmq_auth_backend_oauth2 , Par , undefined ).
735+ get_env (Par , Default ) ->
736+ application :get_env (rabbitmq_auth_backend_oauth2 , Par , Default ).
737+
680738
681739build_http_mock_behaviour (Request , Response ) ->
682740 #{request => Request , response => Response }.
0 commit comments