/**
 * Copyright 2015 LinkedIn Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
package security;

import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

import javax.naming.AuthenticationException;
import javax.naming.NamingException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;

public class AuthenticationManager {

  public static void authenticateUser(String userName, String password) throws NamingException {
    if (userName == null || userName.isEmpty() || password == null || password.isEmpty()) {
      throw new IllegalArgumentException("Username and password can not be blank.");
    }
    LoginContext lc = null;
    try {
      lc = new LoginContext("WHZ-Authentication", new WHZCallbackHandler(userName, password));
    } catch (LoginException le) {
      throw new AuthenticationException(le.toString());
    }

    try {
      lc.login();
    } catch (LoginException le) {
      throw new AuthenticationException(le.toString());
    }
  }

  private static class WHZCallbackHandler implements CallbackHandler {
    private String password = null;
    private String username = null;
    private WHZCallbackHandler(String username, String password) {
      this.username = username;
      this.password = password;
    }
    public void handle(Callback[] callbacks)
        throws UnsupportedCallbackException {
      NameCallback nc = null;
      PasswordCallback pc = null;
      for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
          nc = (NameCallback) callback;
          nc.setName(this.username);
        } else if (callback instanceof PasswordCallback) {
          pc = (PasswordCallback) callback;
          pc.setPassword(this.password.toCharArray());
        } else {
          throw new UnsupportedCallbackException(callback, "The submitted Callback is unsupported");
        }
      }
    }
  }
}

@@ -972,16 +972,62 @@
 lback;%0A%0A
+import com.google.common.base.Preconditions;%0A%0A
 public c
@@ -1161,137 +1161,209 @@
     
-if (userName == null %7C%7C userName.isEmpty() %7C%7C password == null %7C%7C password.isEmpty()) %7B%0A      throw new IllegalArgumentException(
+Preconditions.checkNotNull(userName, %22Username String cannot be null%22);%0A    Preconditions.checkNotNull(password, %22Password String cannot be null%22);%0A    Preconditions.checkArgument(!userName.isEmpty(), 
 %22Use
@@ -1372,13 +1372,82 @@
 ame 
+c
 an
-d p
+not be empty%22);%0A    Preconditions.checkArgument(!password.isEmpty(), %22P
 assw
@@ -1457,30 +1457,28 @@
  can
- 
 not be 
-blank.
+empty
 %22);%0A    
 %7D%0A  
@@ -1469,26 +1469,32 @@
 mpty%22);%0A    
-%7D%0A
+try %7B%0A  
     LoginCon
@@ -1506,35 +1506,8 @@
 lc =
- null;%0A    try %7B%0A      lc =
  new
@@ -1591,115 +1591,8 @@
 ));%0A
-    %7D catch (LoginException le) %7B%0A      throw new AuthenticationException(le.toString());%0A    %7D%0A%0A    try %7B%0A
     
@@ -1981,24 +1981,39 @@
 sword;%0A    %7D
+%0A%0A    @Override
 %0A    public 

address reviewer comments