Skip to content

Commit d87bdb2

Browse files
committed
added ROLE_CLIENT to assertion client authentication, cleaned up roles on client secret authentication, closes mitreid-connect#728, closes mitreid-connect#401
1 parent 34afe21 commit d87bdb2

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

openid-connect-common/src/main/java/org/mitre/oauth2/service/impl/DefaultClientUserDetailsService.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import java.math.BigInteger;
2020
import java.security.SecureRandom;
21-
import java.util.ArrayList;
2221
import java.util.Collection;
22+
import java.util.HashSet;
2323

2424
import org.mitre.oauth2.model.ClientDetailsEntity;
2525
import org.mitre.oauth2.model.ClientDetailsEntity.AuthMethod;
@@ -44,6 +44,8 @@
4444
@Service("clientUserDetailsService")
4545
public class DefaultClientUserDetailsService implements UserDetailsService {
4646

47+
private static GrantedAuthority ROLE_CLIENT = new SimpleGrantedAuthority("ROLE_CLIENT");
48+
4749
@Autowired
4850
private ClientDetailsEntityService clientDetailsService;
4951

@@ -70,14 +72,8 @@ public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundE
7072
boolean accountNonExpired = true;
7173
boolean credentialsNonExpired = true;
7274
boolean accountNonLocked = true;
73-
Collection<GrantedAuthority> authorities = client.getAuthorities();
74-
if (authorities == null || authorities.isEmpty()) {
75-
// automatically inject ROLE_CLIENT if none exists ...
76-
// TODO: this should probably happen on the client service side instead to keep it in the real data model
77-
authorities = new ArrayList<GrantedAuthority>();
78-
GrantedAuthority roleClient = new SimpleGrantedAuthority("ROLE_CLIENT");
79-
authorities.add(roleClient);
80-
}
75+
Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(client.getAuthorities());
76+
authorities.add(ROLE_CLIENT);
8177

8278
return new User(clientId, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
8379
} else {

openid-connect-server/src/main/java/org/mitre/openid/connect/assertion/JwtBearerAuthenticationProvider.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.text.ParseException;
2323
import java.util.Date;
24+
import java.util.HashSet;
25+
import java.util.Set;
2426

2527
import org.mitre.jwt.signer.service.JwtSigningAndValidationService;
2628
import org.mitre.jwt.signer.service.impl.JWKSetCacheService;
@@ -36,6 +38,8 @@
3638
import org.springframework.security.authentication.AuthenticationServiceException;
3739
import org.springframework.security.core.Authentication;
3840
import org.springframework.security.core.AuthenticationException;
41+
import org.springframework.security.core.GrantedAuthority;
42+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3943
import org.springframework.security.core.userdetails.UsernameNotFoundException;
4044
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
4145

@@ -52,6 +56,8 @@ public class JwtBearerAuthenticationProvider implements AuthenticationProvider {
5256

5357
private static final Logger logger = LoggerFactory.getLogger(JwtBearerAuthenticationProvider.class);
5458

59+
private static final GrantedAuthority ROLE_CLIENT = new SimpleGrantedAuthority("ROLE_CLIENT");
60+
5561
// map of verifiers, load keys for clients
5662
@Autowired
5763
private JWKSetCacheService validators;
@@ -182,7 +188,12 @@ public Authentication authenticate(Authentication authentication) throws Authent
182188
}
183189

184190
// IFF we managed to get all the way down here, the token is valid
185-
return new JwtBearerAssertionAuthenticationToken(client.getClientId(), jwt, client.getAuthorities());
191+
192+
// add in the ROLE_CLIENT authority
193+
Set<GrantedAuthority> authorities = new HashSet<>(client.getAuthorities());
194+
authorities.add(ROLE_CLIENT);
195+
196+
return new JwtBearerAssertionAuthenticationToken(client.getClientId(), jwt, authorities);
186197

187198
} catch (InvalidClientException e) {
188199
throw new UsernameNotFoundException("Could not find client: " + jwtAuth.getClientId());

0 commit comments

Comments
 (0)