Search in sources :

Example 6 with PagePropertiesCallback

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();
}
Also used : Status(com.sun.identity.authentication.AuthContext.Status) Packet(org.forgerock.openam.radius.common.Packet) OpenAMAuthFactory(org.forgerock.openam.radius.server.spi.handlers.amhandler.OpenAMAuthFactory) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) AccessRequest(org.forgerock.openam.radius.common.AccessRequest) AuthContext(com.sun.identity.authentication.AuthContext) EventBus(org.forgerock.guava.common.eventbus.EventBus) Properties(java.util.Properties) RadiusRequest(org.forgerock.openam.radius.server.RadiusRequest) RadiusResponse(org.forgerock.openam.radius.server.RadiusResponse) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) ContextHolder(org.forgerock.openam.radius.server.spi.handlers.amhandler.ContextHolder) ContextHolderCache(org.forgerock.openam.radius.server.spi.handlers.amhandler.ContextHolderCache) AttributeSet(org.forgerock.openam.radius.common.AttributeSet) RadiusRequestContext(org.forgerock.openam.radius.server.RadiusRequestContext) UserNameAttribute(org.forgerock.openam.radius.common.UserNameAttribute) PasswordCallback(javax.security.auth.callback.PasswordCallback) Authenticator(org.forgerock.openam.radius.common.Authenticator) UserPasswordAttribute(org.forgerock.openam.radius.common.UserPasswordAttribute) Test(org.testng.annotations.Test)

Example 7 with PagePropertiesCallback

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;
}
Also used : PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback)

Example 8 with PagePropertiesCallback

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);
    }
}
Also used : PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 9 with PagePropertiesCallback

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);
}
Also used : PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) Callback(javax.security.auth.callback.Callback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) Test(org.testng.annotations.Test)

Example 10 with PagePropertiesCallback

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;
}
Also used : ChoiceCallback(javax.security.auth.callback.ChoiceCallback) ReplyMessageAttribute(org.forgerock.openam.radius.common.ReplyMessageAttribute) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) HttpCallback(com.sun.identity.authentication.spi.HttpCallback) RedirectCallback(com.sun.identity.authentication.spi.RedirectCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Aggregations

PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)13 Callback (javax.security.auth.callback.Callback)8 NameCallback (javax.security.auth.callback.NameCallback)6 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)5 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)5 PasswordCallback (javax.security.auth.callback.PasswordCallback)5 HttpCallback (com.sun.identity.authentication.spi.HttpCallback)4 RedirectCallback (com.sun.identity.authentication.spi.RedirectCallback)4 Test (org.testng.annotations.Test)4 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 JsonValue (org.forgerock.json.JsonValue)2 LoginProcess (org.forgerock.openam.core.rest.authn.core.LoginProcess)2 AuthContextLocalWrapper (org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper)2 ModelControlException (com.iplanet.jato.model.ModelControlException)1 SSOException (com.iplanet.sso.SSOException)1 AuthContext (com.sun.identity.authentication.AuthContext)1 Status (com.sun.identity.authentication.AuthContext.Status)1 HiddenValueCallback (com.sun.identity.authentication.callbacks.HiddenValueCallback)1