Search in sources :

Example 1 with OAuth20Code

use of org.apereo.cas.ticket.code.OAuth20Code in project cas by apereo.

the class OAuth20AuthorizationCodeGrantTypeTokenRequestValidatorTests method registerTicket.

private OAuth20Code registerTicket(final OAuthRegisteredService service) throws Exception {
    val builder = new OAuth20DefaultCasAuthenticationBuilder(PrincipalFactoryUtils.newPrincipalFactory(), new WebApplicationServiceFactory(), new DefaultOAuth20ProfileScopeToAttributesFilter(), new CasConfigurationProperties());
    val oauthCasAuthenticationBuilderService = builder.buildService(service, null, false);
    val expirationPolicy = new ExpirationPolicyBuilder() {

        private static final long serialVersionUID = 3911344031977989503L;

        @Override
        public ExpirationPolicy buildTicketExpirationPolicy() {
            return new OAuth20CodeExpirationPolicy(1, 60);
        }

        @Override
        public Class getTicketType() {
            return OAuth20Code.class;
        }
    };
    val oauthCode = new OAuth20DefaultOAuthCodeFactory(expirationPolicy, mock(ServicesManager.class)).create(oauthCasAuthenticationBuilderService, RegisteredServiceTestUtils.getAuthentication(), new MockTicketGrantingTicket("casuser"), new HashSet<>(), null, null, "clientid12345", new HashMap<>(), OAuth20ResponseTypes.CODE, OAuth20GrantTypes.AUTHORIZATION_CODE);
    this.ticketRegistry.addTicket(oauthCode);
    return oauthCode;
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) OAuth20Code(org.apereo.cas.ticket.code.OAuth20Code) WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) OAuth20DefaultCasAuthenticationBuilder(org.apereo.cas.support.oauth.authenticator.OAuth20DefaultCasAuthenticationBuilder) OAuth20DefaultOAuthCodeFactory(org.apereo.cas.ticket.code.OAuth20DefaultOAuthCodeFactory) ExpirationPolicyBuilder(org.apereo.cas.ticket.ExpirationPolicyBuilder) OAuth20CodeExpirationPolicy(org.apereo.cas.ticket.code.OAuth20CodeExpirationPolicy) DefaultOAuth20ProfileScopeToAttributesFilter(org.apereo.cas.support.oauth.profile.DefaultOAuth20ProfileScopeToAttributesFilter)

Example 2 with OAuth20Code

use of org.apereo.cas.ticket.code.OAuth20Code in project cas by apereo.

the class OAuth20AuthorizeEndpointControllerTests method verifyCodeRedirectToClientApproved.

@Test
public void verifyCodeRedirectToClientApproved() throws Exception {
    clearAllServices();
    val mockRequest = new MockHttpServletRequest(HttpMethod.GET.name(), CONTEXT + OAuth20Constants.AUTHORIZE_URL);
    mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuth20Constants.RESPONSE_TYPE, OAuth20ResponseTypes.CODE.name().toLowerCase());
    mockRequest.setServerName(CAS_SERVER);
    mockRequest.setServerPort(CAS_PORT);
    mockRequest.setScheme(CAS_SCHEME);
    val mockResponse = new MockHttpServletResponse();
    val service = getRegisteredService(REDIRECT_URI, SERVICE_NAME);
    service.setBypassApprovalPrompt(false);
    this.servicesManager.save(service);
    val profile = new CasProfile();
    profile.setId(ID);
    val attributes = new HashMap<String, Object>();
    attributes.put(FIRST_NAME_ATTRIBUTE, FIRST_NAME);
    attributes.put(LAST_NAME_ATTRIBUTE, LAST_NAME);
    profile.addAttributes(attributes);
    val session = new MockHttpSession();
    mockRequest.setSession(session);
    val sessionStore = oAuth20AuthorizeEndpointController.getConfigurationContext().getSessionStore();
    val context = new JEEContext(mockRequest, mockResponse);
    val ticket = new MockTicketGrantingTicket("casuser");
    oAuth20AuthorizeEndpointController.getConfigurationContext().getTicketRegistry().addTicket(ticket);
    sessionStore.set(context, WebUtils.PARAMETER_TICKET_GRANTING_TICKET_ID, ticket.getId());
    sessionStore.set(context, Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    sessionStore.set(context, OAuth20Constants.BYPASS_APPROVAL_PROMPT, "true");
    val modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
    val view = modelAndView.getView();
    assertTrue(view instanceof RedirectView);
    val redirectView = (RedirectView) view;
    val redirectUrl = redirectView.getUrl();
    assertNotNull(redirectUrl);
    assertEquals(redirectUrl, REDIRECT_URI);
    val code = modelAndView.getModelMap().get("code");
    val oAuthCode = (OAuth20Code) this.ticketRegistry.getTicket(String.valueOf(code));
    assertNotNull(oAuthCode);
    val principal = oAuthCode.getAuthentication().getPrincipal();
    assertEquals(ID, principal.getId());
    val principalAttributes = principal.getAttributes();
    assertEquals(attributes.size(), principalAttributes.size());
    assertEquals(FIRST_NAME, principalAttributes.get(FIRST_NAME_ATTRIBUTE).get(0));
}
Also used : lombok.val(lombok.val) CasProfile(org.pac4j.cas.profile.CasProfile) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) OAuth20Code(org.apereo.cas.ticket.code.OAuth20Code) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) RedirectView(org.springframework.web.servlet.view.RedirectView) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with OAuth20Code

