|
1 | 1 | package eu.openanalytics.containerproxy.auth.impl.saml; |
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
4 | 5 | import java.util.Collection; |
5 | | -import java.util.Collections; |
6 | 6 | import java.util.List; |
7 | 7 | import java.util.Timer; |
8 | 8 |
|
|
23 | 23 | import org.springframework.context.annotation.Lazy; |
24 | 24 | import org.springframework.core.env.Environment; |
25 | 25 | import org.springframework.security.authentication.AuthenticationManager; |
| 26 | +import org.springframework.security.core.GrantedAuthority; |
| 27 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
26 | 28 | import org.springframework.security.core.userdetails.User; |
27 | 29 | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
28 | 30 | import org.springframework.security.saml.SAMLAuthenticationProvider; |
|
63 | 65 | @ConditionalOnProperty(name="proxy.authentication", havingValue="saml") |
64 | 66 | public class SAMLConfiguration { |
65 | 67 |
|
| 68 | + private static final String DEFAULT_NAME_ATTRIBUTE = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"; |
| 69 | + |
66 | 70 | @Inject |
67 | 71 | private Environment environment; |
68 | 72 |
|
@@ -254,10 +258,22 @@ public SAMLAuthenticationProvider samlAuthenticationProvider() { |
254 | 258 | samlAuthenticationProvider.setUserDetails(new SAMLUserDetailsService() { |
255 | 259 | @Override |
256 | 260 | public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException { |
257 | | - //TODO The claim to use as username should be configurable |
258 | | - String claimName = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"; |
259 | | - String claimValue = credential.getAttributeAsString(claimName); |
260 | | - return new User(claimValue, "", Collections.emptyList()); |
| 261 | + String nameAttribute = environment.getProperty("proxy.saml.name-attribute", DEFAULT_NAME_ATTRIBUTE); |
| 262 | + String nameValue = credential.getAttributeAsString(nameAttribute); |
| 263 | + if (nameValue == null) throw new UsernameNotFoundException("Name attribute missing from SAML assertion: " + nameAttribute); |
| 264 | + |
| 265 | + List<GrantedAuthority> auth = new ArrayList<>(); |
| 266 | + String rolesAttribute = environment.getProperty("proxy.saml.roles-attribute"); |
| 267 | + if (rolesAttribute != null && !rolesAttribute.trim().isEmpty()) { |
| 268 | + String[] roles = credential.getAttributeAsStringArray(rolesAttribute); |
| 269 | + if (roles != null && roles.length > 0) { |
| 270 | + Arrays.stream(roles) |
| 271 | + .map(r -> "ROLE_" + r.toUpperCase()) |
| 272 | + .forEach(a -> auth.add(new SimpleGrantedAuthority(a))); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + return new User(nameValue, "", auth); |
261 | 277 | } |
262 | 278 | }); |
263 | 279 | samlAuthenticationProvider.setForcePrincipalAsString(false); |
|
0 commit comments