Skip to content

Commit 6891fe3

Browse files
Modify schema to include scope_aliases
WIP Add translation function
1 parent bc1e0ad commit 6891fe3

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

deps/rabbitmq_auth_backend_oauth2/priv/schema/rabbitmq_auth_backend_oauth2.schema

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@
7373
list_to_binary(cuttlefish:conf_get("auth_oauth2.additional_scopes_key", Conf))
7474
end}.
7575

76+
{mapping,
77+
"auth_oauth2.scope_aliases.$alias",
78+
"rabbitmq_auth_backend_oauth2.scope_aliases",
79+
[{datatype, string}]}.
80+
81+
{translation,
82+
"rabbitmq_auth_backend_oauth2.scope_aliases",
83+
fun(Conf) ->
84+
rabbit_oauth2_schema:translate_scope_aliases(Conf)
85+
end}.
7686

7787
%% Configure the plugin to skip validation of the aud field
7888
%%

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_schema.erl

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,54 @@
1111
-export([
1212
translate_oauth_providers/1,
1313
translate_resource_servers/1,
14-
translate_signing_keys/1
14+
translate_signing_keys/1,
15+
translate_scope_aliases/1
1516
]).
1617

1718
extract_key_as_binary({Name,_}) -> list_to_binary(Name).
1819
extract_value({_Name,V}) -> V.
1920

21+
-spec translate_scope_aliases([{list(), binary()}]) -> map().
22+
translate_scope_aliases(Conf) ->
23+
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.scope_aliases", Conf),
24+
maps:merge(extract_scope_aliases_as_a_map(Settings),
25+
extract_scope_aliases_as_a_list_of_alias_scope_props(Settings)).
26+
27+
convert_space_separated_string_to_list_of_binaries(String) ->
28+
[ list_to_binary(V) || V <- string:tokens(String, " ")].
29+
30+
extract_scope_aliases_as_a_map(Settings) ->
31+
maps:from_list([{
32+
list_to_binary(K),
33+
convert_space_separated_string_to_list_of_binaries(V)
34+
} || {["auth_oauth2", "scope_aliases", K], V} <- Settings ]).
35+
extract_scope_aliases_as_a_list_of_alias_scope_props(Settings) ->
36+
KeyFun = fun extract_key_as_binary/1,
37+
ValueFun = fun extract_value/1,
38+
39+
List0 = [{K, {list_to_atom(Attr), list_to_binary(V)}}
40+
|| {["auth_oauth2", "scope_aliases", K, Attr], V} <- Settings ],
41+
List1 = maps:to_list(maps:groups_from_list(KeyFun, ValueFun, List0)),
42+
maps:from_list([
43+
extract_scope_alias_mapping(Proplist) || {_, Proplist} <- List1]).
44+
45+
extract_scope_alias_mapping(Proplist) ->
46+
Alias =
47+
case proplists:get_value(alias, Proplist) of
48+
undefined -> {error, missing_alias_attribute};
49+
A -> A
50+
end,
51+
Scope =
52+
case proplists:get_value(scope, Proplist) of
53+
undefined -> {error, missing_scope_attribute};
54+
S -> convert_space_separated_string_to_list_of_binaries(S)
55+
end,
56+
case {Alias, Scope} of
57+
{{error, _} = Err0, _} -> Err0;
58+
{_, {error, _} = Err1 } -> Err1;
59+
_ = V -> V
60+
end.
61+
2062
-spec translate_resource_servers([{list(), binary()}]) -> map().
2163
translate_resource_servers(Conf) ->
2264
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.resource_servers", Conf),
@@ -100,7 +142,7 @@ extract_resource_server_properties(Settings) ->
100142
ValueFun = fun extract_value/1,
101143

102144
OAuthProviders = [{Name, {list_to_atom(Key), list_to_binary(V)}}
103-
|| {["auth_oauth2","resource_servers", Name, Key], V} <- Settings ],
145+
|| {["auth_oauth2", "resource_servers", Name, Key], V} <- Settings ],
104146
maps:groups_from_list(KeyFun, ValueFun, OAuthProviders).
105147

