Search in sources :

Example 1 with UserPrincipal

use of com.sun.security.auth.UserPrincipal in project simba-os by cegeka.

the class SimbaLoginModule method commit.

@Override
public boolean commit() throws LoginException {
    if (!succeeded) {
        return false;
    }
    userPrincipal = new UserPrincipal(username);
    getSubject().getPrincipals().add(userPrincipal);
    commitSucceeded = true;
    username = null;
    password = null;
    return true;
}
Also used : UserPrincipal(com.sun.security.auth.UserPrincipal)

Example 2 with UserPrincipal

use of com.sun.security.auth.UserPrincipal in project simba-os by cegeka.

the class SimbaJAXWSHandler method handleMessage.

@Override
public boolean handleMessage(final SOAPMessageContext context) {
    if (isInboundMessage(context)) {
        try {
            final SOAPHeader header = context.getMessage().getSOAPHeader();
            final HttpServletRequest httpServletRequest = (HttpServletRequest) context.get(MessageContext.SERVLET_REQUEST);
            final ServletContext servletContext = (ServletContext) context.get(MessageContext.SERVLET_CONTEXT);
            final RequestData requestData = RequestUtil.createWSSERequestData(httpServletRequest, header, getSimbaWebURL(servletContext));
            THttpClient tHttpClient = null;
            try {
                tHttpClient = new THttpClient(SimbaConfiguration.getSimbaAuthenticationURL());
                TProtocol tProtocol = new TJSONProtocol(tHttpClient);
                AuthenticationFilterService.Client authenticationClient = new AuthenticationFilterService.Client(tProtocol);
                ActionDescriptor actionDescriptor = authenticationClient.processRequest(requestData, "wsLoginChain");
                if (!actionDescriptor.getActionTypes().contains(ActionType.DO_FILTER_AND_SET_PRINCIPAL)) {
                    throw new SimbaWSAuthenticationException("Authentication Failed");
                }
                String username = actionDescriptor.getPrincipal();
                Principal principal = null;
                if (username != null) {
                    principal = new UserPrincipal(username);
                }
                if (principal != null) {
                    context.put(SimbaPrincipal.SIMBA_USER_CTX_KEY, principal);
                    context.setScope(SimbaPrincipal.SIMBA_USER_CTX_KEY, MessageContext.Scope.APPLICATION);
                }
            } finally {
                if (tHttpClient != null) {
                    tHttpClient.close();
                }
            }
        } catch (Exception e) {
            throw new SimbaWSAuthenticationException("Authentication Failed", e);
        }
    }
    return true;
}
Also used : AuthenticationFilterService(org.simbasecurity.api.service.thrift.AuthenticationFilterService) ActionDescriptor(org.simbasecurity.api.service.thrift.ActionDescriptor) THttpClient(org.apache.thrift.transport.THttpClient) UserPrincipal(com.sun.security.auth.UserPrincipal) HttpServletRequest(javax.servlet.http.HttpServletRequest) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) RequestData(org.simbasecurity.api.service.thrift.RequestData) ServletContext(javax.servlet.ServletContext) THttpClient(org.apache.thrift.transport.THttpClient) SOAPHeader(javax.xml.soap.SOAPHeader) SimbaPrincipal(org.simbasecurity.client.principal.SimbaPrincipal) UserPrincipal(com.sun.security.auth.UserPrincipal) Principal(java.security.Principal)

Example 3 with UserPrincipal

use of com.sun.security.auth.UserPrincipal in project jdk8u_jdk by JetBrains.

the class LdapLoginModule method attemptAuthentication.

/**
     * Attempt authentication
     *
     * @param getPasswdFromSharedState boolean that tells this method whether
     *          to retrieve the password from the sharedState.
     * @exception LoginException if the authentication attempt fails.
     */
