use of org.apache.karaf.jaas.modules.publickey.PublickeyCallback in project karaf by apache.
the class KarafJaasAuthenticator method authenticate.
public boolean authenticate(final String username, final PublicKey key, final ServerSession session) {
try {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PublickeyCallback) {
((PublickeyCallback) callback).setPublicKey(key);
} else {
throw new UnsupportedCallbackException(callback);
}
}
});
loginContext.login();
int roleCount = 0;
for (Principal principal : subject.getPrincipals()) {
if (principal instanceof RolePrincipal) {
roleCount++;
}
}
if (roleCount == 0) {
throw new FailedLoginException("User doesn't have role defined");
}
session.setAttribute(SUBJECT_ATTRIBUTE_KEY, subject);
return true;
} catch (Exception e) {
LOGGER.debug("User authentication failed with " + e.getMessage(), e);
return false;
}
}
Aggregations