1- % % This Source Code Form is subject to the terms of the Mozilla Public
21% % License, v. 2.0. If a copy of the MPL was not distributed with this
32% % file, You can obtain one at https://mozilla.org/MPL/2.0/.
43% %
1716
1817-compile (export_all ).
1918
19+ -define (MOCK_OPAQUE_TOKEN , <<" some opaque token" >>).
20+ -define (MOCK_INTROSPECTION_ENDPOINT , <<" /introspection" >>).
2021-define (MOCK_TOKEN_ENDPOINT , <<" /token" >>).
2122-define (AUTH_PORT , 8000 ).
2223-define (ISSUER_PATH , " /somepath" ).
@@ -28,7 +29,8 @@ all() ->
2829[
2930 {group , https_down },
3031 {group , https },
31- {group , with_all_oauth_provider_settings }
32+ {group , with_all_oauth_provider_settings },
33+ {group , verify_introspect_token }
3234
3335].
3436
@@ -40,6 +42,20 @@ groups() ->
4042 jwks_uri_takes_precedence_over_jwks_url ,
4143 jwks_url_is_used_in_absense_of_jwks_uri
4244 ]},
45+ {verify_introspect_token , [], [
46+ {with_all_oauth_provider_settings , [], [
47+ cannot_introspect_due_to_missing_configuration ,
48+ {with_introspection_endpoint , [], [
49+ cannot_introspect_due_to_missing_configuration ,
50+ {with_introspection_basic_client_credentials , [], [
51+ can_introspect_token
52+ ]},
53+ {with_introspection_request_param_client_credentials , [], [
54+ can_introspect_token
55+ ]}
56+ ]}
57+ ]}
58+ ]},
4359 {without_all_oauth_providers_settings , [], [
4460 {group , verify_get_oauth_provider }
4561 ]},
@@ -152,6 +168,40 @@ init_per_group(with_default_oauth_provider, Config) ->
152168 OAuthProvider # oauth_provider .id ),
153169 Config ;
154170
171+ init_per_group (with_introspection_endpoint , Config ) ->
172+ application :set_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint ,
173+ " https://introspection" ),
174+ Config ;
175+
176+ init_per_group (with_introspection_basic_client_credentials , Config ) ->
177+ application :set_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint_client_id ,
178+ " some-client-id" ),
179+ application :set_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint_client_secret ,
180+ " some-client-secret" ),
181+ application :set_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint_client_auth_method ,
182+ basic ),
183+ [{with_introspection_basic_client_credentials , [
184+ {introspection_endpoint , build_http_mock_behaviour (
185+ build_introspection_token_request (? MOCK_OPAQUE_TOKEN , basic , <<" some-client-id" >>,
186+ <<" some-client-secret" >>),
187+ build_http_200_introspection_token_response ())}
188+ ]} | Config ];
189+
190+ init_per_group (with_introspection_request_param_client_credentials , Config ) ->
191+ application :set_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint_client_id ,
192+ " some-client-id" ),
193+ application :set_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint_client_secret ,
194+ " some-client-secret" ),
195+ application :set_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint_client_auth_method ,
196+ request_param ),
197+ [{with_introspection_request_param_client_credentials , [
198+ {introspection_endpoint , build_http_mock_behaviour (
199+ build_introspection_token_request (? MOCK_OPAQUE_TOKEN , request_param , <<" some-client-id" >>,
200+ <<" some-client-secret" >>),
201+ build_http_200_introspection_token_response ())}
202+ ]} | Config ];
203+
204+
155205init_per_group (_ , Config ) ->
156206 Config .
157207
@@ -311,6 +361,10 @@ end_per_group(with_default_oauth_provider, Config) ->
311361 application :unset_env (rabbitmq_auth_backend_oauth2 , default_oauth_provider ),
312362 Config ;
313363
364+ end_per_group (with_introspection_endpoint , Config ) ->
365+ application :unset_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint ),
366+ Config ;
367+
314368end_per_group (_ , Config ) ->
315369 Config .
316370
@@ -598,19 +652,25 @@ get_oauth_provider_given_oauth_provider_id(Config) ->
598652 Jwks_uri )
599653 end .
600654
601- jwks_url_is_used_in_absense_of_jwks_uri (Config ) ->
655+ jwks_url_is_used_in_absense_of_jwks_uri (_Config ) ->
602656 {ok , # oauth_provider {
603657 jwks_uri = Jwks_uri }} = oauth2_client :get_oauth_provider ([jwks_uri ]),
604658 ? assertEqual (
605659 proplists :get_value (jwks_url , get_env (key_config , []), undefined ),
606660 Jwks_uri ).
607661
608- jwks_uri_takes_precedence_over_jwks_url (Config ) ->
662+ jwks_uri_takes_precedence_over_jwks_url (_Config ) ->
609663 {ok , # oauth_provider {
610664 jwks_uri = Jwks_uri }} = oauth2_client :get_oauth_provider ([jwks_uri ]),
611665 ? assertEqual (get_env (jwks_uri ), Jwks_uri ).
612666
613667
668+ cannot_introspect_due_to_missing_configuration (_Config )->
669+ {error , not_found_introspection_endpoint } = oauth2_client :introspect_token (<<" some token" >>).
670+
671+ can_introspect_token (_Config ) ->
672+ {ok , _ } = oauth2_client :introspect_token (<<" some token" >>).
673+
614674% %% HELPERS
615675
616676build_issuer (Scheme ) ->
@@ -816,6 +876,36 @@ denies_access_token_expectation() ->
816876 {? REQUEST_CLIENT_SECRET , <<" password" >>}
817877 ]), build_http_400_access_token_response ()
818878 ).
879+ build_introspection_token_request (Token , basic , ClientId , ClientSecret ) ->
880+ Map = build_http_request (
881+ <<" POST" >>,
882+ ? MOCK_TOKEN_ENDPOINT ,
883+ [
884+ {? REQUEST_TOKEN , Token }
885+ ]),
886+ Credentials = binary_to_list (<<ClientId /binary ," :" ,ClientSecret /binary >>),
887+ AuthStr = base64 :encode_to_string (Credentials ),
888+ maps :put (headers , #{
889+ <<" authorization" >> => " Basic " ++ AuthStr
890+ }, Map );
891+ build_introspection_token_request (Token , request_param , ClientId , ClientSecret ) ->
892+ build_http_request (
893+ <<" POST" >>,
894+ ? MOCK_INTROSPECTION_ENDPOINT ,
895+ [
896+ {? REQUEST_TOKEN , Token },
897+ {? REQUEST_CLIENT_ID , ClientId },
898+ {? REQUEST_CLIENT_SECRET , ClientSecret }
899+ ]).
900+ build_http_200_introspection_token_response () ->
901+ [
902+ {code , 200 },
903+ {content_type , ? CONTENT_JSON },
904+ {payload , [
905+ {active , true },
906+ {scope , <<" openid" >>}
907+ ]}
908+ ].
819909auth_server_error_when_access_token_request_expectation () ->
820910 build_http_mock_behaviour (build_http_request (
821911 <<" POST" >>,
0 commit comments