Search in sources :

Example 1 with UsernamePasswordAuthenticationProvider

use of com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider in project gitblit by gitblit.

the class AuthenticationManager method authenticate.

/**
	 * Authenticate a user based on a username and password.
	 *
	 * @see IUserService.authenticate(String, char[])
	 * @param username
	 * @param password
	 * @return a user object or null
	 */
@Override
public UserModel authenticate(String username, char[] password, String remoteIP) {
    if (StringUtils.isEmpty(username)) {
        // can not authenticate empty username
        return null;
    }
    if (username.equalsIgnoreCase(Constants.FEDERATION_USER)) {
        // it must be routed to FederationManager
        return null;
    }
    String usernameDecoded = StringUtils.decodeUsername(username);
    String pw = new String(password);
    if (StringUtils.isEmpty(pw)) {
        // can not authenticate empty password
        return null;
    }
    UserModel user = userManager.getUserModel(usernameDecoded);
    // try local authentication
    if (user != null && user.isLocalAccount()) {
        UserModel returnedUser = authenticateLocal(user, password);
        if (returnedUser != null) {
            // user authenticated
            return returnedUser;
        }
    } else {
        // try registered external authentication providers
        for (AuthenticationProvider provider : authenticationProviders) {
            if (provider instanceof UsernamePasswordAuthenticationProvider) {
                UserModel returnedUser = provider.authenticate(usernameDecoded, password);
                if (returnedUser != null) {
                    // user authenticated
                    returnedUser.accountType = provider.getAccountType();
                    return validateAuthentication(returnedUser, AuthenticationType.CREDENTIALS);
                }
            }
        }
    }
    // could not authenticate locally or with a provider
    logger.warn(MessageFormat.format("Failed login attempt for {0}, invalid credentials from {1}", username, remoteIP != null ? remoteIP : "unknown"));
    return null;
}
Also used : UserModel(com.gitblit.models.UserModel) UsernamePasswordAuthenticationProvider(com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider) AuthenticationProvider(com.gitblit.auth.AuthenticationProvider) UsernamePasswordAuthenticationProvider(com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider)

Aggregations

AuthenticationProvider (com.gitblit.auth.AuthenticationProvider)1 UsernamePasswordAuthenticationProvider (com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider)1 UserModel (com.gitblit.models.UserModel)1