loginmodules/password/authenticators/jdbc/src/main/java/org/beiter/michael/authn/jaas/loginmodules/password/authenticators/jdbc/JdbcPasswordAuthenticator.java
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
    // CHECKSTYLE:ON
    public final Subject authenticate(final String domain, final String userName, final char[] password,
                                      final PasswordValidator passwordValidator)
            throws LoginException {

        // make sure the credentials are not null
        if (domain == null || userName == null || password == null) {
            throw new LoginException("The credentials cannot be null");
        }

        // SQL query is required
        if (StringUtils.isBlank(dbProps.get().getSqlUserQuery())) {
            final String error = "Invalid SQL user authentication query (query is null or empty)";
            LOG.warn(error);
            throw new LoginException(error);
        }

        final UserRecord userRecord = getUserRecord(domain, userName);

        if (userRecord.getUserId() == null || userRecord.getUserId().length() == 0) {
            final String error = "User ID for username '" + userName + "' is null or empty in the database";
            LOG.warn(error);
            throw new LoginException(error);
        }

        if (userRecord.getCredential() == null || userRecord.getCredential().length() == 0) {
            final String error = "Credential for username '" + userName + "' / user ID '" + userRecord.getUserId()
                    + "' is null or empty in the database";
            LOG.warn(error);
            throw new LoginException(error);
        }

        // no need for defensive copies of Strings, but create a defensive copy of the password
        final char[] myPassword = password.clone();

        // convert the credential string to a char array
        final char[] myCredential = userRecord.getCredential().toCharArray();

        if (!passwordValidator.validate(myPassword, myCredential)) {
            final String error = "Invalid password for username '" + userName + "'";
            LOG.info(error);
            throw new FailedLoginException(error);
        }

        // The authentication was successful!
        // Create the subject and clean up confidential data as far as possible.

        // clear the char representation of the credential
        Cleanser.wipe(myCredential);

        // clear the defensive copy of the password created earlier
        Cleanser.wipe(myPassword);

        // create a principal that includes the username and domain name that were used to authenticate the user
        final UserPrincipal userPrincipal = new UserPrincipal(userRecord.getUserId(), domain, userName);

        // wrap the principal in a Subject
        final Subject subject = new Subject();
        subject.getPrincipals().add(userPrincipal);

        return subject;
    }
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
    // CHECKSTYLE:ON
    public final Subject authenticate(final String domain, final String userName, final char[] password,
                                      final PasswordValidator passwordValidator)
            throws LoginException {

        // make sure the credentials are not null
        if (domain == null || userName == null || password == null) {
            throw new LoginException("The credentials cannot be null");
        }

        // SQL query is required
        if (StringUtils.isBlank(dbProps.get().getSqlUserQuery())) {
            final String error = "Invalid SQL user authentication query (query is null or empty)";
            LOG.warn(error);
            throw new LoginException(error);
        }

        final UserRecord userRecord = getUserRecord(domain, userName);

        if (userRecord.getUserId() == null || userRecord.getUserId().length() == 0) {
            final String error = "User ID for username '" + userName + "' is null or empty in the database";
            LOG.warn(error);
            throw new LoginException(error);
        }

        if (userRecord.getCredential() == null || userRecord.getCredential().length() == 0) {
            final String error = "Credential for username '" + userName + "' / user ID '" + userRecord.getUserId()
                    + "' is null or empty in the database";
            LOG.warn(error);
            throw new LoginException(error);
        }

        // no need for defensive copies of Strings, but create a defensive copy of the password
        final char[] myPassword = password.clone();

        // convert the credential string to a char array
        final char[] myCredential = userRecord.getCredential().toCharArray();

        if (!passwordValidator.validate(myPassword, myCredential)) {
            final String error = "Invalid password for username '" + userName + "'";
            LOG.info(error);
            throw new FailedLoginException(error);
        }

        // The authentication was successful!
        // Create the subject and clean up confidential data as far as possible.

        // clear the char representation of the credential
        Cleanser.wipe(myCredential);

        // clear the defensive copy of the password created earlier
        Cleanser.wipe(myPassword);

        // create a principal that includes the username and domain name that were used to authenticate the user
        final UserPrincipal userPrincipal = new UserPrincipal(userRecord.getUserId(), domain, userName);

        // wrap the principal in a Subject
        final Subject subject = new Subject();
        subject.getPrincipals().add(userPrincipal);

        return subject;
    }
@ SuppressWarnings ( { "PMD.CyclomaticComplexity" , "PMD.NPathComplexity" } ) // CHECKSTYLE:ON public final Subject authenticate ( final String domain , final String userName , final char [ ] password , final PasswordValidator passwordValidator ) throws LoginException { // make sure the credentials are not null if ( domain == null || userName == null || password == null ) { throw new LoginException ( "The credentials cannot be null" ) ; } // SQL query is required if ( StringUtils . isBlank ( dbProps . get ( ) . getSqlUserQuery ( ) ) ) { final String error = "Invalid SQL user authentication query (query is null or empty)" ; LOG . warn ( error ) ; throw new LoginException ( error ) ; } final UserRecord userRecord = getUserRecord ( domain , userName ) ; if ( userRecord . getUserId ( ) == null || userRecord . getUserId ( ) . length ( ) == 0 ) { final String error = "User ID for username '" + userName + "' is null or empty in the database" ; LOG . warn ( error ) ; throw new LoginException ( error ) ; } if ( userRecord . getCredential ( ) == null || userRecord . getCredential ( ) . length ( ) == 0 ) { final String error = "Credential for username '" + userName + "' / user ID '" + userRecord . getUserId ( ) + "' is null or empty in the database" ; LOG . warn ( error ) ; throw new LoginException ( error ) ; } // no need for defensive copies of Strings, but create a defensive copy of the password final char [ ] myPassword = password . clone ( ) ; // convert the credential string to a char array final char [ ] myCredential = userRecord . getCredential ( ) . toCharArray ( ) ; if ( ! passwordValidator . validate ( myPassword , myCredential ) ) { final String error = "Invalid password for username '" + userName + "'" ; LOG . info ( error ) ; throw new FailedLoginException ( error ) ; } // The authentication was successful! // Create the subject and clean up confidential data as far as possible. // clear the char representation of the credential Cleanser . wipe ( myCredential ) ; // clear the defensive copy of the password created earlier Cleanser . wipe ( myPassword ) ; // create a principal that includes the username and domain name that were used to authenticate the user final UserPrincipal userPrincipal = new UserPrincipal ( userRecord . getUserId ( ) , domain , userName ) ; // wrap the principal in a Subject final Subject subject = new Subject ( ) ; subject . getPrincipals ( ) . add ( userPrincipal ) ; return subject ; }
CHECKSTYLE:OFF
CHECKSTYLE : OFF