Skip to content

Commit 8562e4d

Browse files
Minor refactoring
1 parent c025270 commit 8562e4d

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_schema.erl

Lines changed: 44 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,
@@ -20,7 +29,8 @@ extract_value({_Name,V}) -> V.
2029

2130
-spec translate_scope_aliases([{list(), binary()}]) -> map().
2231
translate_scope_aliases(Conf) ->
23-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.scope_aliases", Conf),
32+
Settings = cuttlefish_variable:filter_by_prefix(
33+
?AUTH_OAUTH2_SCOPE_ALIASES, Conf),
2434
extract_scope_aliases_as_a_list_of_alias_scope_props(Settings).
2535

2636
convert_space_separated_string_to_list_of_binaries(String) ->
@@ -31,7 +41,7 @@ extract_scope_aliases_as_a_list_of_alias_scope_props(Settings) ->
3141
ValueFun = fun extract_value/1,
3242

3343
List0 = [{Index, {list_to_atom(Attr), V}}
34-
|| {["auth_oauth2", "scope_aliases", Index, Attr], V} <- Settings ],
44+
|| {[?AUTH_OAUTH2, ?SCOPE_ALIASES, Index, Attr], V} <- Settings ],
3545
List1 = maps:to_list(maps:groups_from_list(KeyFun, ValueFun, List0)),
3646
maps:from_list([
3747
extract_scope_alias_mapping(Proplist) || {_, Proplist} <- List1]).
@@ -55,7 +65,8 @@ extract_scope_alias_mapping(Proplist) ->
5565

