1
- % % This Source Code Form is subject to the terms of the Mozilla Public
2
1
% % License, v. 2.0. If a copy of the MPL was not distributed with this
3
2
% % file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
3
% %
17
16
18
17
-compile (export_all ).
19
18
19
+ -define (MOCK_OPAQUE_TOKEN , <<" some opaque token" >>).
20
+ -define (MOCK_INTROSPECTION_ENDPOINT , <<" /introspection" >>).
20
21
-define (MOCK_TOKEN_ENDPOINT , <<" /token" >>).
21
22
-define (AUTH_PORT , 8000 ).
22
23
-define (ISSUER_PATH , " /somepath" ).
@@ -28,7 +29,8 @@ all() ->
28
29
[
29
30
{group , https_down },
30
31
{group , https },
31
- {group , with_all_oauth_provider_settings }
32
+ {group , with_all_oauth_provider_settings },
33
+ {group , verify_introspect_token }
32
34
33
35
].
34
36
@@ -40,6 +42,20 @@ groups() ->
40
42
jwks_uri_takes_precedence_over_jwks_url ,
41
43
jwks_url_is_used_in_absense_of_jwks_uri
42
44
]},
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
+ ]},
43
59
{without_all_oauth_providers_settings , [], [
44
60
{group , verify_get_oauth_provider }
45
61
]},
@@ -152,6 +168,40 @@ init_per_group(with_default_oauth_provider, Config) ->
152
168
OAuthProvider # oauth_provider .id ),
153
169
Config ;
154
170
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
+
155
205
init_per_group (_ , Config ) ->
156
206
Config .
157
207
@@ -311,6 +361,10 @@ end_per_group(with_default_oauth_provider, Config) ->
311
361
application :unset_env (rabbitmq_auth_backend_oauth2 , default_oauth_provider ),
312
362
Config ;
313
363
364
+ end_per_group (with_introspection_endpoint , Config ) ->
365
+ application :unset_env (rabbitmq_auth_backend_oauth2 , introspection_endpoint ),
366
+ Config ;
367
+
314
368
end_per_group (_ , Config ) ->
315
369
Config .
316
370
@@ -598,19 +652,25 @@ get_oauth_provider_given_oauth_provider_id(Config) ->
598
652
Jwks_uri )
599
653
end .
600
654
601
- jwks_url_is_used_in_absense_of_jwks_uri (Config ) ->
655
+ jwks_url_is_used_in_absense_of_jwks_uri (_Config ) ->
602
656
{ok , # oauth_provider {
603
657
jwks_uri = Jwks_uri }} = oauth2_client :get_oauth_provider ([jwks_uri ]),
604
658
? assertEqual (
605
659
proplists :get_value (jwks_url , get_env (key_config , []), undefined ),
606
660
Jwks_uri ).
607
661
608
- jwks_uri_takes_precedence_over_jwks_url (Config ) ->
662
+ jwks_uri_takes_precedence_over_jwks_url (_Config ) ->
609
663
{ok , # oauth_provider {
610
664
jwks_uri = Jwks_uri }} = oauth2_client :get_oauth_provider ([jwks_uri ]),
611
665
? assertEqual (get_env (jwks_uri ), Jwks_uri ).
612
666
613
667
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
+
614
674
% %% HELPERS
615
675
616
676
build_issuer (Scheme ) ->
@@ -816,6 +876,36 @@ denies_access_token_expectation() ->
816
876
{? REQUEST_CLIENT_SECRET , <<" password" >>}
817
877
]), build_http_400_access_token_response ()
818
878
).
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
+ ].
819
909
auth_server_error_when_access_token_request_expectation () ->
820
910
build_http_mock_behaviour (build_http_request (
821
911
<<" POST" >>,
0 commit comments