Example usage for javax.security.auth.login CredentialException CredentialException

List of usage examples for javax.security.auth.login CredentialException CredentialException

Introduction

In this page you can find the example usage for javax.security.auth.login CredentialException CredentialException.

Prototype

public CredentialException(String msg) 

Source Link

Document

Constructs a CredentialException with the specified detail message.

Usage

From source file:com.hs.mail.security.login.JndiLoginModule.java

@Override
protected Principal[] validate(Callback[] callbacks) throws LoginException {
    String username = ((NameCallback) callbacks[0]).getName();
    char[] password = ((PasswordCallback) callbacks[1]).getPassword();

    Principal[] principals = new Principal[1];
    principals[0] = new UserPrincipal(username);
    try {//from   w w w  . j  a v  a2 s  . c  o m
        boolean ok = authenticate(username, String.valueOf(password));
        if (!ok)
            throw new CredentialException("Incorrect password for " + username);
        else
            return principals;
    } catch (Exception e) {
        throw (LoginException) new LoginException("LDAP Error").initCause(e);
    }
}

From source file:com.hs.mail.security.login.PropertiesLoginModule.java

@Override
protected Principal[] validate(Callback[] callbacks) throws LoginException {
    String username = ((NameCallback) callbacks[0]).getName();
    char[] password = ((PasswordCallback) callbacks[1]).getPassword();

    String entry = getLine(file, username + "=");
    if (entry == null)
        throw new AccountNotFoundException("Account for " + username + " not found");
    int index = entry.indexOf('=');
    if (index == -1)
        throw new FailedLoginException("Invalid user record");
    entry = entry.substring(index + 1);//w  w w .  j a v a  2s.c  o  m
    index = entry.indexOf(':');
    if (index == -1)
        throw new FailedLoginException("Invalid user record");
    String encodedPwd = entry.substring(0, index);
    String roles = entry.substring(index + 1);
    StringTokenizer st = new StringTokenizer(roles, ",");
    Principal[] principals = new Principal[st.countTokens() + 1];
    for (int i = 0; i < principals.length - 1; i++) {
        principals[i] = new RolePrincipal(st.nextToken().trim());
    }
    principals[principals.length - 1] = new UserPrincipal(username);
    boolean ok = checkPassword(encodedPwd, password);
    if (!ok)
        throw new CredentialException("Incorrect password for " + username);
    else
        return principals;
}

From source file:com.hs.mail.imap.user.DefaultUserManager.java

/**
 * Authenticate the given user against the given password. When
 * authenticated, the ID of the user will be supplied.
 * /*  ww  w .j av a2  s.co  m*/
 * @param username
 *            user name
 * @param password
 *            password supplied
 * @return id of the user when authenticated
 * @throws LoginException
 *             when the user does not exist or not authenticated
 */
public long login(String username, String password) throws LoginException {
    String address = toAddress(username);
    User user = DaoFactory.getUserDao().getUserByAddress(address);
    if (user == null) {
        throw new AccountNotFoundException("Account for " + username + " not found");
    }
    if (Config.getAuthScheme() != null) {
        CallbackHandler callbackHandler = new BasicCallbackHandler(address, password.toCharArray());
        LoginContext lc = new LoginContext(Config.getAuthScheme(), callbackHandler);
        lc.login();
    } else {
        if (!password.equals(user.getPassword())) {
            throw new CredentialException("Incorrect password for " + username);
        }
    }
    return user.getID();
}

From source file:com.liferay.jsonwebserviceclient.JSONWebServiceClientImpl.java

protected String execute(HttpRequestBase httpRequestBase) throws CredentialException, IOException {

    HttpHost httpHost = new HttpHost(_hostName, _hostPort, _protocol);

    try {//from w  w  w  .jav  a 2 s  . c  om
        if (_closeableHttpClient == null) {
            afterPropertiesSet();
        }

        HttpResponse httpResponse = _closeableHttpClient.execute(httpHost, httpRequestBase);

        StatusLine statusLine = httpResponse.getStatusLine();

        if (statusLine.getStatusCode() == HttpServletResponse.SC_NOT_FOUND) {

            if (_logger.isWarnEnabled()) {
                _logger.warn("Status code " + statusLine.getStatusCode());
            }

            return null;
        } else if (statusLine.getStatusCode() == HttpServletResponse.SC_UNAUTHORIZED) {

            throw new CredentialException("Not authorized to access JSON web service");
        } else if (statusLine.getStatusCode() == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {

            throw new JSONWebServiceUnavailableException("Service unavailable");
        }

        return EntityUtils.toString(httpResponse.getEntity(), Charsets.UTF_8);
    } finally {
        httpRequestBase.releaseConnection();
    }
}

From source file:servlets.Install_servlets.java

private boolean testValidAdminUser(String adminPassword) throws CredentialException {
    try {//from   ww w .  j  av  a  2 s  .  co  m
        //CHECK IF USER IS VALID ADMIN
        String password = SHA1.getHash(adminPassword);
        Object[] params = { password, false, false };
        DAO dao_instance = DAOProvider.getDAOByName("User");
        User user = (User) ((User_JDBCDAO) dao_instance).findByID("admin", params);

        if (user == null) {
            throw new CredentialException("Unable to update databases. Invalid admin account or password.");
        }
    } catch (Exception ex) {
        throw new CredentialException(
                "Unable to update databases. Failed when validating the admin account. Reason: "
                        + ex.getMessage());
    }
    return true;
}