2015-11-19 14:39:21 -08:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2018-07-31 13:22:18 -07:00
|
|
|
import com.google.common.base.Preconditions;
|
2018-07-27 16:33:52 -07:00
|
|
|
import javax.security.auth.callback.NameCallback;
|
|
|
|
import javax.security.auth.callback.PasswordCallback;
|
2017-04-06 22:01:29 -07:00
|
|
|
import javax.naming.AuthenticationException;
|
2015-11-19 14:39:21 -08:00
|
|
|
import javax.naming.NamingException;
|
2018-07-27 16:33:52 -07:00
|
|
|
import javax.security.auth.login.LoginContext;
|
|
|
|
import javax.security.auth.login.LoginException;
|
|
|
|
import javax.security.auth.callback.CallbackHandler;
|
|
|
|
import javax.security.auth.callback.Callback;
|
2018-07-31 13:22:18 -07:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import play.Logger;
|
2018-07-31 10:10:52 -07:00
|
|
|
|
2015-11-19 14:39:21 -08:00
|
|
|
public class AuthenticationManager {
|
|
|
|
|
2017-04-06 22:01:29 -07:00
|
|
|
public static void authenticateUser(String userName, String password) throws NamingException {
|
2018-07-31 13:22:18 -07:00
|
|
|
Preconditions.checkArgument(
|
|
|
|
!StringUtils.isAnyEmpty(userName, password), "Username or password cannot be empty"
|
|
|
|
);
|
2018-07-27 16:33:52 -07:00
|
|
|
try {
|
2018-07-31 10:10:52 -07:00
|
|
|
LoginContext lc = new LoginContext("WHZ-Authentication", new WHZCallbackHandler(userName, password));
|
2018-07-27 16:33:52 -07:00
|
|
|
lc.login();
|
|
|
|
} catch (LoginException le) {
|
|
|
|
throw new AuthenticationException(le.toString());
|
2017-04-06 22:01:29 -07:00
|
|
|
}
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|
|
|
|
|
2018-07-30 15:39:24 -07:00
|
|
|
private static class WHZCallbackHandler implements CallbackHandler {
|
2018-07-27 16:33:52 -07:00
|
|
|
private String password = null;
|
|
|
|
private String username = null;
|
2018-07-30 15:39:24 -07:00
|
|
|
private WHZCallbackHandler(String username, String password) {
|
2018-07-27 16:33:52 -07:00
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|
2018-07-31 10:10:52 -07:00
|
|
|
|
|
|
|
@Override
|
2018-07-31 13:22:18 -07:00
|
|
|
public void handle(Callback[] callbacks) {
|
2018-07-27 16:33:52 -07:00
|
|
|
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 {
|
2018-07-31 13:22:18 -07:00
|
|
|
Logger.warn("The submitted callback is unsupported! ", callback);
|
2018-07-27 16:33:52 -07:00
|
|
|
}
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|
2016-03-10 16:32:37 -08:00
|
|
|
}
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|
|
|
|
}
|