106148
mapOauthProviderProperty({Key, Value}) ->
@@ -117,7 +159,7 @@ extract_oauth_providers_https(Settings) ->
117159
ExtractProviderNameFun = fun extract_key_as_binary/1,
118160

119161
AttributesPerProvider = [{Name, mapHttpProperty({list_to_atom(Key), V})} ||
120-
{["auth_oauth2","oauth_providers", Name, "https", Key], V} <- Settings ],
162+
{["auth_oauth2", "oauth_providers", Name, "https", Key], V} <- Settings ],
121163

122164
maps:map(fun(_K,V)-> [{https, V}] end,
123165
maps:groups_from_list(ExtractProviderNameFun, fun({_, V}) -> V end, AttributesPerProvider)).
@@ -132,7 +174,7 @@ extract_oauth_providers_algorithm(Settings) ->
132174
KeyFun = fun extract_key_as_binary/1,
133175

134176
IndexedAlgorithms = [{Name, {Index, list_to_binary(V)}} ||
135-
{["auth_oauth2","oauth_providers", Name, "algorithms", Index], V} <- Settings ],
177+
{["auth_oauth2", "oauth_providers", Name, "algorithms", Index], V} <- Settings ],
136178
SortedAlgorithms = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end, IndexedAlgorithms),
137179
Algorithms = [{Name, V} || {Name, {_I, V}} <- SortedAlgorithms],
138180
maps:map(fun(_K,V)-> [{algorithms, V}] end,
@@ -142,7 +184,7 @@ extract_resource_server_preferred_username_claims(Settings) ->
142184
KeyFun = fun extract_key_as_binary/1,
143185

144186
IndexedClaims = [{Name, {Index, list_to_binary(V)}} ||
145-
{["auth_oauth2","resource_servers", Name, "preferred_username_claims", Index], V} <- Settings ],
187+
{["auth_oauth2", "resource_servers", Name, "preferred_username_claims", Index], V} <- Settings ],
146188
SortedClaims = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end, IndexedClaims),
147189
Claims = [{Name, V} || {Name, {_I, V}} <- SortedClaims],
148190
maps:map(fun(_K,V)-> [{preferred_username_claims, V}] end,

deps/rabbitmq_auth_backend_oauth2/test/config_schema_SUITE_data/rabbitmq_auth_backend_oauth2.snippets

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,45 @@
184184
{scope_prefix,<<>>}
185185
]}
186186
],[]
187+
},
188+
{scope_aliases_1,
189+
"auth_oauth2.resource_server_id = new_resource_server_id
190+
auth_oauth2.scope_aliases.admin = rabbitmq.tag:administrator
191+
auth_oauth2.scope_aliases.developer = rabbitmq.tag:management rabbitmq.read:*/*",
192+
[
193+
{rabbitmq_auth_backend_oauth2, [
194+
{resource_server_id,<<"new_resource_server_id">>},
195+
{scope_aliases, #{
196+
<<"admin">> => [
197+
<<"rabbitmq.tag:administrator">>
198+
],
199+
<<"developer">> => [
200+
<<"rabbitmq.tag:administrator">>,
201+
<<"rabbitmq.read:*/*">>
202+
]
203+
}}
204+
]}
205+
], []
206+
},
207+
{scope_aliases_2,
208+
"auth_oauth2.resource_server_id = new_resource_server_id
209+
auth_oauth2.scope_aliases.1.alias = admin
210+
auth_oauth2.scope_aliases.1.scope = rabbitmq.tag:administrator
211+
auth_oauth2.scope_aliases.2.alias = developer
212+
auth_oauth2.scope_aliases.2.scope = rabbitmq.tag:management rabbitmq.read:*/*",
213+
[
214+
{rabbitmq_auth_backend_oauth2, [
215+
{resource_server_id,<<"new_resource_server_id">>},
216+
{scope_aliases, #{
217+
<<"admin">> => [
218+
<<"rabbitmq.tag:administrator">>
219+
],
220+
<<"developer">> => [
221+
<<"rabbitmq.tag:administrator">>,
222+
<<"rabbitmq.read:*/*">>
223+
]
224+
}}
225+
]}
226+
], []
187227
}
188228
].

0 commit comments

Comments
 (0)