|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth2.server.resource.authentication;
|
18 | 18 |
|
| 19 | +import java.util.ArrayList; |
19 | 20 | import java.util.Arrays;
|
20 | 21 | import java.util.Collection;
|
21 |
| -import java.util.HashSet; |
22 | 22 | import java.util.LinkedHashSet;
|
23 | 23 |
|
24 | 24 | import org.springframework.core.convert.converter.Converter;
|
25 | 25 | import org.springframework.security.core.GrantedAuthority;
|
26 | 26 | import org.springframework.security.oauth2.jwt.Jwt;
|
| 27 | +import org.springframework.util.Assert; |
27 | 28 |
|
28 | 29 | /**
|
29 |
| - * Implementation of {@link Converter} that wraps multiple {@link Converter} instances into one. |
| 30 | + * A {@link Jwt} to {@link GrantedAuthority} {@link Converter} that is a composite of |
| 31 | + * converters. |
30 | 32 | *
|
31 | 33 | * @author Laszlo Stahorszki
|
| 34 | + * @author Josh Cummings |
32 | 35 | * @since 5.5
|
| 36 | + * @see org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter |
33 | 37 | */
|
34 | 38 | public class DelegatingJwtGrantedAuthoritiesConverter implements Converter<Jwt, Collection<GrantedAuthority>> {
|
35 | 39 |
|
36 |
| - private final Collection<Converter<Jwt, Collection<GrantedAuthority>>> converters = new HashSet<>(); |
| 40 | + private final Collection<Converter<Jwt, Collection<GrantedAuthority>>> authoritiesConverters; |
37 | 41 |
|
38 | 42 | /**
|
39 |
| - * Constructs a {@link DelegatingJwtGrantedAuthoritiesConverter} using the provided {@link Collection} of |
40 |
| - * {@link Converter}s |
41 |
| - * |
42 |
| - * @param converters the {@link Collection} of {@link Converter}s to use |
| 43 | + * Constructs a {@link DelegatingJwtGrantedAuthoritiesConverter} using the provided |
| 44 | + * {@link Collection} of {@link Converter}s |
| 45 | + * @param authoritiesConverters the {@link Collection} of {@link Converter}s to use |
43 | 46 | */
|
44 |
| - public DelegatingJwtGrantedAuthoritiesConverter(Collection<Converter<Jwt, Collection<GrantedAuthority>>> converters) { |
45 |
| - this.converters.addAll(converters); |
| 47 | + public DelegatingJwtGrantedAuthoritiesConverter( |
| 48 | + Collection<Converter<Jwt, Collection<GrantedAuthority>>> authoritiesConverters) { |
| 49 | + Assert.notNull(authoritiesConverters, "authoritiesConverters cannot be null"); |
| 50 | + this.authoritiesConverters = new ArrayList<>(authoritiesConverters); |
46 | 51 | }
|
47 | 52 |
|
48 | 53 | /**
|
49 |
| - * Constructs a {@link DelegatingJwtGrantedAuthoritiesConverter} using the provided array of |
50 |
| - * {@link Converter}s |
51 |
| - * |
52 |
| - * @param converters the array of {@link Converter}s to use |
| 54 | + * Constructs a {@link DelegatingJwtGrantedAuthoritiesConverter} using the provided |
| 55 | + * array of {@link Converter}s |
| 56 | + * @param authoritiesConverters the array of {@link Converter}s to use |
53 | 57 | */
|
54 | 58 | @SafeVarargs
|
55 |
| - public DelegatingJwtGrantedAuthoritiesConverter(Converter<Jwt, Collection<GrantedAuthority>>... converters) { |
56 |
| - this(Arrays.asList(converters)); |
| 59 | + public DelegatingJwtGrantedAuthoritiesConverter( |
| 60 | + Converter<Jwt, Collection<GrantedAuthority>>... authoritiesConverters) { |
| 61 | + this(Arrays.asList(authoritiesConverters)); |
57 | 62 | }
|
58 | 63 |
|
59 | 64 | /**
|
60 |
| - * Collects the {@link Collection} of authorities from the provided {@link Jwt} token. The method iterates through |
61 |
| - * all the {@link Converter}s provided during construction and returns the union of {@link GrantedAuthority}s |
62 |
| - * they extract. |
63 |
| - * @param source the source object to convert, which must be an instance of {@code S} (never {@code null}) |
64 |
| - * @return the converted object, which must be an instance of {@code T} (potentially {@code null}) |
65 |
| - * @throws IllegalArgumentException if the source cannot be converted to the desired target type |
| 65 | + * Extract {@link GrantedAuthority}s from the given {@link Jwt}. |
| 66 | + * <p> |
| 67 | + * The authorities are extracted from each delegated {@link Converter} one at a time. |
| 68 | + * For each converter, its authorities are added in order, with duplicates removed. |
| 69 | + * @param jwt The {@link Jwt} token |
| 70 | + * @return The {@link GrantedAuthority authorities} read from the token scopes |
66 | 71 | */
|
67 | 72 | @Override
|
68 |
| - public Collection<GrantedAuthority> convert(Jwt source) { |
| 73 | + public Collection<GrantedAuthority> convert(Jwt jwt) { |
69 | 74 | Collection<GrantedAuthority> result = new LinkedHashSet<>();
|
70 | 75 |
|
71 |
| - for (Converter<Jwt, Collection<GrantedAuthority>> converter: this.converters) { |
72 |
| - Collection<GrantedAuthority> authorities = converter.convert(source); |
| 76 | + for (Converter<Jwt, Collection<GrantedAuthority>> authoritiesConverter : this.authoritiesConverters) { |
| 77 | + Collection<GrantedAuthority> authorities = authoritiesConverter.convert(jwt); |
73 | 78 | if (authorities != null) {
|
74 | 79 | result.addAll(authorities);
|
75 | 80 | }
|
76 | 81 | }
|
77 | 82 |
|
78 | 83 | return result;
|
79 | 84 | }
|
| 85 | + |
80 | 86 | }
|
0 commit comments