Skip to content

Commit 96571e0

Browse files
rabbit_oauth2_schema: naming, formatting
1 parent cc8f954 commit 96571e0

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_schema.erl

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,35 @@
2424
translate_scope_aliases/1
2525
]).
2626

27-
extract_key_as_binary({Name,_}) -> list_to_binary(Name).
28-
extract_value({_Name,V}) -> V.
27+
extract_binary_key({Name, _}) -> rabbit_data_coercion:to_binary(Name).
28+
extract_value({_Name, V}) -> V.
2929

3030
-spec translate_scope_aliases([{list(), binary()}]) -> map().
3131
translate_scope_aliases(Conf) ->
3232
Settings = cuttlefish_variable:filter_by_prefix(
3333
?AUTH_OAUTH2_SCOPE_ALIASES, Conf),
3434
maps:merge(extract_scope_alias_as_map(Settings),
3535
extract_scope_aliases_as_list_of_alias_scope_props(Settings)).
36-
37-
convert_space_separated_string_to_list_of_binaries(String) ->
36+
37+
binary_tokens_from(String) ->
3838
[ list_to_binary(V) || V <- string:tokens(String, " ")].
3939

4040
extract_scope_alias_as_map(Settings) ->
4141
maps:from_list([{
42-
list_to_binary(Alias),
43-
convert_space_separated_string_to_list_of_binaries(Scope)
42+
rabbit_data_coercion:to_binary(Alias),
43+
binary_tokens_from(Scope)
4444
}
4545
|| {[?AUTH_OAUTH2, ?SCOPE_ALIASES, Alias], Scope} <- Settings ]).
4646

4747
extract_scope_aliases_as_list_of_alias_scope_props(Settings) ->
48-
KeyFun = fun extract_key_as_binary/1,
48+
KeyFun = fun extract_binary_key/1,
4949
ValueFun = fun extract_value/1,
5050

51-
List0 = [{Index, {list_to_atom(Attr), V}}
52-
|| {[?AUTH_OAUTH2, ?SCOPE_ALIASES, Index, Attr], V} <- Settings ],
53-
List1 = maps:to_list(maps:groups_from_list(KeyFun, ValueFun, List0)),
51+
List0 = [{Index, {list_to_atom(Attr), V}} || {[?AUTH_OAUTH2, ?SCOPE_ALIASES, Index, Attr], V} <- Settings ],
52+
List1 = maps:to_list(maps:groups_from_list(KeyFun, ValueFun, List0)),
5453
List2 = [extract_scope_alias_mapping(Proplist) || {_, Proplist} <- List1],
5554
maps:from_list([ V || V <- List2, V =/= {}]).
56-
55+
5756
extract_scope_alias_mapping(Proplist) ->
5857
Alias =
5958
case proplists:get_value(alias, Proplist) of
@@ -63,7 +62,7 @@ extract_scope_alias_mapping(Proplist) ->
6362
Scope =
6463
case proplists:get_value(scope, Proplist) of
6564
undefined -> {error, missing_scope_attribute};
66-
S -> convert_space_separated_string_to_list_of_binaries(S)
65+
S -> binary_tokens_from(S)
6766
end,
6867
case {Alias, Scope} of
6968
{{error, _}, _} ->
@@ -77,50 +76,49 @@ extract_scope_alias_mapping(Proplist) ->
7776
_ = V -> V
7877
end.
7978

80-
extract_resource_server_scope_aliases_as_list_of_props(Settings) ->
81-
KeyFun = fun extract_key_as_binary/1,
79+
extract_resource_server_scope_aliases_as_proplist(Settings) ->
80+
KeyFun = fun extract_binary_key/1,
8281
ValueFun = fun extract_value/1,
8382

