|
30 | 30 |
|
31 | 31 | -import(rabbit_oauth2_rar, [extract_scopes_from_rich_auth_request/2]). |
32 | 32 |
|
33 | | --import(rabbit_oauth2_scope, [filter_matching_scope_prefix_and_drop_it/2]). |
| 33 | +-import(rabbit_oauth2_scope, [ |
| 34 | + filter_matching_scope_prefix/2, |
| 35 | + filter_matching_scope_prefix_and_drop_it/2]). |
34 | 36 |
|
35 | 37 | -ifdef(TEST). |
36 | 38 | -compile(export_all). |
@@ -240,15 +242,30 @@ extract_scopes_from_scope_claim(Payload) -> |
240 | 242 | -spec normalize_token_scope( |
241 | 243 | ResourceServer :: resource_server(), DecodedToken :: decoded_jwt_token()) -> map(). |
242 | 244 | normalize_token_scope(ResourceServer, Payload) -> |
243 | | - |
244 | | - Payload1 = extract_scopes_from_rich_auth_request(ResourceServer, |
245 | | - extract_scopes_using_scope_aliases(ResourceServer, |
246 | | - extract_scopes_from_additional_scopes_key(ResourceServer, |
247 | | - extract_scopes_from_scope_claim(Payload)))), |
248 | 245 |
|
249 | | - FilteredScopes = filter_matching_scope_prefix_and_drop_it( |
250 | | - get_scope(Payload1), ResourceServer#resource_server.scope_prefix), |
251 | | - set_scope(FilteredScopes, Payload1). |
| 246 | + filter_duplicates( |
| 247 | + filter_matching_scope_prefix(ResourceServer, |
| 248 | + extract_scopes_from_rich_auth_request(ResourceServer, |
| 249 | + extract_scopes_using_scope_aliases(ResourceServer, |
| 250 | + extract_scopes_from_additional_scopes_key(ResourceServer, |
| 251 | + extract_scopes_from_requesting_party_token(ResourceServer, |
| 252 | + extract_scopes_from_scope_claim(Payload))))))). |
| 253 | + |
| 254 | +filter_duplicates(#{?SCOPE_JWT_FIELD := Scopes} = Payload) -> |
| 255 | + set_scope(lists:usort(Scopes), Payload); |
| 256 | +filter_duplicates(Payload) -> Payload. |
| 257 | + |
| 258 | +-spec extract_scopes_from_requesting_party_token( |
| 259 | + ResourceServer :: resource_server(), DecodedToken :: decoded_jwt_token()) -> map(). |
| 260 | +extract_scopes_from_requesting_party_token(ResourceServer, Payload) -> |
| 261 | + Path = ?SCOPES_LOCATION_IN_REQUESTING_PARTY_TOKEN, |
| 262 | + case extract_token_value(ResourceServer, Payload, Path, |
| 263 | + fun extract_scope_list_from_token_value/2) of |
| 264 | + [] -> |
| 265 | + Payload; |
| 266 | + AdditionalScopes -> |
| 267 | + set_scope(lists:flatten(AdditionalScopes) ++ get_scope(Payload), Payload) |
| 268 | + end. |
252 | 269 |
|
253 | 270 | -spec extract_scopes_using_scope_aliases( |
254 | 271 | ResourceServer :: resource_server(), Payload :: map()) -> map(). |
@@ -322,9 +339,9 @@ extract_token_value_from_map(R, Map, Acc, [KeyStr | Rest], Mapper) when is_map(M |
322 | 339 | {ok, L} when is_list(L) -> extract_token_value_from_list(R, L, Acc, Rest, Mapper); |
323 | 340 | {ok, Value} when Rest =:= [] -> Acc ++ Mapper(R, Value); |
324 | 341 | _ -> Acc |
325 | | - end; |
326 | | -extract_token_value_from_map(_, _, Acc, _, _Mapper) -> |
327 | | - Acc. |
| 342 | + end. |
| 343 | +%extract_token_value_from_map(_, _, Acc, _, _Mapper) -> |
| 344 | +% Acc. |
328 | 345 |
|
329 | 346 | extract_token_value_from_list(_, [], Acc, [], _Mapper) -> |
330 | 347 | Acc; |
@@ -355,35 +372,13 @@ split_path(Path) when is_binary(Path) -> |
355 | 372 | ResourceServer :: resource_server(), Payload :: map()) -> map(). |
356 | 373 | extract_scopes_from_additional_scopes_key( |
357 | 374 | #resource_server{additional_scopes_key = Key} = ResourceServer, Payload) |
358 | | - when is_list(Key) or is_binary(Key) -> |
359 | | - Paths = case Key of |
360 | | - B when is_binary(B) -> binary:split(B, <<" ">>, [global, trim_all]); |
361 | | - L when is_list(L) -> L |
362 | | - end, |
| 375 | + when is_binary(Key) -> |
| 376 | + Paths = binary:split(Key, <<" ">>, [global, trim_all]), |
363 | 377 | AdditionalScopes = [ extract_token_value(ResourceServer, |
364 | 378 | Payload, Path, fun extract_scope_list_from_token_value/2) || Path <- Paths], |
365 | 379 | set_scope(lists:flatten(AdditionalScopes) ++ get_scope(Payload), Payload); |
366 | 380 | extract_scopes_from_additional_scopes_key(_, Payload) -> Payload. |
367 | 381 |
|
368 | | -extract_additional_scopes(ResourceServer, ComplexClaim) -> |
369 | | - ResourceServerId = ResourceServer#resource_server.id, |
370 | | - case ComplexClaim of |
371 | | - L when is_list(L) -> L; |
372 | | - M when is_map(M) -> |
373 | | - case maps:get(ResourceServerId, M, undefined) of |
374 | | - undefined -> []; |
375 | | - Ks when is_list(Ks) -> |
376 | | - [erlang:iolist_to_binary([ResourceServerId, <<".">>, K]) || K <- Ks]; |
377 | | - ClaimBin when is_binary(ClaimBin) -> |
378 | | - UnprefixedClaims = binary:split(ClaimBin, <<" ">>, [global, trim_all]), |
379 | | - [erlang:iolist_to_binary([ResourceServerId, <<".">>, K]) || K <- UnprefixedClaims]; |
380 | | - _ -> [] |
381 | | - end; |
382 | | - Bin when is_binary(Bin) -> |
383 | | - binary:split(Bin, <<" ">>, [global, trim_all]); |
384 | | - _ -> [] |
385 | | - end. |
386 | | - |
387 | 382 |
|
388 | 383 | %% A token may be present in the password credential or in the rabbit_auth_backend_oauth2 |
389 | 384 | %% credential. The former is the most common scenario for the first time authentication. |
|
0 commit comments