Search in sources :

Example 6 with AuthContext

use of com.sun.identity.authentication.AuthContext 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 AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class Login method main.

public static void main(String[] args) {
    try {
        System.out.print("Realm (e.g. /): ");
        String orgName = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        System.out.print("Login module name (e.g. DataStore or LDAP): ");
        String moduleName = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        System.out.print("Login locale (e.g. en_US or fr_FR): ");
        String locale = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        Login login = new Login(moduleName, orgName, locale);
        AuthContext lc = login.getAuthContext();
        if (login.login(lc)) {
            login.logout(lc);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (AuthLoginException e) {
        e.printStackTrace();
    } catch (UnsupportedCallbackException e) {
        e.printStackTrace();
    }
    System.exit(0);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AuthContext(com.sun.identity.authentication.AuthContext) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 8 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class Login method getAuthContext.

protected AuthContext getAuthContext() throws AuthLoginException {
    AuthContext lc = new AuthContext(orgName);
    AuthContext.IndexType indexType = AuthContext.IndexType.MODULE_INSTANCE;
    if (locale == null || locale.length() == 0) {
        lc.login(indexType, loginIndexName);
    } else {
        lc.login(indexType, loginIndexName, locale);
    }
    debugMessage(loginIndexName + ": Obtained login context");
    return lc;
}
Also used : AuthContext(com.sun.identity.authentication.AuthContext)

Example 9 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class ProxyPETest method login.

private SSOToken login() throws Exception {
    lc = new AuthContext("/");
    AuthContext.IndexType indexType = AuthContext.IndexType.MODULE_INSTANCE;
    lc.login(indexType, "DataStore");
    Callback[] callbacks = null;
    // get information requested from module
    while (lc.hasMoreRequirements()) {
        callbacks = lc.getRequirements();
        if (callbacks != null) {
            addLoginCallbackMessage(callbacks);
            lc.submitRequirements(callbacks);
        }
    }
    return (lc.getStatus() == AuthContext.Status.SUCCESS) ? lc.getSSOToken() : null;
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) AuthContext(com.sun.identity.authentication.AuthContext)

Example 10 with AuthContext

use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.

the class LogSampleUtils method realmLogin.

public SSOToken realmLogin(String userid, String password, AuthContext ac) throws SSOException, AuthLoginException, Exception {
    String adminDN;
    String adminPassword;
    SSOToken ssoToken = null;
    String userID = null;
    try {
        ac.login();
    } catch (AuthLoginException le) {
        System.err.println("LogSampleUtils: Failed to start login " + "for default auth module.");
        throw le;
    }
    userID = userid;
    Callback[] callbacks = null;
    Hashtable values = new Hashtable();
    values.put(AuthXMLTags.NAME_CALLBACK, userid);
    values.put(AuthXMLTags.PASSWORD_CALLBACK, password);
    while (ac.hasMoreRequirements()) {
        callbacks = ac.getRequirements();
        try {
            fillCallbacks(callbacks, values);
            ac.submitRequirements(callbacks);
        } catch (Exception e) {
            System.err.println("Failed to submit callbacks!");
            e.printStackTrace();
            return null;
        }
    }
    AuthContext.Status istat = ac.getStatus();
    if (istat == AuthContext.Status.SUCCESS) {
        System.out.println("==>Authentication SUCCESSFUL for user " + userid);
    } else if (istat == AuthContext.Status.COMPLETED) {
        System.out.println("==>Authentication Status for user " + userid + " = " + istat);
        return null;
    }
    try {
        ssoToken = ac.getSSOToken();
    } catch (Exception e) {
        System.err.println("Failed to get SSO token!");
        throw e;
    }
    return ssoToken;
}
Also used : SSOToken(com.iplanet.sso.SSOToken) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) AuthContext(com.sun.identity.authentication.AuthContext) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) SSOException(com.iplanet.sso.SSOException)

Aggregations

AuthContext (com.sun.identity.authentication.AuthContext)40 Callback (javax.security.auth.callback.Callback)22 NameCallback (javax.security.auth.callback.NameCallback)21 PasswordCallback (javax.security.auth.callback.PasswordCallback)20 SSOToken (com.iplanet.sso.SSOToken)14 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)14 SSOException (com.iplanet.sso.SSOException)12 LoginException (javax.security.auth.login.LoginException)8 Iterator (java.util.Iterator)7 Set (java.util.Set)7 IdRepoException (com.sun.identity.idm.IdRepoException)6 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Subject (javax.security.auth.Subject)5 Principal (java.security.Principal)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 SSOTokenManager (com.iplanet.sso.SSOTokenManager)3 LoginState (com.sun.identity.authentication.service.LoginState)3