8483
List0 = [
8584
{
86-
Name,
87-
{Index, {list_to_atom(Attr), V}}
88-
} ||
85+
Name,
86+
{Index, {rabbit_data_coercion:to_atom(Attr), V}}
87+
} ||
8988
{[
9089
?AUTH_OAUTH2, ?RESOURCE_SERVERS, Name, ?SCOPE_ALIASES,
9190
Index, Attr
9291
], V
93-
} <- Settings ],
92+
} <- Settings],
9493
Map0 = maps:groups_from_list(KeyFun, ValueFun, List0),
95-
94+
9695
Map4 = maps:map(fun (_, L) ->
97-
Map2 = maps:map(fun (_, L2) -> extract_scope_alias_mapping(L2) end,
96+
Map2 = maps:map(fun (_, L2) -> extract_scope_alias_mapping(L2) end,
9897
maps:groups_from_list(KeyFun, ValueFun, L)),
9998
Map3 = maps:filter(fun (_,V) -> V =/= {} end, Map2),
10099
[{scope_aliases, maps:from_list([ V || {_, V} <- maps:to_list(Map3)])}]
101100
end, Map0),
102-
103101
Map4.
104-
102+
105103
extract_resource_server_scope_aliases_as_map(Settings) ->
106-
KeyFun = fun extract_key_as_binary/1,
104+
KeyFun = fun extract_binary_key/1,
107105
ValueFun = fun extract_value/1,
108106

109107
List0 = [
110108
{
111-
Name,
109+
Name,
112110
{
113111
list_to_binary(Alias),
114-
convert_space_separated_string_to_list_of_binaries(Scope)
115-
}
116-
} ||
112+
binary_tokens_from(Scope)
113+
}
114+
} ||
117115
{[
118116
?AUTH_OAUTH2, ?RESOURCE_SERVERS, Name, ?SCOPE_ALIASES,
119117
Alias
120118
], Scope
121119
} <- Settings ],
122120
Map0 = maps:groups_from_list(KeyFun, ValueFun, List0),
123-
maps:map(fun (_, L) -> [{scope_aliases, maps:from_list(L)}] end, Map0).
121+
maps:map(fun (_, L) -> [{scope_aliases, maps:from_list(L)}] end, Map0).
124122

125123
-spec translate_resource_servers([{list(), binary()}]) -> map().
126124
translate_resource_servers(Conf) ->
@@ -129,7 +127,7 @@ translate_resource_servers(Conf) ->
129127
Map = merge_list_of_maps([
130128
extract_resource_server_properties(Settings),
131129
extract_resource_server_preferred_username_claims(Settings),
132-
extract_resource_server_scope_aliases_as_list_of_props(Settings),
130+
extract_resource_server_scope_aliases_as_proplist(Settings),
133131
extract_resource_server_scope_aliases_as_map(Settings)
134132
]),
135133
Map0 = maps:map(fun(K,V) ->
@@ -202,7 +200,7 @@ merge_list_of_maps(ListOfMaps) ->
202200
fun(_K,V1,V2) -> V1 ++ V2 end, Elem, AccIn) end, #{}, ListOfMaps).
203201

204202
extract_oauth_providers_properties(Settings) ->
205-
KeyFun = fun extract_key_as_binary/1,
203+
KeyFun = fun extract_binary_key/1,
206204
ValueFun = fun extract_value/1,
207205

208206
OAuthProviders = [{Name, mapOauthProviderProperty(
@@ -213,7 +211,7 @@ extract_oauth_providers_properties(Settings) ->
213211
maps:groups_from_list(KeyFun, ValueFun, OAuthProviders).
214212

215213
extract_resource_server_properties(Settings) ->
216-
KeyFun = fun extract_key_as_binary/1,
214+
KeyFun = fun extract_binary_key/1,
217215
ValueFun = fun extract_value/1,
218216

219217
OAuthProviders = [{Name, {list_to_atom(Key), list_to_binary(V)}}
@@ -231,7 +229,7 @@ mapOauthProviderProperty({Key, Value}) ->
231229
end}.
232230

233231
extract_oauth_providers_https(Settings) ->
234-
ExtractProviderNameFun = fun extract_key_as_binary/1,
232+
ExtractProviderNameFun = fun extract_binary_key/1,
235233

236234
AttributesPerProvider = [{Name, mapHttpProperty({list_to_atom(Key), V})} ||
237235
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "https", Key], V} <- Settings ],
@@ -247,7 +245,7 @@ mapHttpProperty({Key, Value}) ->
247245
end}.
248246

249247
extract_oauth_providers_algorithm(Settings) ->
250-
KeyFun = fun extract_key_as_binary/1,
248+
KeyFun = fun extract_binary_key/1,
251249

252250
IndexedAlgorithms = [{Name, {Index, list_to_binary(V)}} ||
253251
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "algorithms", Index], V}
@@ -259,7 +257,7 @@ extract_oauth_providers_algorithm(Settings) ->
259257
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, Algorithms)).
260258

261259
extract_resource_server_preferred_username_claims(Settings) ->
262-
KeyFun = fun extract_key_as_binary/1,
260+
KeyFun = fun extract_binary_key/1,
263261

264262
IndexedClaims = [{Name, {Index, list_to_binary(V)}} ||
265263
{[?AUTH_OAUTH2, ?RESOURCE_SERVERS, Name, "preferred_username_claims",
@@ -271,7 +269,7 @@ extract_resource_server_preferred_username_claims(Settings) ->
271269
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, Claims)).
272270

273271
extract_oauth_providers_signing_keys(Settings) ->
274-
KeyFun = fun extract_key_as_binary/1,
272+
KeyFun = fun extract_binary_key/1,
275273

276274
IndexedSigningKeys = [{Name, {list_to_binary(Kid), list_to_binary(V)}} ||
277275
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, ?SIGNING_KEYS, Kid], V}

0 commit comments

Comments
 (0)