Skip to content

Commit 58716aa

Browse files
Modify schema to include scope_aliases
WIP Add translation function
1 parent 692f299 commit 58716aa

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
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: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,54 @@
1212
translate_oauth_providers/1,
1313
translate_resource_servers/1,
1414
translate_signing_keys/1,
15-
translate_endpoint_params/2
15+
translate_endpoint_params/2,
16+
translate_scope_aliases/1
1617
]).
1718

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

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

136178
OAuthProviders = [{Name, {list_to_atom(Key), list_to_binary(V)}}
137-
|| {["auth_oauth2","resource_servers", Name, Key], V} <- Settings ],
179+
|| {["auth_oauth2", "resource_servers", Name, Key], V} <- Settings ],
138180
maps:groups_from_list(KeyFun, ValueFun, OAuthProviders).
139181

140182
mapOauthProviderProperty({Key, Value}) ->
@@ -156,7 +198,7 @@ extract_oauth_providers_https(Settings) ->
156198
ExtractProviderNameFun = fun extract_key_as_binary/1,
157199

158200
AttributesPerProvider = [{Name, mapHttpProperty({list_to_atom(Key), V})} ||
159-
{["auth_oauth2","oauth_providers", Name, "https", Key], V} <- Settings ],
201+
{["auth_oauth2", "oauth_providers", Name, "https", Key], V} <- Settings ],
160202

161203
maps:map(fun(_K,V)-> [{https, V}] end,
162204
maps:groups_from_list(ExtractProviderNameFun, fun({_, V}) -> 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
@@ -196,5 +196,45 @@
196196
{scope_prefix,<<>>}
197197
]}
198198
],[]
199+
},
200+
{scope_aliases_1,
201+
"auth_oauth2.resource_server_id = new_resource_server_id
202+
auth_oauth2.scope_aliases.admin = rabbitmq.tag:administrator
203+
auth_oauth2.scope_aliases.developer = rabbitmq.tag:management rabbitmq.read:*/*",
204+
[
205+
{rabbitmq_auth_backend_oauth2, [
206+
{resource_server_id,<<"new_resource_server_id">>},
207+
{scope_aliases, #{
208+
<<"admin">> => [
209+
<<"rabbitmq.tag:administrator">>
210+
],
211+
<<"developer">> => [
212+
<<"rabbitmq.tag:administrator">>,
213+
<<"rabbitmq.read:*/*">>
214+
]
215+
}}
216+
]}
217+
], []
218+
},
219+
{scope_aliases_2,
220+
"auth_oauth2.resource_server_id = new_resource_server_id
221+
auth_oauth2.scope_aliases.1.alias = admin
222+
auth_oauth2.scope_aliases.1.scope = rabbitmq.tag:administrator
223+
auth_oauth2.scope_aliases.2.alias = developer
224+
auth_oauth2.scope_aliases.2.scope = rabbitmq.tag:management rabbitmq.read:*/*",
225+
[
226+
{rabbitmq_auth_backend_oauth2, [
227+
{resource_server_id,<<"new_resource_server_id">>},
228+
{scope_aliases, #{
229+
<<"admin">> => [
230+
<<"rabbitmq.tag:administrator">>
231+
],
232+
<<"developer">> => [
233+
<<"rabbitmq.tag:administrator">>,
234+
<<"rabbitmq.read:*/*">>
235+
]
236+
}}
237+
]}
238+
], []
199239
}
200240
].

0 commit comments

Comments
 (0)