@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 {
            // Some native authentication mechanisms appear to not handle high load
            // 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.
            }
        }
    }
Authenticates using the native operating system authentication method. On Windows,
this means Win32 authentication; on Unix/Linux, PAM authentication. New user accounts
will be created automatically as needed.<p>

Authentication is handled using the <a href="http://opensource.cenqua.com/shaj">Shaj</a>
library. In order for this provider to work, the appropriate native library must be loaded.
The appropriate native library must be manually moved from the resources/nativeAuth
directory to the lib directory.<p>

To enable this provider, set the following in the system properties:

<ul>
<li>{@code provider.auth.className = org.jivesoftware.openfire.auth.NativeAuthProvider}</li>
<li>{@code provider.user.className = org.jivesoftware.openfire.user.NativeUserProvider}</li>
</ul>

The properties to configure the provider are as follows:

<ul>
     <li>nativeAuth.domain -- <i> on Windows, the domain to use for authentication.
     If the value is not set, the machine's default domain will be used or standard OS
     auth will be used if the machine is not part of a domain. On Unix/Linux, this
     value specifies the PAM module to use for authentication. If the value is not set,
     the PAM module "other" will be used.</i></li>
</ul>

For more information about configuring the domain value and other aspects of Shaj,
please see: <a href="http://opensource.cenqua.com/shaj/doc.html">
http://opensource.cenqua.com/shaj/doc.html</a>.

@author Matt Tucker
xmppserver/src/main/java/org/jivesoftware/openfire/auth/NativeAuthProvider.java