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
1718extract_key_as_binary ({Name ,_ }) -> list_to_binary (Name ).
1819extract_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 ().
2163translate_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
106148mapOauthProviderProperty ({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 ,
0 commit comments