Skip to content

Commit 76c5c86

Browse files
Minor refactoring
1 parent 1dd8ff5 commit 76c5c86

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_schema.erl

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77

88
-module(rabbit_oauth2_schema).
99

10+
-define(AUTH_OAUTH2, "auth_oauth2").
11+
-define(SCOPE_ALIASES, "scope_aliases").
12+
-define(RESOURCE_SERVERS, "resource_servers").
13+
-define(OAUTH_PROVIDERS, "oauth_providers").
14+
-define(SIGNING_KEYS, "signing_keys").
15+
-define(AUTH_OAUTH2_SCOPE_ALIASES, ?AUTH_OAUTH2 ++ "." ++ ?SCOPE_ALIASES).
16+
-define(AUTH_OAUTH2_RESOURCE_SERVERS, ?AUTH_OAUTH2 ++ "." ++ ?RESOURCE_SERVERS).
17+
-define(AUTH_OAUTH2_OAUTH_PROVIDERS, ?AUTH_OAUTH2 ++ "." ++ ?OAUTH_PROVIDERS).
18+
-define(AUTH_OAUTH2_SIGNING_KEYS, ?AUTH_OAUTH2 ++ "." ++ ?SIGNING_KEYS).
1019

1120
-export([
1221
translate_oauth_providers/1,
@@ -21,7 +30,8 @@ extract_value({_Name,V}) -> V.
2130

2231
-spec translate_scope_aliases([{list(), binary()}]) -> map().
2332
translate_scope_aliases(Conf) ->
24-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.scope_aliases", Conf),
33+
Settings = cuttlefish_variable:filter_by_prefix(
34+
?AUTH_OAUTH2_SCOPE_ALIASES, Conf),
2535
extract_scope_aliases_as_a_list_of_alias_scope_props(Settings).
2636

2737
convert_space_separated_string_to_list_of_binaries(String) ->
@@ -32,7 +42,7 @@ extract_scope_aliases_as_a_list_of_alias_scope_props(Settings) ->
3242
ValueFun = fun extract_value/1,
3343

3444
List0 = [{Index, {list_to_atom(Attr), V}}
35-
|| {["auth_oauth2", "scope_aliases", Index, Attr], V} <- Settings ],
45+
|| {[?AUTH_OAUTH2, ?SCOPE_ALIASES, Index, Attr], V} <- Settings ],
3646
List1 = maps:to_list(maps:groups_from_list(KeyFun, ValueFun, List0)),
3747
maps:from_list([
3848
extract_scope_alias_mapping(Proplist) || {_, Proplist} <- List1]).
@@ -56,8 +66,8 @@ extract_scope_alias_mapping(Proplist) ->
5666

5767
-spec translate_resource_servers([{list(), binary()}]) -> map().
5868
translate_resource_servers(Conf) ->
59-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.resource_servers",
60-
Conf),
69+
Settings = cuttlefish_variable:filter_by_prefix(
70+
?AUTH_OAUTH2_RESOURCE_SERVERS, Conf),
6171
Map = merge_list_of_maps([
6272
extract_resource_server_properties(Settings),
6373
extract_resource_server_preferred_username_claims(Settings)
@@ -68,14 +78,12 @@ translate_resource_servers(Conf) ->
6878
_ -> V
6979
end end, Map),
7080
ResourceServers = maps:values(Map0),
71-
lists:foldl(fun(Elem,AccMap) ->
72-
maps:put(proplists:get_value(id, Elem), Elem, AccMap) end, #{},
73-
ResourceServers).
81+
lists:foldl(fun(Elem,AccMap)-> maps:put(proplists:get_value(id, Elem),
82+
Elem, AccMap) end, #{}, ResourceServers).
7483

7584
-spec translate_oauth_providers([{list(), binary()}]) -> map().
7685
translate_oauth_providers(Conf) ->
77-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.oauth_providers",
78-
Conf),
86+
Settings = cuttlefish_variable:filter_by_prefix(?AUTH_OAUTH2_OAUTH_PROVIDERS, Conf),
7987

8088
merge_list_of_maps([
8189
extract_oauth_providers_properties(Settings),
@@ -88,8 +96,8 @@ translate_oauth_providers(Conf) ->
8896

8997
-spec translate_signing_keys([{list(), binary()}]) -> map().
9098
translate_signing_keys(Conf) ->
91-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.signing_keys",
92-
Conf),
99+
Settings = cuttlefish_variable:filter_by_prefix(
100+
?AUTH_OAUTH2_SIGNING_KEYS, Conf),
93101
ListOfKidPath = lists:map(fun({Id, Path}) -> {
94102
list_to_binary(lists:last(Id)), Path} end, Settings),
95103
translate_list_of_signing_keys(ListOfKidPath).
@@ -152,16 +160,18 @@ validator_https_uri(Attr, Uri) when is_list(Uri) ->
152160
end.
153161

154162
merge_list_of_maps(ListOfMaps) ->
155-
lists:foldl(fun(Elem, AccIn) -> maps:merge_with(fun(_K,V1,V2) -> V1 ++ V2 end,
156-
Elem, AccIn) end, #{}, ListOfMaps).
163+
lists:foldl(fun(Elem, AccIn) -> maps:merge_with(
164+
fun(_K,V1,V2) -> V1 ++ V2 end, Elem, AccIn) end, #{}, ListOfMaps).
157165

158166
extract_oauth_providers_properties(Settings) ->
159167
KeyFun = fun extract_key_as_binary/1,
160168
ValueFun = fun extract_value/1,
161169

162-
OAuthProviders = [
163-
{Name, mapOauthProviderProperty({list_to_atom(Key), list_to_binary(V)})}
164-
|| {["auth_oauth2", "oauth_providers", Name, Key], V} <- Settings],
170+
OAuthProviders = [{Name, mapOauthProviderProperty(
171+
{
172+
list_to_atom(Key),
173+
list_to_binary(V)})
174+
} || {[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, Key], V} <- Settings ],
165175
maps:groups_from_list(KeyFun, ValueFun, OAuthProviders).
166176

167177

@@ -170,7 +180,7 @@ extract_resource_server_properties(Settings) ->
170180
ValueFun = fun extract_value/1,
171181

172182
OAuthProviders = [{Name, {list_to_atom(Key), list_to_binary(V)}}
173-
|| {["auth_oauth2", "resource_servers", Name, Key], V} <- Settings ],
183+
|| {[?AUTH_OAUTH2, ?RESOURCE_SERVERS, Name, Key], V} <- Settings ],
174184
maps:groups_from_list(KeyFun, ValueFun, OAuthProviders).
175185

176186
mapOauthProviderProperty({Key, Value}) ->
@@ -192,7 +202,7 @@ extract_oauth_providers_https(Settings) ->
192202
ExtractProviderNameFun = fun extract_key_as_binary/1,
193203

194204
AttributesPerProvider = [{Name, mapHttpProperty({list_to_atom(Key), V})} ||
195-
{["auth_oauth2", "oauth_providers", Name, "https", Key], V} <- Settings ],
205+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "https", Key], V} <- Settings ],
196206