use of org.apereo.cas.ticket.code.OAuth20Code in project cas by apereo.

the class OAuth20AuthorizeEndpointControllerTests method verifyCodeRedirectToClientWithState.

@Test
public void verifyCodeRedirectToClientWithState() throws Exception {
    clearAllServices();
    val mockRequest = new MockHttpServletRequest(HttpMethod.GET.name(), CONTEXT + OAuth20Constants.AUTHORIZE_URL);
    mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuth20Constants.RESPONSE_TYPE, OAuth20ResponseTypes.CODE.name().toLowerCase());
    mockRequest.setServerName(CAS_SERVER);
    mockRequest.setServerPort(CAS_PORT);
    mockRequest.setScheme(CAS_SCHEME);
    mockRequest.setParameter(OAuth20Constants.STATE, STATE);
    val mockResponse = new MockHttpServletResponse();
    val service = getRegisteredService(REDIRECT_URI, SERVICE_NAME);
    service.setBypassApprovalPrompt(true);
    this.servicesManager.save(service);
    val profile = new CasProfile();
    profile.setId(ID);
    val attributes = new HashMap<String, Object>();
    attributes.put(FIRST_NAME_ATTRIBUTE, FIRST_NAME);
    attributes.put(LAST_NAME_ATTRIBUTE, LAST_NAME);
    profile.addAttributes(attributes);
    val session = new MockHttpSession();
    mockRequest.setSession(session);
    val sessionStore = oAuth20AuthorizeEndpointController.getConfigurationContext().getSessionStore();
    val context = new JEEContext(mockRequest, mockResponse);
    val ticket = new MockTicketGrantingTicket("casuser");
    oAuth20AuthorizeEndpointController.getConfigurationContext().getTicketRegistry().addTicket(ticket);
    sessionStore.set(context, WebUtils.PARAMETER_TICKET_GRANTING_TICKET_ID, ticket.getId());
    sessionStore.set(context, Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    val modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
    val view = modelAndView.getView();
    assertTrue(view instanceof RedirectView);
    val redirectView = (RedirectView) view;
    val redirectUrl = redirectView.getUrl();
    assertNotNull(redirectUrl);
    assertEquals(redirectUrl, REDIRECT_URI);
    val code = modelAndView.getModelMap().getAttribute("code");
    val oAuthCode = (OAuth20Code) this.ticketRegistry.getTicket(String.valueOf(code));
    assertNotNull(oAuthCode);
    val principal = oAuthCode.getAuthentication().getPrincipal();
    assertEquals(ID, principal.getId());
    val principalAttributes = principal.getAttributes();
    assertEquals(attributes.size(), principalAttributes.size());
    assertEquals(FIRST_NAME, principalAttributes.get(FIRST_NAME_ATTRIBUTE).get(0));
}
Also used : lombok.val(lombok.val) CasProfile(org.pac4j.cas.profile.CasProfile) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) OAuth20Code(org.apereo.cas.ticket.code.OAuth20Code) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) RedirectView(org.springframework.web.servlet.view.RedirectView) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with OAuth20Code

use of org.apereo.cas.ticket.code.OAuth20Code in project cas by apereo.

the class OAuth20AuthorizeEndpointControllerTests method verifyCodeRedirectToClient.

