Skip to content

Commit e3ae834

Browse files
Add token endpoint params to schema
1 parent cb9bda4 commit e3ae834

File tree

4 files changed

+99
-22
lines changed

4 files changed

+99
-22
lines changed

deps/oauth2_client/include/types.hrl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
%%
77

88
%% The closest we have to a type import in Erlang
9-
-type option(T) :: rabbit_types:option(T).
9+
-type(option(T) :: T | 'undefined').
1010

1111
-type oauth_provider_id() :: root | binary().
1212

@@ -22,10 +22,15 @@
2222
-record(oauth_provider, {
2323
id :: oauth_provider_id(),
2424
issuer :: option(uri_string:uri_string()),
25+
discovery_endpoint_path :: option(uri_string:uri_string()),
26+
discovery_endpoint_params :: option([tuple()]),
2527
token_endpoint :: option(uri_string:uri_string()),
28+
token_endpoint_params :: option([tuple()]),
2629
authorization_endpoint :: option(uri_string:uri_string()),
30+
authorization_endpoint_params :: option([tuple()]),
2731
end_session_endpoint :: option(uri_string:uri_string()),
2832
jwks_uri :: option(uri_string:uri_string()),
33+
jwks_uri_params :: option([tuple()]),
2934
ssl_options :: option(list())
3035
}).
3136

deps/rabbitmq_auth_backend_oauth2/priv/schema/rabbitmq_auth_backend_oauth2.schema

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,55 @@
158158
"rabbitmq_auth_backend_oauth2.authorization_endpoint",
159159
[{datatype, string}, {validators, ["uri", "https_uri"]}]}.
160160

161-
%% auth_oauth2.authorization_endpoint_params.audience
162-
%% auth_oauth2.resource_servers.rabbitmq.authorization_endpoint_params.audience
163-
%% auth_oauth2.resource_servers.rabbitmq.token_endpoint_params.audience
164-
%% auth_oauth2.resource_servers.rabbitmq.jkws_uri_params.appId =
165-
166-
%%{mapping,
167-
%% "auth_oauth2.authorization_endpoint_params.$param",
168-
%% "rabbitmq_auth_backend_oauth2.oauth_providers",
169-
%% [{datatype, string}]}.
170-
171-
%%{translation, "rabbitmq_auth_backend_oauth2.authorization_endpoint_params",
172-
%% fun(Conf) ->
173-
%% oauth2_schema:translate_authorization_endpoint_params(Conf)
174-
%% end}.
161+
{mapping,
162+
"auth_oauth2.authorization_endpoint_params.$param",
163+
"rabbitmq_auth_backend_oauth2.authorization_endpoint_params",
164+
[{datatype, string}]}.
165+
166+
{translation, "rabbitmq_auth_backend_oauth2.authorization_endpoint_params",
167+
fun(Conf) ->
168+
oauth2_schema:translate_endpoint_params("authorization_endpoint_params", Conf)
169+
end}.
170+
171+
{mapping,
172+
"auth_oauth2.discovery_endpoint_path",
173+
"rabbitmq_auth_backend_oauth2.discovery_endpoint_path",
174+
[{datatype, string}]}.
175+
176+
{mapping,
177+
"auth_oauth2.discovery_endpoint_params.$param",
178+
"rabbitmq_auth_backend_oauth2.discovery_endpoint_params",
179+
[{datatype, string}]}.
180+
181+
{translation, "rabbitmq_auth_backend_oauth2.discovery_endpoint_params",
182+
fun(Conf) ->
183+
oauth2_schema:translate_endpoint_params("discovery_endpoint_params", Conf)
184+
end}.
185+
186+
{mapping,
187+
"auth_oauth2.oauth_providers.$name.discovery_endpoint_params.$param",
188+
"rabbitmq_auth_backend_oauth2.oauth_providers",
189+
[{datatype, string}]}.
190+
191+
{mapping,
192+
"auth_oauth2.token_endpoint_params.$param",
193+
"rabbitmq_auth_backend_oauth2.token_endpoint_params",
194+
[{datatype, string}]}.
195+
196+
{translation, "rabbitmq_auth_backend_oauth2.token_endpoint_params",
197+
fun(Conf) ->
198+
oauth2_schema:translate_endpoint_params("token_endpoint_params", Conf)
199+
end}.
200+
201+
{mapping,
202+
"auth_oauth2.oauth_providers.$name.authorization_endpoint_params.$param",
203+
"rabbitmq_auth_backend_oauth2.oauth_providers",
204+
[{datatype, string}]}.
205+
206+
{mapping,
207+
"auth_oauth2.oauth_providers.$name.token_endpoint_params.$param",
208+
"rabbitmq_auth_backend_oauth2.oauth_providers",
209+
[{datatype, string}]}.
175210

