com.capitalone.dashboard.auth.DefaultAuthenticationResponseService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Core package shared by API layer and Microservices
package com.capitalone.dashboard.auth;
import com.capitalone.dashboard.auth.apitoken.ApiTokenAuthenticationToken;
import com.capitalone.dashboard.model.AuthType;
import com.capitalone.dashboard.model.UserRole;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@Component
public class DefaultAuthenticationResponseService implements AuthenticationResponseService {
@Override
public void handle(HttpServletResponse response, Authentication authentication) {
AuthType authType = (AuthType)authentication.getDetails();
if (authType == AuthType.APIKEY) {
Collection roles = new ArrayList<>();
roles.add(UserRole.ROLE_API);
AbstractAuthenticationToken authenticationWithAuthorities = new ApiTokenAuthenticationToken(authentication.getPrincipal(),
authentication.getCredentials(), createAuthorities(roles));
authenticationWithAuthorities.setDetails(authentication.getDetails());
}
}
private Collection extends GrantedAuthority> createAuthorities(Collection authorities) {
Collection grantedAuthorities = new HashSet<>();
authorities.forEach(authority -> grantedAuthorities.add(new SimpleGrantedAuthority(authority.name())));
return grantedAuthorities;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy