Search in sources :

Example 66 with Callback

use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerManagerTest method shouldHandleJsonCallbacks.

@Test
public void shouldHandleJsonCallbacks() throws RestAuthException {
    //Given
    Callback callback1 = mock(Callback.class);
    Callback callback2 = mock(Callback.class);
    Callback[] callbacks = new Callback[] { callback1, callback2 };
    RestAuthCallbackHandler restAuthCallbackHandler1 = mock(RestAuthCallbackHandler.class);
    RestAuthCallbackHandler restAuthCallbackHandler2 = mock(RestAuthCallbackHandler.class);
    JsonValue jsonCallback1 = mock(JsonValue.class);
    JsonValue jsonCallback2 = mock(JsonValue.class);
    JsonValue jsonCallbacks = mock(JsonValue.class);
    JsonValue jsonCallback1Type = mock(JsonValue.class);
    JsonValue jsonCallback2Type = mock(JsonValue.class);
    given(jsonCallbacks.size()).willReturn(2);
    given(jsonCallbacks.get(0)).willReturn(jsonCallback1);
    given(jsonCallbacks.get(1)).willReturn(jsonCallback2);
    given(restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(Matchers.<Class<? extends Callback>>anyObject())).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler2);
    given(restAuthCallbackHandler1.getCallbackClassName()).willReturn("CALLBACK1");
    given(restAuthCallbackHandler2.getCallbackClassName()).willReturn("CALLBACK2");
    given(jsonCallback1.get("type")).willReturn(jsonCallback1Type);
    given(jsonCallback2.get("type")).willReturn(jsonCallback2Type);
    given(jsonCallback1Type.asString()).willReturn("CALLBACK1");
    given(jsonCallback2Type.asString()).willReturn("CALLBACK2");
    given(restAuthCallbackHandler1.convertFromJson(callback1, jsonCallback1)).willReturn(callback1);
    given(restAuthCallbackHandler2.convertFromJson(callback2, jsonCallback2)).willReturn(callback2);
    //When
    Callback[] originalCallbacks = restAuthCallbackHandlerManager.handleJsonCallbacks(callbacks, jsonCallbacks);
    //Then
    verify(restAuthCallbackHandler1).convertFromJson(callback1, jsonCallback1);
    verify(restAuthCallbackHandler2).convertFromJson(callback2, jsonCallback2);
    assertEquals(originalCallbacks.length, 2);
    assertEquals(originalCallbacks[0], callback1);
    assertEquals(originalCallbacks[1], callback2);
}
Also used : Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) BeforeClass(org.testng.annotations.BeforeClass) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Example 67 with Callback

use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.

the class RestAuthenticationHandlerTest method shouldInitiateAuthenticationViaGET4.

@Test
public void shouldInitiateAuthenticationViaGET4() throws AuthLoginException, L10NMessageImpl, JSONException, IOException, RestAuthResponseException, SignatureException, RestAuthException {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    String authIndexType = null;
    String indexValue = null;
    String sessionUpgradeSSOTokenId = null;
    Callback[] callbacks = new Callback[0];
    AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
    LoginProcess loginProcess = mock(LoginProcess.class);
    given(loginProcess.getLoginStage()).willReturn(LoginStage.REQUIREMENTS_WAITING);
    given(loginProcess.getCallbacks()).willReturn(callbacks);
    given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
    PagePropertiesCallback pagePropertiesCallback = mock(PagePropertiesCallback.class);
    given(pagePropertiesCallback.getTemplateName()).willReturn("TEMPLATE_NAME");
    given(pagePropertiesCallback.getModuleName()).willReturn("MODULE_NAME");
    given(pagePropertiesCallback.getPageState()).willReturn("PAGE_STATE");
    JsonValue jsonCallbacks = new JsonValue(new HashMap<String, Object>());
    jsonCallbacks.add("KEY", "VALUE");
    Map<String, String> responseHeaders = new HashMap<String, String>();
    responseHeaders.put("HEADER_KEY", "HEADER_VALUE");
    JsonValue jsonResponse = new JsonValue(new HashMap<String, Object>());
    jsonResponse.add("KEY", "VALUE");
    RestAuthResponseException restAuthResponseException = new RestAuthResponseException(999, responseHeaders, jsonResponse);
    given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
    given(restAuthCallbackHandlerManager.handleCallbacks(request, httpResponse, callbacks)).willThrow(restAuthResponseException);
    given(authIdHelper.createAuthId(Matchers.<LoginConfiguration>anyObject(), eq(authContextLocalWrapper))).willReturn("AUTH_ID");
    //When
    try {
        restAuthenticationHandler.initiateAuthentication(request, httpResponse, authIndexType, indexValue, sessionUpgradeSSOTokenId);
    } catch (RestAuthResponseException e) {
        JsonValue response = e.getJsonResponse();
        assertEquals(response.size(), 2);
        assertEquals(response.get("authId").asString(), "AUTH_ID");
        assertEquals(response.get("KEY").asString(), "VALUE");
        Map<String, String> headers = e.getResponseHeaders();
        assertEquals(headers.get("HEADER_KEY"), "HEADER_VALUE");
        assertEquals(e.getStatusCode(), 999);
        return;
    }
    //Then
    fail();
}
Also used : PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) HashMap(java.util.HashMap) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) LoginProcess(org.forgerock.openam.core.rest.authn.core.LoginProcess) HttpServletRequest(javax.servlet.http.HttpServletRequest) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) Callback(javax.security.auth.callback.Callback) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 68 with Callback

