Search in sources :

Example 1 with UserProvider

use of org.jivesoftware.openfire.user.UserProvider in project Openfire by igniterealtime.

the class NativeAuthProvider method authenticate.

@Override
public void authenticate(String username, String password) throws UnauthorizedException {
    if (username.contains("@")) {
        // Check that the specified domain matches the server's domain
        int index = username.indexOf("@");
        String domain = username.substring(index + 1);
        if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
            username = username.substring(0, index);
        } else {
            // Unknown domain. Return authentication failed.
            throw new UnauthorizedException();
        }
    }
    try {
        // very well. Therefore, synchronize access to Shaj to throttle auth checks.
        synchronized (this) {
            if (!Shaj.checkPassword(domain, username, password)) {
                throw new UnauthorizedException();
            }
        }
    } catch (UnauthorizedException ue) {
        throw ue;
    } catch (Exception e) {
        throw new UnauthorizedException(e);
    }
    // See if the user exists in the database. If not, automatically create them.
    UserManager userManager = UserManager.getInstance();
    try {
        userManager.getUser(username);
    } catch (UserNotFoundException unfe) {
        try {
            Log.debug("Automatically creating new user account for " + username);
            // Create user; use a random password for better safety in the future.
            // Note that we have to go to the user provider directly -- because the
            // provider is read-only, UserManager will usually deny access to createUser.
            UserProvider provider = UserManager.getUserProvider();
            if (!(provider instanceof NativeUserProvider)) {
                Log.error("Error: not using NativeUserProvider so authentication with " + "NativeAuthProvider will likely fail. Using: " + provider.getClass().getName());
            }
            UserManager.getUserProvider().createUser(username, StringUtils.randomString(8), null, null);
        } catch (UserAlreadyExistsException uaee) {
        // Ignore.
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) UserProvider(org.jivesoftware.openfire.user.UserProvider) NativeUserProvider(org.jivesoftware.openfire.user.NativeUserProvider) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NativeUserProvider(org.jivesoftware.openfire.user.NativeUserProvider)

Aggregations

NativeUserProvider (org.jivesoftware.openfire.user.NativeUserProvider)1 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)1 UserManager (org.jivesoftware.openfire.user.UserManager)1 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1 UserProvider (org.jivesoftware.openfire.user.UserProvider)1