use of javax.security.auth.callback.NameCallback in project hive by apache.
the class PlainSaslServer method evaluateResponse.
@Override
public byte[] evaluateResponse(byte[] response) throws SaslException {
try {
// parse the response
// message = [authzid] UTF8NUL authcid UTF8NUL passwd'
Deque<String> tokenList = new ArrayDeque<String>();
StringBuilder messageToken = new StringBuilder();
for (byte b : response) {
if (b == 0) {
tokenList.addLast(messageToken.toString());
messageToken = new StringBuilder();
} else {
messageToken.append((char) b);
}
}
tokenList.addLast(messageToken.toString());
// validate response
if (tokenList.size() < 2 || tokenList.size() > 3) {
throw new SaslException("Invalid message format");
}
String passwd = tokenList.removeLast();
user = tokenList.removeLast();
// optional authzid
String authzId;
if (tokenList.isEmpty()) {
authzId = user;
} else {
authzId = tokenList.removeLast();
}
if (user == null || user.isEmpty()) {
throw new SaslException("No user name provided");
}
if (passwd == null || passwd.isEmpty()) {
throw new SaslException("No password name provided");
}
NameCallback nameCallback = new NameCallback("User");
nameCallback.setName(user);
PasswordCallback pcCallback = new PasswordCallback("Password", false);
pcCallback.setPassword(passwd.toCharArray());
AuthorizeCallback acCallback = new AuthorizeCallback(user, authzId);
Callback[] cbList = { nameCallback, pcCallback, acCallback };
handler.handle(cbList);
if (!acCallback.isAuthorized()) {
throw new SaslException("Authentication failed");
}
} catch (IllegalStateException eL) {
throw new SaslException("Invalid message format", eL);
} catch (IOException eI) {
throw new SaslException("Error validating the login", eI);
} catch (UnsupportedCallbackException eU) {
throw new SaslException("Error validating the login", eU);
}
return null;
}
use of javax.security.auth.callback.NameCallback in project storm by apache.
the class AbstractSaslClientCallbackHandler method handle.
/**
* This method is invoked by SASL for authentication challenges
* @param callbacks a collection of challenge callbacks
*/
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback c : callbacks) {
if (c instanceof NameCallback) {
LOG.debug("name callback");
NameCallback nc = (NameCallback) c;
nc.setName(_username);
} else if (c instanceof PasswordCallback) {
LOG.debug("password callback");
PasswordCallback pc = (PasswordCallback) c;
if (_password != null) {
pc.setPassword(_password.toCharArray());
}
} else if (c instanceof AuthorizeCallback) {
LOG.debug("authorization callback");
AuthorizeCallback ac = (AuthorizeCallback) c;
String authid = ac.getAuthenticationID();
String authzid = ac.getAuthorizationID();
if (authid.equals(authzid)) {
ac.setAuthorized(true);
} else {
ac.setAuthorized(false);
}
if (ac.isAuthorized()) {
ac.setAuthorizedID(authzid);
}
} else if (c instanceof RealmCallback) {
RealmCallback rc = (RealmCallback) c;
((RealmCallback) c).setText(rc.getDefaultText());
} else {
throw new UnsupportedCallbackException(c);
}
}
}
use of javax.security.auth.callback.NameCallback in project jetty.project by eclipse.
the class JAASLoginService method getUserName.
/* ------------------------------------------------------------ */
private String getUserName(CallbackHandler callbackHandler) throws IOException, UnsupportedCallbackException {
NameCallback nameCallback = new NameCallback("foo");
callbackHandler.handle(new Callback[] { nameCallback });
return nameCallback.getName();
}
use of javax.security.auth.callback.NameCallback in project jetty.project by eclipse.
the class JAASLoginService method login.
/* ------------------------------------------------------------ */
@Override
public UserIdentity login(final String username, final Object credentials, final ServletRequest request) {
try {
CallbackHandler callbackHandler = null;
if (_callbackHandlerClass == null) {
callbackHandler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword((char[]) credentials.toString().toCharArray());
} else if (callback instanceof ObjectCallback) {
((ObjectCallback) callback).setObject(credentials);
} else if (callback instanceof RequestParameterCallback) {
RequestParameterCallback rpc = (RequestParameterCallback) callback;
if (request != null)
rpc.setParameterValues(Arrays.asList(request.getParameterValues(rpc.getParameterName())));
} else
throw new UnsupportedCallbackException(callback);
}
}
};
} else {
Class<?> clazz = Loader.loadClass(_callbackHandlerClass);
callbackHandler = (CallbackHandler) clazz.newInstance();
}
//set up the login context
//TODO jaspi requires we provide the Configuration parameter
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(_loginModuleName, subject, callbackHandler);
loginContext.login();
//login success
JAASUserPrincipal userPrincipal = new JAASUserPrincipal(getUserName(callbackHandler), subject, loginContext);
subject.getPrincipals().add(userPrincipal);
return _identityService.newUserIdentity(subject, userPrincipal, getGroups(subject));
} catch (LoginException e) {
LOG.warn(e);
} catch (IOException e) {
LOG.warn(e);
} catch (UnsupportedCallbackException e) {
LOG.warn(e);
} catch (InstantiationException e) {
LOG.warn(e);
} catch (IllegalAccessException e) {
LOG.warn(e);
} catch (ClassNotFoundException e) {
LOG.warn(e);
}
return null;
}
use of javax.security.auth.callback.NameCallback in project jetty.project by eclipse.
the class AbstractLoginModule method login.
/**
* @see javax.security.auth.spi.LoginModule#login()
* @return true if is authenticated, false otherwise
* @throws LoginException if unable to login
*/
public boolean login() throws LoginException {
try {
if (isIgnored())
return false;
if (callbackHandler == null)
throw new LoginException("No callback handler");
Callback[] callbacks = configureCallbacks();
callbackHandler.handle(callbacks);
String webUserName = ((NameCallback) callbacks[0]).getName();
Object webCredential = null;
//first check if ObjectCallback has the credential
webCredential = ((ObjectCallback) callbacks[1]).getObject();
if (webCredential == null)
//use standard PasswordCallback
webCredential = ((PasswordCallback) callbacks[2]).getPassword();
if ((webUserName == null) || (webCredential == null)) {
setAuthenticated(false);
throw new FailedLoginException();
}
UserInfo userInfo = getUserInfo(webUserName);
if (userInfo == null) {
setAuthenticated(false);
throw new FailedLoginException();
}
currentUser = new JAASUserInfo(userInfo);
setAuthenticated(currentUser.checkCredential(webCredential));
if (isAuthenticated()) {
currentUser.fetchRoles();
return true;
} else
throw new FailedLoginException();
} catch (IOException e) {
throw new LoginException(e.toString());
} catch (UnsupportedCallbackException e) {
throw new LoginException(e.toString());
} catch (Exception e) {
if (e instanceof LoginException)
throw (LoginException) e;
throw new LoginException(e.toString());
}
}
Aggregations