Search in sources :

Example 26 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project javaee7-samples by javaee-samples.

the class TestWrappingServerAuthModule method validateRequest.

@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
    try {
        handler.handle(new Callback[] { new CallerPrincipalCallback(clientSubject, "test"), new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) });
    } catch (IOException | UnsupportedCallbackException e) {
        throw (AuthException) new AuthException().initCause(e);
    }
    // Wrap the request - the resource to be invoked should get to see this
    messageInfo.setRequestMessage(new TestHttpServletRequestWrapper((HttpServletRequest) messageInfo.getRequestMessage()));
    // Wrap the response - the resource to be invoked should get to see this
    messageInfo.setResponseMessage(new TestHttpServletResponseWrapper((HttpServletResponse) messageInfo.getResponseMessage()));
    return SUCCESS;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) TestHttpServletRequestWrapper(org.javaee7.jaspic.wrapping.servlet.TestHttpServletRequestWrapper) AuthException(javax.security.auth.message.AuthException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TestHttpServletResponseWrapper(org.javaee7.jaspic.wrapping.servlet.TestHttpServletResponseWrapper)

Example 27 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project javaee7-samples by javaee-samples.

the class TestServerAuthModule method validateRequest.

@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
    HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
    Callback[] callbacks;
    if (request.getParameter("doLogin") != null) {
        callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, "test"), new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) };
    } else {
        // The JASPIC protocol for "do nothing"
        callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
    }
    try {
        handler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw (AuthException) new AuthException().initCause(e);
    }
    return SUCCESS;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) Callback(javax.security.auth.callback.Callback) AuthException(javax.security.auth.message.AuthException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 28 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project javaee7-samples by javaee-samples.

the class TestServerAuthModule method validateRequest.

@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
    HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
    Callback[] callbacks;
    if (request.getParameter("doLogin") != null) {
        // For the test perform a login by directly "returning" the details of the authenticated user.
        // Normally credentials would be checked and the details fetched from some repository
        callbacks = new Callback[] { // The name of the authenticated user
        new CallerPrincipalCallback(clientSubject, "test"), // the roles of the authenticated user
        new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) };
    } else {
        // The JASPIC protocol for "do nothing"
        callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
    }
    try {
        // Communicate the details of the authenticated user to the container. In many
        // cases the handler will just store the details and the container will actually handle
        // the login after we return from this method.
        handler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw (AuthException) new AuthException().initCause(e);
    }
    return SUCCESS;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) Callback(javax.security.auth.callback.Callback) AuthException(javax.security.auth.message.AuthException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 29 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project javaee7-samples by javaee-samples.

the class TestServerAuthModule method validateRequest.

@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
    HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
    Callback[] callbacks;
    if (request.getParameter("doLogin") != null) {
        // For the test perform a login by directly "returning" the details of the authenticated user.
        // Normally credentials would be checked and the details fetched from some repository
        callbacks = new Callback[] { // The name of the authenticated user
        new CallerPrincipalCallback(clientSubject, "test"), // the roles of the authenticated user
        new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) };
    } else {
        // The JASPIC protocol for "do nothing"
        callbacks = new Callback[] { new CallerPrincipalCallback(clientSubject, (Principal) null) };
    }
    try {
        // Communicate the details of the authenticated user to the container. In many
        // cases the handler will just store the details and the container will actually handle
        // the login after we return from this method.
        handler.handle(callbacks);
    } catch (IOException | UnsupportedCallbackException e) {
        throw (AuthException) new AuthException().initCause(e);
    }
    return SUCCESS;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) Callback(javax.security.auth.callback.Callback) AuthException(javax.security.auth.message.AuthException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Principal(java.security.Principal)

Example 30 with CallerPrincipalCallback

use of javax.security.auth.message.callback.CallerPrincipalCallback in project wildfly by wildfly.

the class SimpleServerAuthModule method validateRequest.

public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
    HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
    HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
    final String authType = request.getHeader(AUTH_TYPE_HEADER);
    final String username = request.getHeader(USERNAME_HEADER);
    final String password = request.getHeader(PASSWORD_HEADER);
    final String roles = request.getHeader(ROLES_HEADER);
    final String session = request.getHeader(SESSION_HEADER);
    if (username == null || username.length() == 0 || ((password == null || password.length() == 0) && !ANONYMOUS.equals(username))) {
        sendChallenge(response);
        return AuthStatus.SEND_CONTINUE;
    }
    final boolean validated;
    if ("anonymous".equals(username)) {
        // Skip Authentication.
        validated = true;
    } else if (selfValidating) {
        // In this mode the ServerAuthModule is taking over it's own validation and only using Callbacks to establish the identity.
        validated = "user1".equals(username) && "password1".equals(password);
    } else {
        PasswordValidationCallback pvc = new PasswordValidationCallback(serviceSubject, username, password.toCharArray());
        try {
            handle(pvc);
        } finally {
            pvc.clearPassword();
        }
        validated = pvc.getResult();
    }
    if (validated) {
        if ("anonymous".equals(username)) {
            handle(new CallerPrincipalCallback(clientSubject, (Principal) null));
        } else {
            handle(new CallerPrincipalCallback(clientSubject, new NamePrincipal(username)));
        }
        if (roles != null) {
            handle(new GroupPrincipalCallback(clientSubject, roles.split(",")));
        }
        if (defaultRoles != null) {
            handle(new GroupPrincipalCallback(clientSubject, defaultRoles));
        }
        Map map = messageInfo.getMap();
        if (authType != null) {
            map.put(AUTH_TYPE, authType);
        }
        if ("register".equals(session)) {
            System.out.println("Requesting session registration");
            map.put(SESSION, Boolean.TRUE.toString());
        }
        return AuthStatus.SUCCESS;
    } else {
        // It is a failure as authentication was deliberately attempted and the supplied username / password failed validation.
        sendChallenge(response);
        return AuthStatus.SEND_FAILURE;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CallerPrincipalCallback(javax.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(javax.security.auth.message.callback.GroupPrincipalCallback) NamePrincipal(org.wildfly.security.auth.principal.NamePrincipal) HttpServletResponse(javax.servlet.http.HttpServletResponse) PasswordValidationCallback(javax.security.auth.message.callback.PasswordValidationCallback) Map(java.util.Map) NamePrincipal(org.wildfly.security.auth.principal.NamePrincipal) Principal(java.security.Principal)

Aggregations

CallerPrincipalCallback (javax.security.auth.message.callback.CallerPrincipalCallback)30 GroupPrincipalCallback (javax.security.auth.message.callback.GroupPrincipalCallback)24 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)21 IOException (java.io.IOException)18 Principal (java.security.Principal)17 AuthException (javax.security.auth.message.AuthException)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)16 Callback (javax.security.auth.callback.Callback)14 PasswordValidationCallback (javax.security.auth.message.callback.PasswordValidationCallback)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Subject (javax.security.auth.Subject)4 CertStoreCallback (javax.security.auth.message.callback.CertStoreCallback)3 PrivateKeyCallback (javax.security.auth.message.callback.PrivateKeyCallback)3 SecretKeyCallback (javax.security.auth.message.callback.SecretKeyCallback)3 TrustStoreCallback (javax.security.auth.message.callback.TrustStoreCallback)3 Map (java.util.Map)2 HttpSession (javax.servlet.http.HttpSession)2 LoginCallbackImpl (org.eclipse.jetty.security.authentication.LoginCallbackImpl)2 CredentialValidationCallback (org.eclipse.jetty.security.jaspi.callback.CredentialValidationCallback)2 UserIdentity (org.eclipse.jetty.server.UserIdentity)2