5666
-spec translate_resource_servers([{list(), binary()}]) -> map().
5767
translate_resource_servers(Conf) ->
58-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.resource_servers", Conf),
68+
Settings = cuttlefish_variable:filter_by_prefix(
69+
?AUTH_OAUTH2_RESOURCE_SERVERS, Conf),
5970
Map = merge_list_of_maps([
6071
extract_resource_server_properties(Settings),
6172
extract_resource_server_preferred_username_claims(Settings)
@@ -66,12 +77,12 @@ translate_resource_servers(Conf) ->
6677
_ -> V
6778
end end, Map),
6879
ResourceServers = maps:values(Map0),
69-
lists:foldl(fun(Elem,AccMap)-> maps:put(proplists:get_value(id, Elem), Elem, AccMap) end, #{},
70-
ResourceServers).
80+
lists:foldl(fun(Elem,AccMap)-> maps:put(proplists:get_value(id, Elem),
81+
Elem, AccMap) end, #{}, ResourceServers).
7182

7283
-spec translate_oauth_providers([{list(), binary()}]) -> map().
7384
translate_oauth_providers(Conf) ->
74-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.oauth_providers", Conf),
85+
Settings = cuttlefish_variable:filter_by_prefix(?AUTH_OAUTH2_OAUTH_PROVIDERS, Conf),
7586

7687
merge_list_of_maps([
7788
extract_oauth_providers_properties(Settings),
@@ -81,8 +92,10 @@ translate_oauth_providers(Conf) ->
8192

8293
-spec translate_signing_keys([{list(), binary()}]) -> map().
8394
translate_signing_keys(Conf) ->
84-
Settings = cuttlefish_variable:filter_by_prefix("auth_oauth2.signing_keys", Conf),
85-
ListOfKidPath = lists:map(fun({Id, Path}) -> {list_to_binary(lists:last(Id)), Path} end, Settings),
95+
Settings = cuttlefish_variable:filter_by_prefix(
96+
?AUTH_OAUTH2_SIGNING_KEYS, Conf),
97+
ListOfKidPath = lists:map(fun({Id, Path}) -> {
98+
list_to_binary(lists:last(Id)), Path} end, Settings),
8699
translate_list_of_signing_keys(ListOfKidPath).
87100

88101
-spec translate_list_of_signing_keys([{list(), list()}]) -> map().
@@ -97,7 +110,8 @@ translate_list_of_signing_keys(ListOfKidPath) ->
97110
cuttlefish:invalid("file does not exist or cannot be read by the node")
98111
end
99112
end,
100-
maps:map(fun(_K, Path) -> {pem, TryReadingFileFun(Path)} end, maps:from_list(ListOfKidPath)).
113+
maps:map(fun(_K, Path) -> {pem, TryReadingFileFun(Path)} end,
114+
maps:from_list(ListOfKidPath)).
101115

102116
validator_file_exists(Attr, Filename) ->
103117
case file:read_file(Filename) of
@@ -120,23 +134,26 @@ validator_https_uri(Attr, Uri) ->
120134
end.
121135

122136
merge_list_of_maps(ListOfMaps) ->
123-
lists:foldl(fun(Elem, AccIn) -> maps:merge_with(fun(_K,V1,V2) -> V1 ++ V2 end,
124-
Elem, AccIn) end, #{}, ListOfMaps).
137+
lists:foldl(fun(Elem, AccIn) -> maps:merge_with(
138+
fun(_K,V1,V2) -> V1 ++ V2 end, Elem, AccIn) end, #{}, ListOfMaps).
125139

126140
extract_oauth_providers_properties(Settings) ->
127141
KeyFun = fun extract_key_as_binary/1,
128142
ValueFun = fun extract_value/1,
129143

130-
OAuthProviders = [{Name, mapOauthProviderProperty({list_to_atom(Key), list_to_binary(V)})}
131-
|| {["auth_oauth2","oauth_providers", Name, Key], V} <- Settings ],
144+
OAuthProviders = [{Name, mapOauthProviderProperty(
145+
{
146+
list_to_atom(Key),
147+
list_to_binary(V)})
148+
} || {[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, Key], V} <- Settings ],
132149
maps:groups_from_list(KeyFun, ValueFun, OAuthProviders).
133150

134151
extract_resource_server_properties(Settings) ->
135152
KeyFun = fun extract_key_as_binary/1,
136153
ValueFun = fun extract_value/1,
137154

138155
OAuthProviders = [{Name, {list_to_atom(Key), list_to_binary(V)}}
139-
|| {["auth_oauth2", "resource_servers", Name, Key], V} <- Settings ],
156+
|| {[?AUTH_OAUTH2, ?RESOURCE_SERVERS, Name, Key], V} <- Settings ],
140157
maps:groups_from_list(KeyFun, ValueFun, OAuthProviders).
141158

142159
mapOauthProviderProperty({Key, Value}) ->
@@ -153,10 +170,11 @@ extract_oauth_providers_https(Settings) ->
153170
ExtractProviderNameFun = fun extract_key_as_binary/1,
154171

155172
AttributesPerProvider = [{Name, mapHttpProperty({list_to_atom(Key), V})} ||
156-
{["auth_oauth2", "oauth_providers", Name, "https", Key], V} <- Settings ],
173+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "https", Key], V} <- Settings ],
157174

158175
maps:map(fun(_K,V)-> [{https, V}] end,
159-
maps:groups_from_list(ExtractProviderNameFun, fun({_, V}) -> V end, AttributesPerProvider)).
176+
maps:groups_from_list(ExtractProviderNameFun, fun({_, V}) -> V end,
177+
AttributesPerProvider)).
160178

161179
mapHttpProperty({Key, Value}) ->
162180
{Key, case Key of
@@ -168,8 +186,10 @@ extract_oauth_providers_algorithm(Settings) ->
168186
KeyFun = fun extract_key_as_binary/1,
169187

170188
IndexedAlgorithms = [{Name, {Index, list_to_binary(V)}} ||
171-
{["auth_oauth2", "oauth_providers", Name, "algorithms", Index], V} <- Settings ],
172-
SortedAlgorithms = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end, IndexedAlgorithms),
189+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "algorithms", Index], V}
190+
<- Settings ],
191+
SortedAlgorithms = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end,
192+
IndexedAlgorithms),
173193
Algorithms = [{Name, V} || {Name, {_I, V}} <- SortedAlgorithms],
174194
maps:map(fun(_K,V)-> [{algorithms, V}] end,
175195
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, Algorithms)).
@@ -178,8 +198,10 @@ extract_resource_server_preferred_username_claims(Settings) ->
178198
KeyFun = fun extract_key_as_binary/1,
179199

180200
IndexedClaims = [{Name, {Index, list_to_binary(V)}} ||
181-
{["auth_oauth2", "resource_servers", Name, "preferred_username_claims", Index], V} <- Settings ],
182-
SortedClaims = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end, IndexedClaims),
201+
{[?AUTH_OAUTH2, ?RESOURCE_SERVERS, Name, "preferred_username_claims",
202+
Index], V} <- Settings ],
203+
SortedClaims = lists:sort(fun({_,{AI,_}},{_,{BI,_}}) -> AI < BI end,
204+
IndexedClaims),
183205
Claims = [{Name, V} || {Name, {_I, V}} <- SortedClaims],
184206
maps:map(fun(_K,V)-> [{preferred_username_claims, V}] end,
185207
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, Claims)).
@@ -188,6 +210,7 @@ extract_oauth_providers_signing_keys(Settings) ->
188210
KeyFun = fun extract_key_as_binary/1,
189211

190212
IndexedSigningKeys = [{Name, {list_to_binary(Kid), list_to_binary(V)}} ||
191-
{["auth_oauth2","oauth_providers", Name, "signing_keys", Kid], V} <- Settings ],
213+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "signing_keys", Kid], V}
214+
<- Settings ],
192215
maps:map(fun(_K,V)-> [{signing_keys, translate_list_of_signing_keys(V)}] end,
193216
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, IndexedSigningKeys)).

0 commit comments

Comments
 (0)