-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UUID Token AuthenticationProvider
- Loading branch information
Showing
2 changed files
with
52 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/cbioportal/security/token/uuid/UuidTokenAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.cbioportal.security.token.uuid; | ||
|
||
import org.cbioportal.model.UserAuthorities; | ||
import org.cbioportal.persistence.SecurityRepository; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
|
||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class UuidTokenAuthenticationProvider implements AuthenticationProvider { | ||
private static final Logger log = LoggerFactory.getLogger(UuidTokenAuthenticationProvider.class); | ||
|
||
private final SecurityRepository securityRepository; | ||
|
||
public UuidTokenAuthenticationProvider(final SecurityRepository securityRepository) { | ||
this.securityRepository = securityRepository; | ||
} | ||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
String user = (String) authentication.getPrincipal(); | ||
UserAuthorities authorities = securityRepository.getPortalUserAuthorities(user); | ||
Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); | ||
if (!Objects.isNull(authorities)) { | ||
mappedAuthorities.addAll(AuthorityUtils.createAuthorityList(authorities.getAuthorities())); | ||
} | ||
return new UsernamePasswordAuthenticationToken(user, "does not match unused", mappedAuthorities); | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return authentication.isAssignableFrom(UsernamePasswordAuthenticationToken.class); | ||
} | ||
} |