@Test
public void verifyCodeRedirectToClient() throws Exception {
    clearAllServices();
    val mockRequest = new MockHttpServletRequest(HttpMethod.GET.name(), CONTEXT + OAuth20Constants.AUTHORIZE_URL);
    mockRequest.setParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuth20Constants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuth20Constants.RESPONSE_TYPE, OAuth20ResponseTypes.CODE.name().toLowerCase());
    mockRequest.setServerName(CAS_SERVER);
    mockRequest.setServerPort(CAS_PORT);
    mockRequest.setScheme(CAS_SCHEME);
    mockRequest.setContextPath(StringUtils.EMPTY);
    val mockResponse = new MockHttpServletResponse();
    val casProperties = oAuth20AuthorizeEndpointController.getConfigurationContext().getCasProperties();
    casProperties.getSessionReplication().getCookie().setAutoConfigureCookiePath(true);
    casProperties.getAuthn().getOauth().setReplicateSessions(true);
    oAuth20AuthorizeEndpointController.getConfigurationContext().getOauthDistributedSessionCookieGenerator().setCookiePath(StringUtils.EMPTY);
    val service = getRegisteredService(REDIRECT_URI, SERVICE_NAME);
    service.setBypassApprovalPrompt(true);
    oAuth20AuthorizeEndpointController.getConfigurationContext().getServicesManager().save(service);
    val profile = new CasProfile();
    profile.setId(ID);
    val attributes = new HashMap<String, Object>();
    attributes.put(FIRST_NAME_ATTRIBUTE, FIRST_NAME);
    attributes.put(LAST_NAME_ATTRIBUTE, LAST_NAME);
    profile.addAttributes(attributes);
    val session = new MockHttpSession();
    mockRequest.setSession(session);
    val sessionStore = oAuth20AuthorizeEndpointController.getConfigurationContext().getSessionStore();
    val context = new JEEContext(mockRequest, mockResponse);
    val ticket = new MockTicketGrantingTicket("casuser");
    oAuth20AuthorizeEndpointController.getConfigurationContext().getTicketRegistry().addTicket(ticket);
    sessionStore.set(context, WebUtils.PARAMETER_TICKET_GRANTING_TICKET_ID, ticket.getId());
    sessionStore.set(context, Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    val modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
    val view = modelAndView.getView();
    assertTrue(view instanceof RedirectView);
    val redirectView = (RedirectView) view;
    val redirectUrl = redirectView.getUrl();
    assertNotNull(redirectUrl);
    assertEquals(redirectUrl, REDIRECT_URI);
    assertEquals("/", oAuth20AuthorizeEndpointController.getConfigurationContext().getOauthDistributedSessionCookieGenerator().getCookiePath());
    val code = modelAndView.getModelMap().get("code");
    val oAuthCode = (OAuth20Code) this.ticketRegistry.getTicket(String.valueOf(code));
    assertNotNull(oAuthCode);
    val principal = oAuthCode.getAuthentication().getPrincipal();
    assertEquals(ID, principal.getId());
    val principalAttributes = principal.getAttributes();
    assertEquals(attributes.size(), principalAttributes.size());
    assertEquals(FIRST_NAME, principalAttributes.get(FIRST_NAME_ATTRIBUTE).get(0));
}
Also used : lombok.val(lombok.val) CasProfile(org.pac4j.cas.profile.CasProfile) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) OAuth20Code(org.apereo.cas.ticket.code.OAuth20Code) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) RedirectView(org.springframework.web.servlet.view.RedirectView) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)4 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)4 OAuth20Code (org.apereo.cas.ticket.code.OAuth20Code)4 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 CasProfile (org.pac4j.cas.profile.CasProfile)3 JEEContext (org.pac4j.core.context.JEEContext)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 RedirectView (org.springframework.web.servlet.view.RedirectView)3 WebApplicationServiceFactory (org.apereo.cas.authentication.principal.WebApplicationServiceFactory)1 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)1 OAuth20DefaultCasAuthenticationBuilder (org.apereo.cas.support.oauth.authenticator.OAuth20DefaultCasAuthenticationBuilder)1 DefaultOAuth20ProfileScopeToAttributesFilter (org.apereo.cas.support.oauth.profile.DefaultOAuth20ProfileScopeToAttributesFilter)1 ExpirationPolicyBuilder (org.apereo.cas.ticket.ExpirationPolicyBuilder)1 OAuth20CodeExpirationPolicy (org.apereo.cas.ticket.code.OAuth20CodeExpirationPolicy)1 OAuth20DefaultOAuthCodeFactory (org.apereo.cas.ticket.code.OAuth20DefaultOAuthCodeFactory)1