use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.

the class RestAuthenticationHandlerTest method shouldInitiateAuthenticationViaGET2.

@Test
public void shouldInitiateAuthenticationViaGET2() throws AuthLoginException, L10NMessageImpl, JSONException, IOException, RestAuthResponseException, SignatureException, RestAuthException {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    String authIndexType = null;
    String indexValue = null;
    String sessionUpgradeSSOTokenId = null;
    Callback callbackOne = mock(Callback.class);
    Callback callbackTwo = mock(Callback.class);
    Callback[] callbacks = new Callback[] { callbackOne, callbackTwo };
    PagePropertiesCallback pagePropertiesCallback = mock(PagePropertiesCallback.class);
    given(pagePropertiesCallback.getTemplateName()).willReturn("TEMPLATE_NAME");
    given(pagePropertiesCallback.getModuleName()).willReturn("MODULE_NAME");
    given(pagePropertiesCallback.getPageState()).willReturn("PAGE_STATE");
    given(pagePropertiesCallback.getHeader()).willReturn("HEADER");
    AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
    LoginProcess loginProcess = mock(LoginProcess.class);
    given(loginProcess.getLoginStage()).willReturn(LoginStage.REQUIREMENTS_WAITING);
    given(loginProcess.getCallbacks()).willReturn(callbacks);
    given(loginProcess.getPagePropertiesCallback()).willReturn(pagePropertiesCallback);
    given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
    JsonValue jsonCallbacks = new JsonValue(new HashMap<String, Object>());
    jsonCallbacks.add("KEY", "VALUE");
    given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
    given(restAuthCallbackHandlerManager.handleCallbacks(request, httpResponse, callbacks)).willReturn(jsonCallbacks);
    given(authIdHelper.createAuthId(Matchers.<LoginConfiguration>anyObject(), eq(authContextLocalWrapper))).willReturn("AUTH_ID");
    //When
    JsonValue response = restAuthenticationHandler.initiateAuthentication(request, httpResponse, authIndexType, indexValue, sessionUpgradeSSOTokenId);
    //Then
    assertEquals(response.size(), 5);
    assertEquals(response.get("authId").asString(), "AUTH_ID");
    assertEquals(response.get("template").asString(), "TEMPLATE_NAME");
    assertEquals(response.get("stage").asString(), "MODULE_NAMEPAGE_STATE");
    assertEquals(response.get("header").asString(), "HEADER");
    assertEquals(response.get("callbacks").get("KEY").asString(), "VALUE");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) Callback(javax.security.auth.callback.Callback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthContextLocalWrapper(org.forgerock.openam.core.rest.authn.core.wrappers.AuthContextLocalWrapper) LoginProcess(org.forgerock.openam.core.rest.authn.core.LoginProcess) Test(org.testng.annotations.Test)

Example 69 with Callback

use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.

the class SAML2 method initiateSAMLLoginAtIDP.

/**
     * Performs similar to SPSSOFederate.initiateAuthnRequest by returning to the next auth stage
     * with a redirect (either GET or POST depending on the config) which triggers remote IdP authentication.
     */
private int initiateSAMLLoginAtIDP(final HttpServletResponse response, final HttpServletRequest request) throws SAML2Exception, AuthLoginException {
    if (reqBinding == null) {
        reqBinding = SAML2Constants.HTTP_REDIRECT;
    }
    final String spEntityID = SPSSOFederate.getSPEntityId(metaAlias);
    final IDPSSODescriptorElement idpsso = SPSSOFederate.getIDPSSOForAuthnReq(realm, entityName);
    final SPSSODescriptorElement spsso = SPSSOFederate.getSPSSOForAuthnReq(realm, spEntityID);
    if (idpsso == null || spsso == null) {
        return processError(bundle.getString("samlLocalConfigFailed"), "SAML2 :: initiateSAMLLoginAtIDP() : {}", bundle.getString("samlLocalConfigFailed"));
    }
    final String ssoURL = SPSSOFederate.getSSOURL(idpsso.getSingleSignOnService(), reqBinding);
    final List extensionsList = SPSSOFederate.getExtensionsList(spEntityID, realm);
    final Map<String, Collection<String>> spConfigAttrsMap = SPSSOFederate.getAttrsMapForAuthnReq(realm, spEntityID);
    authnRequest = SPSSOFederate.createAuthnRequest(realm, spEntityID, params, spConfigAttrsMap, extensionsList, spsso, idpsso, ssoURL, false);
    final AuthnRequestInfo reqInfo = new AuthnRequestInfo(request, response, realm, spEntityID, null, authnRequest, null, params);
    synchronized (SPCache.requestHash) {
        SPCache.requestHash.put(authnRequest.getID(), reqInfo);
    }
    saveAuthnRequest(authnRequest, reqInfo);
    final Callback[] nextCallbacks = getCallback(REDIRECT);
    final RedirectCallback redirectCallback = (RedirectCallback) nextCallbacks[0];
    setCookiesForRedirects(request, response);
    //we only handle Redirect and POST
    if (SAML2Constants.HTTP_POST.equals(reqBinding)) {
        final String postMsg = SPSSOFederate.getPostBindingMsg(idpsso, spsso, spConfigAttrsMap, authnRequest);
        configurePostRedirectCallback(postMsg, ssoURL, redirectCallback);
    } else {
        final String authReqXMLString = authnRequest.toXMLString(true, true);
        final String redirectUrl = SPSSOFederate.getRedirect(authReqXMLString, null, ssoURL, idpsso, spsso, spConfigAttrsMap);
        configureGetRedirectCallback(redirectUrl, redirectCallback);
    }
    return REDIRECT;
}
Also used : RedirectCallback(com.sun.identity.authentication.spi.RedirectCallback) Callback(javax.security.auth.callback.Callback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) RedirectCallback(com.sun.identity.authentication.spi.RedirectCallback) SPSSODescriptorElement(com.sun.identity.saml2.jaxb.metadata.SPSSODescriptorElement) AuthnRequestInfo(com.sun.identity.saml2.profile.AuthnRequestInfo) Collection(java.util.Collection) List(java.util.List) IDPSSODescriptorElement(com.sun.identity.saml2.jaxb.metadata.IDPSSODescriptorElement)

Example 70 with Callback

use of javax.security.auth.callback.Callback in project OpenAM by OpenRock.

the class AuthUtils method authenticate.

public static SSOToken authenticate(String realm, String userName, String password) throws Exception {
    AuthContext lc = new AuthContext(realm);
    lc.login();
    while (lc.hasMoreRequirements()) {
        Callback[] callbacks = lc.getRequirements();
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                NameCallback nc = (NameCallback) callbacks[i];
                nc.setName(userName);
            } else if (callbacks[i] instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callbacks[i];
                pc.setPassword(password.toCharArray());
            } else {
                throw new Exception("No callback");
            }
        }
        lc.submitRequirements(callbacks);
    }
    return (lc.getStatus() != AuthContext.Status.SUCCESS) ? null : lc.getSSOToken();
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) AuthContext(com.sun.identity.authentication.AuthContext) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Aggregations

Callback (javax.security.auth.callback.Callback)250 NameCallback (javax.security.auth.callback.NameCallback)168 PasswordCallback (javax.security.auth.callback.PasswordCallback)160 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)110 IOException (java.io.IOException)75 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)47 LoginException (javax.security.auth.login.LoginException)47 Test (org.testng.annotations.Test)42 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)37 CallbackHandler (javax.security.auth.callback.CallbackHandler)36 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)31 HashMap (java.util.HashMap)28 Subject (javax.security.auth.Subject)27 HttpServletRequest (javax.servlet.http.HttpServletRequest)23 AuthContext (com.sun.identity.authentication.AuthContext)22 Principal (java.security.Principal)21 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)19 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)18 FailedLoginException (javax.security.auth.login.FailedLoginException)17 Test (org.junit.Test)17