Search in sources :

Example 91 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class OAuth20InvalidAuthorizationResponseBuilderTests method verifyRequestWithCallbackAndDescription.

@Test
public void verifyRequestWithCallbackAndDescription() {
    val request = new MockHttpServletRequest();
    request.addParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    request.addParameter(OAuth20Constants.REDIRECT_URI, "https://github.com/apereo/cas");
    val response = new MockHttpServletResponse();
    val context = new JEEContext(request, response);
    assertFalse(oauthInvalidAuthorizationBuilder.supports(context));
    context.setRequestAttribute(OAuth20Constants.ERROR, OAuth20Constants.INVALID_REQUEST);
    context.setRequestAttribute(OAuth20Constants.ERROR_DESCRIPTION, "Invalid Request Description");
    context.setRequestAttribute(OAuth20Constants.ERROR_WITH_CALLBACK, true);
    assertTrue(oauthInvalidAuthorizationBuilder.supports(context));
    val mv = oauthInvalidAuthorizationBuilder.build(context);
    assertTrue(mv.getView() instanceof RedirectView);
    val mvView = (RedirectView) mv.getView();
    assertEquals(mvView.getUrl(), "https://github.com/apereo/cas");
    assertTrue(mv.getModel().containsKey(OAuth20Constants.ERROR));
    assertTrue(mv.getModel().containsKey(OAuth20Constants.ERROR_DESCRIPTION));
    assertFalse(mv.getModel().containsKey(OAuth20Constants.STATE));
    val error = mv.getModel().get(OAuth20Constants.ERROR).toString();
    assertEquals(error, OAuth20Constants.INVALID_REQUEST);
    val errorDescription = mv.getModel().get(OAuth20Constants.ERROR_DESCRIPTION).toString();
    assertEquals(errorDescription, "Invalid Request Description");
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) RedirectView(org.springframework.web.servlet.view.RedirectView) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 92 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class OAuth20InvalidAuthorizationResponseBuilderTests method verifyRequestWithCallbackWithoutDescription.

