Search in sources :

Example 1 with JaasGrantedAuthority

use of org.springframework.security.authentication.jaas.JaasGrantedAuthority in project kylo by Teradata.

the class AboutKyloController method getCurrentUser.

/**
 * Gets information about the current user.
 */
@GET
@Path("/me")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Gets information about the current user.")
@ApiResponses(@ApiResponse(code = 200, message = "Returns the user.", response = User.class))
public Response getCurrentUser() {
    // Create principal from current user
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    final User user = new User();
    user.setEnabled(true);
    if (auth.getPrincipal() instanceof UserDetails) {
        final UserDetails details = (UserDetails) auth.getPrincipal();
        user.setGroups(details.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()));
        user.setSystemName(details.getUsername());
    } else {
        user.setGroups(auth.getAuthorities().stream().filter(JaasGrantedAuthority.class::isInstance).map(JaasGrantedAuthority.class::cast).filter(authority -> authority.getPrincipal() instanceof Group).map(JaasGrantedAuthority::getAuthority).collect(Collectors.toSet()));
        user.setSystemName(auth.getName());
    }
    // Return principal
    return Response.ok(user).build();
}
Also used : KyloVersionProvider(com.thinkbiganalytics.metadata.api.app.KyloVersionProvider) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ApiResponses(io.swagger.annotations.ApiResponses) Collectors(java.util.stream.Collectors) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) Component(org.springframework.stereotype.Component) MediaType(javax.ws.rs.core.MediaType) Response(javax.ws.rs.core.Response) Group(java.security.acl.Group) ApiResponse(io.swagger.annotations.ApiResponse) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(com.thinkbiganalytics.security.rest.model.User) Api(io.swagger.annotations.Api) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) KyloVersion(com.thinkbiganalytics.KyloVersion) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) Group(java.security.acl.Group) User(com.thinkbiganalytics.security.rest.model.User) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with JaasGrantedAuthority

use of org.springframework.security.authentication.jaas.JaasGrantedAuthority in project kylo by Teradata.

the class SecurityContextUtil method getCurrentPrincipals.

public static Set<Principal> getCurrentPrincipals() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Set<Principal> principals = new HashSet<>();
    if (auth != null) {
        for (GrantedAuthority grant : auth.getAuthorities()) {
            if (grant instanceof JaasGrantedAuthority) {
                JaasGrantedAuthority jaasGrant = (JaasGrantedAuthority) grant;
                principals.add(jaasGrant.getPrincipal());
            } else {
                String authority = grant.getAuthority();
                if (authority != null) {
                    principals.add(new SimplePrincipal(authority));
                }
            }
        }
        principals.add(new UsernamePrincipal(auth.getName()));
    } else {
        principals.add(new AnonymousPrincipal());
    }
    return principals;
}
Also used : UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) Authentication(org.springframework.security.core.Authentication) AnonymousPrincipal(com.thinkbiganalytics.security.AnonymousPrincipal) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) Principal(java.security.Principal) SimplePrincipal(com.thinkbiganalytics.security.SimplePrincipal) UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) AnonymousPrincipal(com.thinkbiganalytics.security.AnonymousPrincipal) SimplePrincipal(com.thinkbiganalytics.security.SimplePrincipal) HashSet(java.util.HashSet)

Example 3 with JaasGrantedAuthority

use of org.springframework.security.authentication.jaas.JaasGrantedAuthority in project kylo by Teradata.

the class DefaultKyloJaasAuthenticationProvider method createSubject.

private Subject createSubject(Authentication auth) {
    Set<Principal> principals = auth.getAuthorities().stream().filter(grant -> grant instanceof JaasGrantedAuthority).map(JaasGrantedAuthority.class::cast).map(jga -> jga.getPrincipal()).collect(Collectors.toCollection(HashSet::new));
    principals.add(new UsernamePrincipal(auth.getName()));
    Subject subject = Subject.getSubject(AccessController.getContext());
    if (subject == null) {
        return new Subject(false, principals, new HashSet<>(), new HashSet<>());
    } else {
        subject.getPrincipals().addAll(principals);
        return subject;
    }
}
Also used : LoginException(javax.security.auth.login.LoginException) LoggerFactory(org.slf4j.LoggerFactory) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) DefaultJaasAuthenticationProvider(org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProvider) Function(java.util.function.Function) LoginContext(javax.security.auth.login.LoginContext) HashSet(java.util.HashSet) LogLevel(com.thinkbiganalytics.logging.LoggingUtil.LogLevel) CallbackHandler(javax.security.auth.callback.CallbackHandler) AuthenticationException(org.springframework.security.core.AuthenticationException) SessionDestroyedEvent(org.springframework.security.core.session.SessionDestroyedEvent) JaasAuthenticationCallbackHandler(org.springframework.security.authentication.jaas.JaasAuthenticationCallbackHandler) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) Logger(org.slf4j.Logger) UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) JaasAuthenticationToken(org.springframework.security.authentication.jaas.JaasAuthenticationToken) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Subject(javax.security.auth.Subject) List(java.util.List) Principal(java.security.Principal) LoggingUtil(com.thinkbiganalytics.logging.LoggingUtil) SecurityContext(org.springframework.security.core.context.SecurityContext) Group(java.security.acl.Group) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Authentication(org.springframework.security.core.Authentication) AccessController(java.security.AccessController) Callback(javax.security.auth.callback.Callback) UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) UsernamePrincipal(com.thinkbiganalytics.security.UsernamePrincipal) Principal(java.security.Principal) Subject(javax.security.auth.Subject)

Aggregations

JaasGrantedAuthority (org.springframework.security.authentication.jaas.JaasGrantedAuthority)3 Authentication (org.springframework.security.core.Authentication)3 UsernamePrincipal (com.thinkbiganalytics.security.UsernamePrincipal)2 Principal (java.security.Principal)2 Group (java.security.acl.Group)2 HashSet (java.util.HashSet)2 Collectors (java.util.stream.Collectors)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 KyloVersion (com.thinkbiganalytics.KyloVersion)1 LoggingUtil (com.thinkbiganalytics.logging.LoggingUtil)1 LogLevel (com.thinkbiganalytics.logging.LoggingUtil.LogLevel)1 KyloVersionProvider (com.thinkbiganalytics.metadata.api.app.KyloVersionProvider)1 AnonymousPrincipal (com.thinkbiganalytics.security.AnonymousPrincipal)1 SimplePrincipal (com.thinkbiganalytics.security.SimplePrincipal)1 User (com.thinkbiganalytics.security.rest.model.User)1 Api (io.swagger.annotations.Api)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponse (io.swagger.annotations.ApiResponse)1 ApiResponses (io.swagger.annotations.ApiResponses)1 IOException (java.io.IOException)1