Skip to content

Commit b0d4e50

Browse files
committed
Polish Add DelegatingJwtGrantedAuthoritiesConverter
- Adjusted internal logic to follow DelegatingOAuth2TokenValidator - Changed JavaDoc to align more closely with JwtGrantedAuthoritiesConverter - Polished test names to follow Spring Security naming convention - Updated test class name to follow Spring Security naming convention - Polished tests to use TestJwts - Added tests to address additional use cases Closes gh-7596
1 parent 97cc119 commit b0d4e50

File tree

3 files changed

+112
-103
lines changed

3 files changed

+112
-103
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/DelegatingJwtGrantedAuthoritiesConverter.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,71 @@
1616

1717
package org.springframework.security.oauth2.server.resource.authentication;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collection;
21-
import java.util.HashSet;
2222
import java.util.LinkedHashSet;
2323

2424
import org.springframework.core.convert.converter.Converter;
2525
import org.springframework.security.core.GrantedAuthority;
2626
import org.springframework.security.oauth2.jwt.Jwt;
27+
import org.springframework.util.Assert;
2728

2829
/**
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.
3032
*
3133
* @author Laszlo Stahorszki
34+
* @author Josh Cummings
3235
* @since 5.5
36+
* @see org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter
3337
*/
3438
public class DelegatingJwtGrantedAuthoritiesConverter implements Converter<Jwt, Collection<GrantedAuthority>> {
3539

36-
private final Collection<Converter<Jwt, Collection<GrantedAuthority>>> converters = new HashSet<>();
40+
private final Collection<Converter<Jwt, Collection<GrantedAuthority>>> authoritiesConverters;
3741

3842
/**
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
4346
*/
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);
4651
}
4752

4853
/**
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
5357
*/
5458
@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));
5762
}
5863

5964
/**
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
6671
*/
6772
@Override
68-
public Collection<GrantedAuthority> convert(Jwt source) {
73+
public Collection<GrantedAuthority> convert(Jwt jwt) {
6974
Collection<GrantedAuthority> result = new LinkedHashSet<>();
7075

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);
7378
if (authorities != null) {
7479
result.addAll(authorities);
7580
}
7681
}
7782

7883
return result;
7984
}
85+
8086
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/DelegatingJwtGrantedAuthoritiesConverterTest.java

Lines changed: 0 additions & 79 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.server.resource.authentication;
18+
19+
import java.util.Collection;
20+
import java.util.LinkedHashSet;
21+
22+
import org.junit.Test;
23+
24+
import org.springframework.core.convert.converter.Converter;
25+
import org.springframework.security.core.GrantedAuthority;
26+
import org.springframework.security.core.authority.AuthorityUtils;
27+
import org.springframework.security.oauth2.jwt.Jwt;
28+
import org.springframework.security.oauth2.jwt.TestJwts;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
32+
33+
/**
34+
* Tests for verifying {@link DelegatingJwtGrantedAuthoritiesConverter}
35+
*
36+
* @author Laszlo Stahorszki
37+
* @author Josh Cummings
38+
*/
39+
public class DelegatingJwtGrantedAuthoritiesConverterTests {
40+
41+
@Test
42+
public void convertWhenNoConvertersThenNoAuthorities() {
43+
DelegatingJwtGrantedAuthoritiesConverter converter = new DelegatingJwtGrantedAuthoritiesConverter();
44+
Jwt jwt = TestJwts.jwt().build();
45+
assertThat(converter.convert(jwt)).isEmpty();
46+
}
47+
48+
@Test
49+
public void convertWhenConverterThenAuthorities() {
50+
DelegatingJwtGrantedAuthoritiesConverter converter = new DelegatingJwtGrantedAuthoritiesConverter(
51+
((jwt) -> AuthorityUtils.createAuthorityList("one")));
52+
Jwt jwt = TestJwts.jwt().build();
53+
Collection<GrantedAuthority> authorities = converter.convert(jwt);
54+
assertThat(authorityListToOrderedSet(authorities)).containsExactly("one");
55+
}
56+
57+
@Test
58+
public void convertWhenMultipleConvertersThenDuplicatesRemoved() {
59+
Converter<Jwt, Collection<GrantedAuthority>> one = (jwt) -> AuthorityUtils.createAuthorityList("one", "two");
60+
Converter<Jwt, Collection<GrantedAuthority>> two = (jwt) -> AuthorityUtils.createAuthorityList("one", "three");
61+
DelegatingJwtGrantedAuthoritiesConverter composite = new DelegatingJwtGrantedAuthoritiesConverter(one, two);
62+
Jwt jwt = TestJwts.jwt().build();
63+
Collection<GrantedAuthority> authorities = composite.convert(jwt);
64+
assertThat(authorityListToOrderedSet(authorities)).containsExactly("one", "two", "three");
65+
}
66+
67+
@Test
68+
public void constructorWhenAuthoritiesConverterIsNullThenIllegalArgumentException() {
69+
assertThatExceptionOfType(IllegalArgumentException.class)
70+
.isThrownBy(() -> new DelegatingJwtGrantedAuthoritiesConverter(
71+
(Collection<Converter<Jwt, Collection<GrantedAuthority>>>) null));
72+
}
73+
74+
private Collection<String> authorityListToOrderedSet(Collection<GrantedAuthority> grantedAuthorities) {
75+
Collection<String> authorities = new LinkedHashSet<>(grantedAuthorities.size());
76+
for (GrantedAuthority authority : grantedAuthorities) {
77+
authorities.add(authority.getAuthority());
78+
}
79+
return authorities;
80+
}
81+
82+
}

0 commit comments

Comments
 (0)