@Test
public void verifyRequestWithCallbackWithoutDescription() {
    val request = new MockHttpServletRequest();
    request.addParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    request.addParameter(OAuth20Constants.REDIRECT_URI, "https://github.com/apereo/cas");
    request.addParameter(OAuth20Constants.STATE, "abcdefgh");
    request.addParameter(OAuth20Constants.RESPONSE_MODE, OAuth20ResponseModeTypes.FORM_POST.getType());
    request.addParameter("ParameterWhichShouldNotComingBack", "notAtAll");
    val response = new MockHttpServletResponse();
    val context = new JEEContext(request, response);
    assertFalse(oauthInvalidAuthorizationBuilder.supports(context));
    context.setRequestAttribute(OAuth20Constants.ERROR, OAuth20Constants.INVALID_REQUEST);
    context.setRequestAttribute(OAuth20Constants.ERROR_WITH_CALLBACK, true);
    assertTrue(oauthInvalidAuthorizationBuilder.supports(context));
    val mv = oauthInvalidAuthorizationBuilder.build(context);
    assertEquals(mv.getViewName(), CasWebflowConstants.VIEW_ID_POST_RESPONSE);
    assertTrue(mv.getModel().containsKey("originalUrl"));
    assertTrue(mv.getModel().containsKey("parameters"));
    val originalUrl = mv.getModel().get("originalUrl");
    assertEquals(originalUrl, "https://github.com/apereo/cas");
    val parameters = (Map) mv.getModel().get("parameters");
    assertTrue(parameters.containsKey(OAuth20Constants.ERROR));
    assertFalse(parameters.containsKey(OAuth20Constants.ERROR_DESCRIPTION));
    assertTrue(parameters.containsKey(OAuth20Constants.STATE));
    assertEquals(parameters.get(OAuth20Constants.ERROR), OAuth20Constants.INVALID_REQUEST);
    assertEquals(parameters.get(OAuth20Constants.STATE), "abcdefgh");
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 93 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class OAuth20InvalidAuthorizationResponseBuilderTests method verifyRequestWithoutCallback.

@Test
public void verifyRequestWithoutCallback() {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val context = new JEEContext(request, response);
    assertFalse(oauthInvalidAuthorizationBuilder.supports(context));
    context.setRequestAttribute(OAuth20Constants.ERROR, OAuth20Constants.INVALID_REQUEST);
    assertTrue(oauthInvalidAuthorizationBuilder.supports(context));
    assertEquals(oauthInvalidAuthorizationBuilder.build(context).getViewName(), CasWebflowConstants.VIEW_ID_SERVICE_ERROR);
    context.setRequestAttribute(OAuth20Constants.ERROR_WITH_CALLBACK, false);
    assertEquals(oauthInvalidAuthorizationBuilder.build(context).getViewName(), CasWebflowConstants.VIEW_ID_SERVICE_ERROR);
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 94 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class OAuth20RefreshTokenGrantTypeTokenRequestValidatorTests method verifyOperationClientSecretPost.

@Test
public void verifyOperationClientSecretPost() throws Exception {
    val request = new MockHttpServletRequest();
    val profile = new CommonProfile();
    profile.setClientName(Authenticators.CAS_OAUTH_CLIENT_BASIC_AUTHN);
    profile.setId(RequestValidatorTestUtils.SUPPORTING_CLIENT_ID);
    val session = request.getSession(true);
    assertNotNull(session);
    session.setAttribute(Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    val response = new MockHttpServletResponse();
    request.setParameter(OAuth20Constants.GRANT_TYPE, OAuth20GrantTypes.REFRESH_TOKEN.getType());
    request.setParameter(OAuth20Constants.CLIENT_ID, RequestValidatorTestUtils.SUPPORTING_CLIENT_ID);
    request.setParameter(OAuth20Constants.CLIENT_SECRET, RequestValidatorTestUtils.SHARED_SECRET);
    request.setParameter(OAuth20Constants.REFRESH_TOKEN, SUPPORTING_TICKET);
    assertTrue(validator.validate(new JEEContext(request, response)));
    profile.setId(RequestValidatorTestUtils.NON_SUPPORTING_CLIENT_ID);
    session.setAttribute(Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    request.setParameter(OAuth20Constants.CLIENT_ID, RequestValidatorTestUtils.NON_SUPPORTING_CLIENT_ID);
    request.setParameter(OAuth20Constants.CLIENT_SECRET, RequestValidatorTestUtils.SHARED_SECRET);
    request.setParameter(OAuth20Constants.REFRESH_TOKEN, NON_SUPPORTING_TICKET);
    assertFalse(validator.validate(new JEEContext(request, response)));
    profile.setId(RequestValidatorTestUtils.PROMISCUOUS_CLIENT_ID);
    session.setAttribute(Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    request.setParameter(OAuth20Constants.CLIENT_ID, RequestValidatorTestUtils.PROMISCUOUS_CLIENT_ID);
    request.setParameter(OAuth20Constants.CLIENT_SECRET, RequestValidatorTestUtils.SHARED_SECRET);
    request.setParameter(OAuth20Constants.REFRESH_TOKEN, PROMISCUOUS_TICKET);
    assertTrue(this.validator.validate(new JEEContext(request, response)));
}
Also used : lombok.val(lombok.val) CommonProfile(org.pac4j.core.profile.CommonProfile) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 95 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class OAuth20RefreshTokenGrantTypeTokenRequestValidatorTests method verifyOperationClientSecretBasic.

@Test
public void verifyOperationClientSecretBasic() throws Exception {
    val request = new MockHttpServletRequest();
    val profile = new CommonProfile();
    profile.setClientName(Authenticators.CAS_OAUTH_CLIENT_BASIC_AUTHN);
    profile.setId(RequestValidatorTestUtils.SUPPORTING_CLIENT_ID);
    val session = request.getSession(true);
    assertNotNull(session);
    session.setAttribute(Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    val response = new MockHttpServletResponse();
    request.addHeader("Authorization", "Basic " + EncodingUtils.encodeBase64(RequestValidatorTestUtils.SUPPORTING_CLIENT_ID + ':' + RequestValidatorTestUtils.SHARED_SECRET));
    request.setParameter(OAuth20Constants.GRANT_TYPE, OAuth20GrantTypes.REFRESH_TOKEN.getType());
    request.setParameter(OAuth20Constants.REFRESH_TOKEN, SUPPORTING_TICKET);
    assertTrue(this.validator.validate(new JEEContext(request, response)));
    profile.setId(RequestValidatorTestUtils.NON_SUPPORTING_CLIENT_ID);
    session.setAttribute(Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    request.removeHeader("Authorization");
    request.addHeader("Authorization", "Basic " + EncodingUtils.encodeBase64(RequestValidatorTestUtils.NON_SUPPORTING_CLIENT_ID + ':' + RequestValidatorTestUtils.SHARED_SECRET));
    request.setParameter(OAuth20Constants.REFRESH_TOKEN, NON_SUPPORTING_TICKET);
    assertFalse(validator.validate(new JEEContext(request, response)));
    profile.setId(RequestValidatorTestUtils.PROMISCUOUS_CLIENT_ID);
    session.setAttribute(Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    request.removeHeader("Authorization");
    request.addHeader("Authorization", "Basic " + EncodingUtils.encodeBase64(RequestValidatorTestUtils.PROMISCUOUS_CLIENT_ID + ':' + RequestValidatorTestUtils.SHARED_SECRET));
    request.setParameter(OAuth20Constants.REFRESH_TOKEN, PROMISCUOUS_TICKET);
    assertTrue(validator.validate(new JEEContext(request, response)));
}
Also used : lombok.val(lombok.val) CommonProfile(org.pac4j.core.profile.CommonProfile) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

JEEContext (org.pac4j.core.context.JEEContext)222 lombok.val (lombok.val)215 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)158 Test (org.junit.jupiter.api.Test)157 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)155 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)34 ProfileManager (org.pac4j.core.profile.ProfileManager)27 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)24 CommonProfile (org.pac4j.core.profile.CommonProfile)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)16 HashMap (java.util.HashMap)15 RedirectView (org.springframework.web.servlet.view.RedirectView)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 CasProfile (org.pac4j.cas.profile.CasProfile)13 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)13 MockRequestContext (org.springframework.webflow.test.MockRequestContext)13 GetMapping (org.springframework.web.bind.annotation.GetMapping)11 Map (java.util.Map)10 Slf4j (lombok.extern.slf4j.Slf4j)10