197207
maps:map(fun(_K,V)-> [{https, V}] end,
198208
maps:groups_from_list(ExtractProviderNameFun, fun({_, V}) -> V end,
@@ -208,7 +218,7 @@ extract_oauth_providers_algorithm(Settings) ->
208218
KeyFun = fun extract_key_as_binary/1,
209219

210220
IndexedAlgorithms = [{Name, {Index, list_to_binary(V)}} ||
211-
{["auth_oauth2","oauth_providers", Name, "algorithms", Index], V}
221+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "algorithms", Index], V}
212222
<- Settings ],
213223
SortedAlgorithms = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end,
214224
IndexedAlgorithms),
@@ -220,7 +230,7 @@ extract_resource_server_preferred_username_claims(Settings) ->
220230
KeyFun = fun extract_key_as_binary/1,
221231

222232
IndexedClaims = [{Name, {Index, list_to_binary(V)}} ||
223-
{["auth_oauth2","resource_servers", Name, "preferred_username_claims",
233+
{[?AUTH_OAUTH2, ?RESOURCE_SERVERS, Name, "preferred_username_claims",
224234
Index], V} <- Settings ],
225235
SortedClaims = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end,
226236
IndexedClaims),
@@ -241,7 +251,7 @@ extract_oauth_providers_signing_keys(Settings) ->
241251
KeyFun = fun extract_key_as_binary/1,
242252

243253
IndexedSigningKeys = [{Name, {list_to_binary(Kid), list_to_binary(V)}} ||
244-
{["auth_oauth2","oauth_providers", Name, "signing_keys", Kid], V}
254+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "signing_keys", Kid], V}
245255
<- Settings ],
246256
maps:map(fun(_K,V)-> [{signing_keys, translate_list_of_signing_keys(V)}] end,
247257
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, IndexedSigningKeys)).

0 commit comments

Comments
 (0)