use of javax.security.sasl.AuthorizeCallback in project storm by apache.
the class SimpleSaslServerCallbackHandler method handle.
@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException, IOException {
NameCallback nc = null;
PasswordCallback pc = null;
AuthorizeCallback ac = null;
RealmCallback rc = null;
for (Callback callback : callbacks) {
if (callback instanceof AuthorizeCallback) {
ac = (AuthorizeCallback) callback;
} else if (callback instanceof NameCallback) {
nc = (NameCallback) callback;
} else if (callback instanceof PasswordCallback) {
pc = (PasswordCallback) callback;
} else if (callback instanceof RealmCallback) {
rc = (RealmCallback) callback;
} else {
throw new UnsupportedCallbackException(callback, "Unrecognized SASL Callback");
}
}
log("GOT", ac, nc, pc, rc);
if (nc != null) {
String userName = nc.getDefaultName();
boolean passwordFound = false;
for (PasswordProvider provider : providers) {
Optional<char[]> password = provider.getPasswordFor(userName);
if (password.isPresent()) {
pc.setPassword(password.get());
nc.setName(provider.userName(userName));
passwordFound = true;
break;
}
}
if (!passwordFound) {
LOG.warn("No password found for user: {}", userName);
throw new IOException("NOT ALLOWED.");
}
}
if (rc != null) {
rc.setText(rc.getDefaultText());
}
if (ac != null) {
boolean allowImpersonation = impersonationAllowed;
String nid = ac.getAuthenticationID();
if (nid != null) {
Pair<String, Boolean> tmp = translateName(nid);
nid = tmp.getFirst();
allowImpersonation = allowImpersonation && tmp.getSecond();
}
String zid = ac.getAuthorizationID();
if (zid != null) {
Pair<String, Boolean> tmp = translateName(zid);
zid = tmp.getFirst();
allowImpersonation = allowImpersonation && tmp.getSecond();
}
LOG.debug("Successfully authenticated client: authenticationID = {} authorizationID = {}", nid, zid);
// if authorizationId is not set, set it to authenticationId.
if (zid == null) {
ac.setAuthorizedID(nid);
zid = nid;
} else {
ac.setAuthorizedID(zid);
}
// add the nid as the real user in reqContext's subject which will be used during authorization.
if (!Objects.equals(nid, zid)) {
LOG.info("Impersonation attempt authenticationID = {} authorizationID = {}", nid, zid);
if (!allowImpersonation) {
throw new IllegalArgumentException(ac.getAuthenticationID() + " attempting to impersonate " + ac.getAuthorizationID() + ". This is not allowed.");
}
ReqContext.context().setRealPrincipal(new SaslTransportPlugin.User(nid));
} else {
ReqContext.context().setRealPrincipal(null);
}
ac.setAuthorized(true);
}
log("FINISHED", ac, nc, pc, rc);
}
use of javax.security.sasl.AuthorizeCallback in project jstorm by alibaba.
the class ClientCallbackHandler 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.sasl.AuthorizeCallback in project kafka by apache.
the class SaslClientCallbackHandler method handle.
@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
Subject subject = Subject.getSubject(AccessController.getContext());
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
NameCallback nc = (NameCallback) callback;
if (subject != null && !subject.getPublicCredentials(String.class).isEmpty()) {
nc.setName(subject.getPublicCredentials(String.class).iterator().next());
} else
nc.setName(nc.getDefaultName());
} else if (callback instanceof PasswordCallback) {
if (subject != null && !subject.getPrivateCredentials(String.class).isEmpty()) {
char[] password = subject.getPrivateCredentials(String.class).iterator().next().toCharArray();
((PasswordCallback) callback).setPassword(password);
} else {
String errorMessage = "Could not login: the client is being asked for a password, but the Kafka" + " client code does not currently support obtaining a password from the user.";
throw new UnsupportedCallbackException(callback, errorMessage);
}
} else if (callback instanceof RealmCallback) {
RealmCallback rc = (RealmCallback) callback;
rc.setText(rc.getDefaultText());
} else if (callback instanceof AuthorizeCallback) {
AuthorizeCallback ac = (AuthorizeCallback) callback;
String authId = ac.getAuthenticationID();
String authzId = ac.getAuthorizationID();
ac.setAuthorized(authId.equals(authzId));
if (ac.isAuthorized())
ac.setAuthorizedID(authzId);
} else if (callback instanceof ScramExtensionsCallback) {
if (ScramMechanism.isScram(mechanism) && subject != null && !subject.getPublicCredentials(Map.class).isEmpty()) {
@SuppressWarnings("unchecked") Map<String, String> extensions = (Map<String, String>) subject.getPublicCredentials(Map.class).iterator().next();
((ScramExtensionsCallback) callback).extensions(extensions);
}
} else if (callback instanceof SaslExtensionsCallback) {
if (!SaslConfigs.GSSAPI_MECHANISM.equals(mechanism) && subject != null && !subject.getPublicCredentials(SaslExtensions.class).isEmpty()) {
SaslExtensions extensions = subject.getPublicCredentials(SaslExtensions.class).iterator().next();
((SaslExtensionsCallback) callback).extensions(extensions);
}
} else {
throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
}
}
}
use of javax.security.sasl.AuthorizeCallback in project alluxio by Alluxio.
the class PlainSaslServerCallbackHandlerTest method authenticateNameNotMatch.
/**
* Tests that the authentication callbacks do not match.
*/
@Test
public void authenticateNameNotMatch() throws Exception {
mThrown.expect(AuthenticationException.class);
mThrown.expectMessage("Only allow the user starting with alluxio");
String authenticateId = "not-alluxio-1";
NameCallback ncb = new NameCallback(" authentication id: ");
ncb.setName(authenticateId);
PasswordCallback pcb = new PasswordCallback(" password: ", false);
pcb.setPassword("password".toCharArray());
Callback[] callbacks = new Callback[] { ncb, pcb, new AuthorizeCallback(authenticateId, authenticateId) };
mPlainServerCBHandler.handle(callbacks);
}
use of javax.security.sasl.AuthorizeCallback in project alluxio by Alluxio.
the class PlainSaslServerCallbackHandlerTest method authenticateCorrectPassword.
/**
* Tests that the incorrect password should fail the authentication.
*/
@Test
public void authenticateCorrectPassword() throws Exception {
mThrown.expect(AuthenticationException.class);
mThrown.expectMessage("Wrong password");
String authenticateId = "alluxio-1";
NameCallback ncb = new NameCallback(" authentication id: ");
ncb.setName(authenticateId);
PasswordCallback pcb = new PasswordCallback(" password: ", false);
pcb.setPassword("not-password".toCharArray());
Callback[] callbacks = new Callback[] { ncb, pcb, new AuthorizeCallback(authenticateId, authenticateId) };
mPlainServerCBHandler.handle(callbacks);
}
Aggregations