176211
{mapping,
177212
"auth_oauth2.oauth_providers.$name.algorithms.$algorithm",

deps/rabbitmq_auth_backend_oauth2/src/oauth2_schema.erl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
-export([
1212
translate_oauth_providers/1,
1313
translate_resource_servers/1,
14-
translate_signing_keys/1 %,
15-
%%translate_authorization_endpoint_params/1
14+
translate_signing_keys/1,
15+
translate_endpoint_params/2
1616
]).
1717

1818
extract_key_as_binary({Name,_}) -> list_to_binary(Name).
@@ -40,9 +40,13 @@ translate_oauth_providers(Conf) ->
4040

4141
merge_list_of_maps([
4242
extract_oauth_providers_properties(Settings),
43+
extract_oauth_providers_endpoint_params(discovery_endpoint_params, Settings),
44+
extract_oauth_providers_endpoint_params(authorization_endpoint_params, Settings),
45+
extract_oauth_providers_endpoint_params(token_endpoint_params, Settings),
4346
extract_oauth_providers_algorithm(Settings),
4447
extract_oauth_providers_https(Settings),
45-
extract_oauth_providers_signing_keys(Settings)]).
48+
extract_oauth_providers_signing_keys(Settings)
49+
]).
4650

4751
-spec translate_signing_keys([{list(), binary()}]) -> map().
4852
translate_signing_keys(Conf) ->
@@ -64,10 +68,13 @@ translate_list_of_signing_keys(ListOfKidPath) ->
6468
end,
6569
maps:map(fun(_K, Path) -> {pem, TryReadingFileFun(Path)} end, maps:from_list(ListOfKidPath)).
6670

67-
%%-spec translate_authorization_endpoint_params([{list(), binary()}]) -> map().
68-
%%translate_authorization_endpoint_params(Conf) ->
69-
%% Params = cuttlefish_variable:filter_by_prefix("auth_oauth2.authorization_endpoint_params", Conf),
70-
%% lists:map(fun({Id, Value}) -> {list_to_binary(lists:last(Id)), Value} end, Params).
71+
-spec translate_endpoint_params(list(), [{list(), binary()}]) -> map().
72+
translate_endpoint_params(Variable, Conf) ->
73+
Params0 = cuttlefish_variable:filter_by_prefix("auth_oauth2." ++ Variable, Conf),
74+
ct:log("translate_endpoint_params ~p -> ~p", [Variable, Params0]),
75+
Params = [{list_to_binary(Param), list_to_binary(V)} ||
76+
{["auth_oauth2", Name, Param], V} <- Params0],
77+
maps:from_list(Params).
7178

7279
validator_file_exists(Attr, Filename) ->
7380
case file:read_file(Filename) of
@@ -154,6 +161,14 @@ extract_resource_server_preferred_username_claims(Settings) ->
154161
maps:map(fun(_K,V)-> [{preferred_username_claims, V}] end,
155162
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, Claims)).
156163

164+
extract_oauth_providers_endpoint_params(Variable, Settings) ->
165+
KeyFun = fun extract_key_as_binary/1,
166+
167+
IndexedParams = [{Name, {ParamName, list_to_binary(V)}} ||
168+
{["auth_oauth2","oauth_providers", Name, EndpointVar, ParamName], V} <- Settings, EndpointVar == Variable ],
169+
maps:map(fun(_K,V)-> [{Variable, V}] end,
170+
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, IndexedParams)).
171+
157172
extract_oauth_providers_signing_keys(Settings) ->
158173
KeyFun = fun extract_key_as_binary/1,
159174

deps/rabbitmq_auth_backend_oauth2/test/oauth2_schema_SUITE.erl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
-include_lib("common_test/include/ct.hrl").
1313
-include_lib("eunit/include/eunit.hrl").
1414

15+
-import(oauth2_schema, [translate_endpoint_params/2, translate_oauth_providers/1]).
1516

1617
all() ->
1718
[
@@ -24,6 +25,8 @@ all() ->
2425
test_oauth_providers_https,
2526
test_oauth_providers_https_with_missing_cacertfile,
2627
test_oauth_providers_signing_keys,
28+
test_without_endpoint_params,
29+
test_with_endpoint_params,
2730
test_without_resource_servers,
2831
test_with_one_resource_server,
2932
test_with_many_resource_servers,
@@ -38,6 +41,25 @@ test_without_oauth_providers(_) ->
3841
test_without_resource_servers(_) ->
3942
#{} = oauth2_schema:translate_resource_servers([]).
4043

44+
test_without_endpoint_params(_) ->
45+
#{} = translate_endpoint_params("discovery_endpoint_params", []),
46+
#{} = translate_endpoint_params("token_endpoint_params", []),
47+
#{} = translate_endpoint_params("authorization_endpoint_params", []).
48+
49+
test_with_endpoint_params(_) ->
50+
Conf = [
51+
{["auth_oauth2","discovery_endpoint_params","param1"], "some-value1"},
52+
{["auth_oauth2","discovery_endpoint_params","param2"], "some-value2"},
53+
{["auth_oauth2","token_endpoint_params","audience"], "some-audience"},
54+
{["auth_oauth2","authorization_endpoint_params","resource"], "some-resource"}
55+
],
56+
#{ <<"param1">> := <<"some-value1">>, <<"param2">> := <<"some-value2">> } =
57+
translate_endpoint_params("discovery_endpoint_params", Conf),
58+
#{ <<"audience">> := <<"some-audience">>} =
59+
translate_endpoint_params("token_endpoint_params", Conf),
60+
#{ <<"resource">> := <<"some-resource">>} =
61+
translate_endpoint_params("authorization_endpoint_params", Conf).
62+
4163
test_with_one_oauth_provider(_) ->
4264
Conf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://rabbit"}
4365
],

0 commit comments

Comments
 (0)