use of com.sun.identity.authentication.spi.PagePropertiesCallback in project OpenAM by OpenRock.
the class OpenAMAuthHandlerTest method handle.
/**
* Test the following method;.
*
* @see org.forgerock.openam.radius.server.spi.handlers.OpenAMAuthHandler#handle
* @throws RadiusProcessingException - should not happen.
* @throws AuthLoginException - should not happen.
* @throws IOException - should not happen.
*/
@Test(enabled = true)
public void handle() throws RadiusProcessingException, AuthLoginException, IOException {
// given
final Callback pagePropCallback = new PagePropertiesCallback("test_module", null, null, 0, null, false, null);
final Callback nameCallback = new NameCallback("Username:");
final Callback pwCallback = new PasswordCallback("pw_prompt", false);
final Callback[] callbacks = new Callback[] { pagePropCallback, nameCallback, pwCallback };
final String testRealm = "test_realm";
final String testChain = "test_chain";
final String cacheKey = "cache_key";
final Properties props = new Properties();
props.setProperty("realm", testRealm);
props.setProperty("chain", testChain);
final Status status = mock(Status.class);
final AuthContext authContext = mock(AuthContext.class);
when(authContext.getStatus()).thenReturn(AuthContext.Status.SUCCESS);
when(status.toString()).thenReturn("success");
when(authContext.hasMoreRequirements()).thenReturn(true, false);
when(authContext.getRequirements(true)).thenReturn(callbacks);
// Context and context holder
final ContextHolder holder = mock(ContextHolder.class);
final OpenAMAuthFactory ctxHolderFactory = mock(OpenAMAuthFactory.class);
when(holder.getCacheKey()).thenReturn(cacheKey);
when(holder.getAuthContext()).thenReturn(authContext);
when(holder.getAuthPhase()).thenReturn(AuthPhase.STARTING, AuthPhase.GATHERING_INPUT, AuthPhase.FINALIZING);
when(holder.getCallbacks()).thenReturn(callbacks, callbacks, (Callback[]) null);
when(holder.getIdxOfCurrentCallback()).thenReturn(1, 2);
final ContextHolderCache ctxHolderCache = mock(ContextHolderCache.class);
when(ctxHolderCache.createCachedContextHolder()).thenReturn(holder);
when(ctxHolderCache.get(isA(String.class))).thenReturn(holder);
EventBus eventBus = new EventBus();
final OpenAMAuthHandler handler = new OpenAMAuthHandler(ctxHolderFactory, ctxHolderCache, eventBus);
handler.init(props);
final Authenticator authenticator = mock(Authenticator.class);
when(authenticator.getOctets()).thenReturn("authenticator".getBytes());
// final StateAttribute mockStateAttribute = new StateAttribute("1");
final UserPasswordAttribute mockUserPasswordAttribute = new UserPasswordAttribute(authenticator, "secret", "testPassword");
final UserNameAttribute mockUsernameAttribute = new UserNameAttribute("testUser");
final AttributeSet mockAttrSet = mock(AttributeSet.class);
when(mockAttrSet.size()).thenReturn(2);
// when(mockAttrSet.getAttributeAt(0)).thenReturn(mockStateAttribute);
when(mockAttrSet.getAttributeAt(0)).thenReturn(mockUserPasswordAttribute);
when(mockAttrSet.getAttributeAt(1)).thenReturn(mockUsernameAttribute);
final AccessRequest mockRequestPacket = mock(AccessRequest.class);
when(mockRequestPacket.getAttributeSet()).thenReturn(mockAttrSet);
RadiusRequestContext reqCtx = mock(RadiusRequestContext.class);
when(reqCtx.getRequestAuthenticator()).thenReturn((mock(Authenticator.class)));
when(reqCtx.getClientSecret()).thenReturn("victoria");
RadiusResponse response = new RadiusResponse();
Packet mockPacket = mock(Packet.class);
when(mockPacket.getIdentifier()).thenReturn((short) 1);
RadiusRequest request = mock(RadiusRequest.class);
when(request.getRequestPacket()).thenReturn(mockPacket);
UserNameAttribute userName = mock(UserNameAttribute.class);
when(userName.getName()).thenReturn("Fred");
UserPasswordAttribute userPassword = mock(UserPasswordAttribute.class);
when(userPassword.extractPassword(isA(Authenticator.class), isA(String.class))).thenReturn("password");
when(request.getAttribute(UserPasswordAttribute.class)).thenReturn(userPassword);
when(request.getAttribute(UserNameAttribute.class)).thenReturn(userName);
String password = userPassword.extractPassword(reqCtx.getRequestAuthenticator(), reqCtx.getClientSecret());
assertThat(password).isNotNull();
// when
handler.handle(request, response, reqCtx);
// then
verify(authContext, times(1)).login(AuthContext.IndexType.SERVICE, testChain);
verify(ctxHolderFactory, times(1)).getAuthContext(testRealm);
verify(holder, times(3)).getCallbacks();
verify(holder, times(1)).setAuthPhase(ContextHolder.AuthPhase.TERMINATED);
verify(authContext, times(1)).logout();
}
use of com.sun.identity.authentication.spi.PagePropertiesCallback in project OpenAM by OpenRock.
the class DSAMECallbackHandler method getTimeOut.
/**
* Returns timeout value , 60 secs assumed if no timeout value found
* @param callbacks checked for timeout.
* @return timeout value for callbacks.
*/
long getTimeOut(Callback[] callbacks) {
long pageTimeOut = 60;
if (callbacks != null && callbacks[0] instanceof PagePropertiesCallback) {
PagePropertiesCallback pagePropertyCallback = (PagePropertiesCallback) callbacks[0];
pageTimeOut = new Integer(pagePropertyCallback.getTimeOutValue()).longValue();
}
return pageTimeOut;
}
use of com.sun.identity.authentication.spi.PagePropertiesCallback in project OpenAM by OpenRock.
the class Anonymous method sendCallback.
private String sendCallback() throws AuthLoginException {
if (callbackHandler == null) {
throw new AuthLoginException(amAuthAnonymous, "NoCallbackHandler", null);
}
String username = null;
try {
Callback[] callbacks = new Callback[2];
String header = bundle.getString("moduleHeader");
PagePropertiesCallback ppc = new PagePropertiesCallback(null, header, null, 0, null, false, null);
callbacks[0] = ppc;
callbacks[1] = new NameCallback(bundle.getString("username"));
if (debug.messageEnabled()) {
debug.message("Callback 0 is.. :" + callbacks[0]);
debug.message("Callback 1 is.. :" + callbacks[1]);
}
callbackHandler.handle(callbacks);
username = ((NameCallback) callbacks[1]).getName();
return username;
} catch (IllegalArgumentException ill) {
debug.message("message type missing");
throw new AuthLoginException(amAuthAnonymous, "IllegalArgs", null);
} catch (java.io.IOException ioe) {
throw new AuthLoginException(ioe);
} catch (UnsupportedCallbackException uce) {
throw new AuthLoginException(amAuthAnonymous, "NoCallbackHandler", null);
}
}
use of com.sun.identity.authentication.spi.PagePropertiesCallback in project OpenAM by OpenRock.
the class LoginProcessTest method shouldGetPagePropertiesCallback.
@Test
public void shouldGetPagePropertiesCallback() {
//Given
Callback callbackOne = mock(Callback.class);
Callback callbackTwo = mock(PagePropertiesCallback.class);
Callback callbackThree = mock(Callback.class);
Callback[] callbacks = new Callback[] { callbackOne, callbackTwo, callbackThree };
given(authContext.getRequirements(true)).willReturn(callbacks);
//When
PagePropertiesCallback pagePropertiesCallback = loginProcess.getPagePropertiesCallback();
//Then
verify(authContext).getRequirements(true);
Assert.assertEquals(pagePropertiesCallback, callbackTwo);
}
use of com.sun.identity.authentication.spi.PagePropertiesCallback in project OpenAM by OpenRock.
the class OpenAMAuthHandler method getNextCallbackReplyMsg.
/**
* Generates reply message for the current callback to be embedded in a challenge response to gather an answer for
* that callback. If an unknown/unexpected callback type is incurred the process is terminated with a reject
* response.
*
* @param respHandler
* @param holder
* @return
*/
private ReplyMessageAttribute getNextCallbackReplyMsg(RadiusResponse response, ContextHolder holder) {
LOG.message("Entering getNextCallbackReplyMsg()");
ReplyMessageAttribute msg = null;
final Callback[] callbacks = holder.getCallbacks();
if (callbacks == null) {
return null;
}
final Callback cb = callbacks[holder.getIdxOfCurrentCallback()];
String header = "";
final PagePropertiesCallback pagePropCallback = holder.getCallbackSetProps();
if (pagePropCallback != null && !"".equals(pagePropCallback.getHeader())) {
header = pagePropCallback.getHeader() + " ";
}
if (cb instanceof NameCallback) {
LOG.message("getNextCallbackReplyMsg(); - processing NameCallback.");
msg = new ReplyMessageAttribute(header + ((NameCallback) cb).getPrompt());
} else if (cb instanceof PasswordCallback) {
LOG.message("getNextCallbackReplyMsg(); - processing PasswordCallback.");
msg = new ReplyMessageAttribute(header + ((PasswordCallback) cb).getPrompt());
} else if (cb instanceof ChoiceCallback) {
LOG.message("getNextCallbackReplyMsg(); - processing ChoiceCallback.");
final ChoiceCallback cc = (ChoiceCallback) cb;
final StringBuilder sb = new StringBuilder();
sb.append(header);
sb.append(cc.getPrompt());
if (cc.allowMultipleSelections()) {
// ugh. we'll have to figure out how to translate this suitably in view of sentence structure for
// a given locale.
// TODO: LOCALIZE
sb.append(" (Separate Selected Numbers by Spaces");
if (cc.getDefaultChoice() >= 0) {
sb.append(". Default is " + cc.getDefaultChoice());
}
sb.append(".)");
}
sb.append('\n');
final String[] choices = cc.getChoices();
for (int j = 0; j < choices.length; j++) {
final String choice = choices[j];
if (j != 0) {
sb.append(",\n");
}
sb.append(j);
sb.append(" = ");
sb.append(choice);
}
msg = new ReplyMessageAttribute(sb.toString());
} else if (cb instanceof ConfirmationCallback) {
LOG.message("getNextCallbackReplyMsg(); - processing ConformationCallback.");
final ConfirmationCallback cc = (ConfirmationCallback) cb;
final StringBuilder sb = new StringBuilder();
sb.append(header);
sb.append(cc.getPrompt());
if (cc.getDefaultOption() >= 0) {
// ugh. ditto on above translation concern
sb.append(" (Default is ");
sb.append(cc.getDefaultOption());
sb.append(".)");
}
sb.append('\n');
final String[] options = cc.getOptions();
for (int j = 0; j < options.length; j++) {
final String option = options[j];
if (j != 0) {
sb.append(",\n");
}
sb.append(j);
sb.append(" = ");
sb.append(option);
}
msg = new ReplyMessageAttribute(sb.toString());
} else {
// unknown and unexpected type
LOG.error("Radius can not support " + cb.getClass().getSimpleName() + " used by module " + holder.getChainModuleIndex() + " with name " + holder.getModuleName() + " in chain '" + this.authChain + "'. Denying Access.");
rejectAccessAndTerminateProcess(response, holder);
}
LOG.message("Entering getNextCallbackReplyMsg() returning '" + msg + "'");
return msg;
}
Aggregations