use of org.apache.jackrabbit.oak.spi.security.authentication.callback.CredentialsCallback in project jackrabbit-oak by apache.
the class AbstractLoginModule method getCredentials.
/**
* Tries to retrieve valid (supported) Credentials:
* <ol>
* <li>using a {@link CredentialsCallback},</li>
* <li>looking for a {@link #SHARED_KEY_CREDENTIALS} entry in the
* shared state (see also {@link #getSharedCredentials()} and finally by</li>
* <li>searching for valid credentials in the subject.</li>
* </ol>
*
* @return Valid (supported) credentials or {@code null}.
*/
@CheckForNull
protected Credentials getCredentials() {
Set<Class> supported = getSupportedCredentials();
if (callbackHandler != null) {
log.debug("Login: retrieving Credentials using callback.");
try {
CredentialsCallback callback = new CredentialsCallback();
callbackHandler.handle(new Callback[] { callback });
Credentials creds = callback.getCredentials();
if (creds != null && supported.contains(creds.getClass())) {
log.debug("Login: Credentials '{}' obtained from callback", creds);
return creds;
} else {
log.debug("Login: No supported credentials obtained from callback; trying shared state.");
}
} catch (UnsupportedCallbackException e) {
log.warn(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
}
}
Credentials creds = getSharedCredentials();
if (creds != null && supported.contains(creds.getClass())) {
log.debug("Login: Credentials obtained from shared state.");
return creds;
} else {
log.debug("Login: No supported credentials found in shared state; looking for credentials in subject.");
for (Class clz : getSupportedCredentials()) {
Set<Credentials> cds = subject.getPublicCredentials(clz);
if (!cds.isEmpty()) {
log.debug("Login: Credentials found in subject.");
return cds.iterator().next();
}
}
}
log.debug("No credentials found.");
return null;
}
use of org.apache.jackrabbit.oak.spi.security.authentication.callback.CredentialsCallback in project jackrabbit-oak by apache.
the class CallbackHandlerImplTest method testCredentialsCallback.
@Test
public void testCredentialsCallback() throws Exception {
CredentialsCallback cb = new CredentialsCallback();
callbackHandler.handle(new Callback[] { cb });
assertSame(simpleCreds, cb.getCredentials());
}
use of org.apache.jackrabbit.oak.spi.security.authentication.callback.CredentialsCallback in project jackrabbit-oak by apache.
the class AbstractLoginModuleTest method testGetCredentialsFromCallbackHandler.
@Test
public void testGetCredentialsFromCallbackHandler() {
CallbackHandler cbh = new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) {
for (Callback cb : callbacks) {
if (cb instanceof CredentialsCallback) {
((CredentialsCallback) cb).setCredentials(new TestCredentials());
}
}
}
};
AbstractLoginModule lm = initLoginModule(TestCredentials.class, cbh);
assertTrue(lm.getCredentials() instanceof TestCredentials);
lm = initLoginModule(SimpleCredentials.class, cbh);
assertNull(lm.getCredentials());
}
use of org.apache.jackrabbit.oak.spi.security.authentication.callback.CredentialsCallback in project jackrabbit-oak by apache.
the class GuestLoginModule method login.
@Override
public boolean login() {
if (callbackHandler != null) {
CredentialsCallback ccb = new CredentialsCallback();
try {
callbackHandler.handle(new Callback[] { ccb });
Credentials credentials = ccb.getCredentials();
if (credentials == null) {
guestCredentials = new GuestCredentials();
sharedState.put(AbstractLoginModule.SHARED_KEY_CREDENTIALS, guestCredentials);
return true;
}
} catch (IOException e) {
log.debug("Login: Failed to retrieve Credentials from CallbackHandler", e);
} catch (UnsupportedCallbackException e) {
log.debug("Login: Failed to retrieve Credentials from CallbackHandler", e);
}
}
// ignore this login module
return false;
}
Aggregations