private void attemptAuthentication(boolean getPasswdFromSharedState) throws LoginException {
    // first get the username and password
    getUsernamePassword(getPasswdFromSharedState);
    if (password == null || password.length == 0) {
        throw (LoginException) new FailedLoginException("No password was supplied");
    }
    String dn = "";
    if (authFirst || authOnly) {
        String id = replaceUsernameToken(identityMatcher, authcIdentity);
        // Prepare to bind using user's username and password
        ldapEnvironment.put(Context.SECURITY_CREDENTIALS, password);
        ldapEnvironment.put(Context.SECURITY_PRINCIPAL, id);
        if (debug) {
            System.out.println("\t\t[LdapLoginModule] " + "attempting to authenticate user: " + username);
        }
        try {
            // Connect to the LDAP server (using simple bind)
            ctx = new InitialLdapContext(ldapEnvironment, null);
        } catch (NamingException e) {
            throw (LoginException) new FailedLoginException("Cannot bind to LDAP server").initCause(e);
        }
        // Locate the user's distinguished name
        if (userFilter != null) {
            dn = findUserDN(ctx);
        } else {
            dn = id;
        }
    } else {
        try {
            // Connect to the LDAP server (using anonymous bind)
            ctx = new InitialLdapContext(ldapEnvironment, null);
        } catch (NamingException e) {
            throw (LoginException) new FailedLoginException("Cannot connect to LDAP server").initCause(e);
        }
        // Locate the user's distinguished name
        dn = findUserDN(ctx);
        try {
            // Prepare to bind using user's distinguished name and password
            ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
            ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
            if (debug) {
                System.out.println("\t\t[LdapLoginModule] " + "attempting to authenticate user: " + username);
            }
            // Connect to the LDAP server (using simple bind)
            ctx.reconnect(null);
        // Authentication has succeeded
        } catch (NamingException e) {
            throw (LoginException) new FailedLoginException("Cannot bind to LDAP server").initCause(e);
        }
    }
    // Save input as shared state only if authentication succeeded
    if (storePass && !sharedState.containsKey(USERNAME_KEY) && !sharedState.containsKey(PASSWORD_KEY)) {
        sharedState.put(USERNAME_KEY, username);
        sharedState.put(PASSWORD_KEY, password);
    }
    // Create the user principals
    userPrincipal = new UserPrincipal(username);
    if (authzIdentity != null) {
        authzPrincipal = new UserPrincipal(authzIdentity);
    }
    try {
        ldapPrincipal = new LdapPrincipal(dn);
    } catch (InvalidNameException e) {
        if (debug) {
            System.out.println("\t\t[LdapLoginModule] " + "cannot create LdapPrincipal: bad DN");
        }
        throw (LoginException) new FailedLoginException("Cannot create LdapPrincipal").initCause(e);
    }
}
Also used : LdapPrincipal(com.sun.security.auth.LdapPrincipal) UserPrincipal(com.sun.security.auth.UserPrincipal)

Example 4 with UserPrincipal

use of com.sun.security.auth.UserPrincipal in project simba-os by cegeka.

the class DoFilterAndSetPrincipalAction method execute.

@Override
public void execute() throws ServletException, IOException {
    String username = getActionDescriptor().getPrincipal();
    Principal principal = null;
    if (username != null) {
        principal = new UserPrincipal(username);
    }
    if (principal != null) {
        request = new HttpServletRequestWithPrincipal(request, principal);
    }
    if (doFilterExtension != null)
        doFilterExtension.before();
    try {
        filterChain.doFilter(request, response);
    } finally {
        if (doFilterExtension != null)
            doFilterExtension.after();
    }
}
Also used : HttpServletRequestWithPrincipal(org.simbasecurity.client.filter.request.HttpServletRequestWithPrincipal) Principal(java.security.Principal) HttpServletRequestWithPrincipal(org.simbasecurity.client.filter.request.HttpServletRequestWithPrincipal) UserPrincipal(com.sun.security.auth.UserPrincipal) UserPrincipal(com.sun.security.auth.UserPrincipal)

Example 5 with UserPrincipal

use of com.sun.security.auth.UserPrincipal in project polymap4-core by Polymap4.

the class LdapLoginModule method commit.

@Override
public boolean commit() throws LoginException {
    if (super.commit()) {
        for (UserPrincipal principal : subject.getPrincipals(UserPrincipal.class)) {
            log.info("principal: " + principal);
            org.polymap.core.security.UserPrincipal user = new org.polymap.core.security.UserPrincipal(principal.getName()) {

                public String getPassword() {
                    // XXX Auto-generated method stub
                    throw new RuntimeException("not yet implemented.");
                }
            };
            subject.getPrincipals().add(user);
            subject.getPrivateCredentials().add(this);
            subject.getPrivateCredentials().add(authModule);
        }
        return true;
    }
    return false;
}
Also used : UserPrincipal(com.sun.security.auth.UserPrincipal)

Aggregations

UserPrincipal (com.sun.security.auth.UserPrincipal)5 Principal (java.security.Principal)2 LdapPrincipal (com.sun.security.auth.LdapPrincipal)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 SOAPHeader (javax.xml.soap.SOAPHeader)1 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)1 TProtocol (org.apache.thrift.protocol.TProtocol)1 THttpClient (org.apache.thrift.transport.THttpClient)1 ActionDescriptor (org.simbasecurity.api.service.thrift.ActionDescriptor)1 AuthenticationFilterService (org.simbasecurity.api.service.thrift.AuthenticationFilterService)1 RequestData (org.simbasecurity.api.service.thrift.RequestData)1 HttpServletRequestWithPrincipal (org.simbasecurity.client.filter.request.HttpServletRequestWithPrincipal)1 SimbaPrincipal (org.simbasecurity.client.principal.SimbaPrincipal)1