Search in sources :

Example 16 with PasswordCredential

use of com.sun.enterprise.security.auth.login.common.PasswordCredential in project Payara by payara.

the class LoginContextDriver method doPasswordLogin.

/**
 * Log in subject with PasswordCredential. This is a generic login
 * which applies to all login mechanisms which process PasswordCredential.
 * In other words, any mechanism which receives an actual username, realm
 * and password set from the client.
 *
 * <P>The realm contained in the credential is checked, and a JAAS
 * LoginContext is created using a context name obtained from the
 * appropriate Realm instance. The applicable JAAS LoginModule
 * is initialized (based on the jaas login configuration) and login()
 * is invoked on it.
 *
 * <P>RI code makes several assumptions which are retained here:
 * <ul>
 *  <li>The PasswordCredential is stored as a private credential of
 *      the subject.
 *  <li>There is only one such credential present (actually, only
 *      the first one is relevant if more are present).
 * </ui>
 *
 * @param s Subject to be authenticated.
 * @throws LoginException Thrown if the login fails.
 */
private static void doPasswordLogin(Subject subject) throws LoginException {
    final Subject s = subject;
    Object obj = getPrivateCredentials(s, PasswordCredential.class);
    assert obj != null;
    PasswordCredential p = (PasswordCredential) obj;
    String user = p.getUser();
    char[] pwd = p.getPassword();
    String realm = p.getRealm();
    String jaasCtx = null;
    try {
        jaasCtx = Realm.getInstance(realm).getJAASContext();
    } catch (Exception ex) {
        if (ex instanceof LoginException)
            throw (LoginException) ex;
        else
            throw (LoginException) new LoginException(ex.toString()).initCause(ex);
    }
    assert user != null;
    assert pwd != null;
    assert realm != null;
    assert jaasCtx != null;
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("Logging in user [" + user + "] into realm: " + realm + " using JAAS module: " + jaasCtx);
    }
    try {
        // A dummyCallback is used to satisfy JAAS but it is never used.
        // name/pwd info is already contained in Subject's Credential
        LoginContext lg = new LoginContext(jaasCtx, s, dummyCallback);
        lg.login();
    } catch (Exception e) {
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.log(Level.FINEST, "doPasswordLogin fails", e);
        }
        if (getAuditManager() != null && getAuditManager().isAuditOn()) {
            getAuditManager().authentication(user, realm, false);
        }
        if (e instanceof LoginException)
            throw (LoginException) e;
        else
            throw (LoginException) new LoginException("Login failed: " + e.getMessage()).initCause(e);
    }
    if (getAuditManager() != null && getAuditManager().isAuditOn()) {
        getAuditManager().authentication(user, realm, true);
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("Password login succeeded for : " + user);
    }
    setSecurityContext(user, s, realm);
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "Set security context as user: " + user);
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) PasswordCredential(com.sun.enterprise.security.auth.login.common.PasswordCredential) LoginException(com.sun.enterprise.security.auth.login.common.LoginException) Subject(javax.security.auth.Subject) LoginException(com.sun.enterprise.security.auth.login.common.LoginException) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) InvalidOperationException(com.sun.enterprise.security.auth.realm.InvalidOperationException) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException)

Aggregations

PasswordCredential (com.sun.enterprise.security.auth.login.common.PasswordCredential)16 Subject (javax.security.auth.Subject)11 LoginException (com.sun.enterprise.security.auth.login.common.LoginException)7 PrivilegedAction (java.security.PrivilegedAction)7 Set (java.util.Set)6 X509CertificateCredential (com.sun.enterprise.security.auth.login.common.X509CertificateCredential)5 Iterator (java.util.Iterator)5 LoginContext (javax.security.auth.login.LoginContext)4 InvalidOperationException (com.sun.enterprise.security.auth.realm.InvalidOperationException)3 NoSuchRealmException (com.sun.enterprise.security.auth.realm.NoSuchRealmException)3 NoSuchUserException (com.sun.enterprise.security.auth.realm.NoSuchUserException)3 HashSet (java.util.HashSet)3 SecurityContext (com.sun.enterprise.common.iiop.security.SecurityContext)2 ClientSecurityContext (com.sun.enterprise.security.common.ClientSecurityContext)2 AnonCredential (com.sun.enterprise.common.iiop.security.AnonCredential)1 GSSUPName (com.sun.enterprise.common.iiop.security.GSSUPName)1 IOException (java.io.IOException)1 Callback (javax.security.auth.callback.Callback)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1