Search in sources :

Example 21 with CasProfile

use of org.pac4j.cas.profile.CasProfile in project pac4j by pac4j.

the class CasRestClientIT method internalTestRestBasic.

private void internalTestRestBasic(final CasRestBasicAuthClient client, int nbAttributes) {
    final MockWebContext context = MockWebContext.create();
    final String token = USER + ":" + USER;
    context.addRequestHeader(VALUE, NAME + Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)));
    final UsernamePasswordCredentials credentials = client.getCredentials(context);
    final CasRestProfile profile = client.getUserProfile(credentials, context);
    assertEquals(USER, profile.getId());
    assertNotNull(profile.getTicketGrantingTicketId());
    final TokenCredentials casCreds = client.requestServiceTicket(PAC4J_BASE_URL, profile, context);
    final CasProfile casProfile = client.validateServiceTicket(PAC4J_BASE_URL, casCreds, context);
    assertNotNull(casProfile);
    assertEquals(USER, casProfile.getId());
    assertEquals(nbAttributes, casProfile.getAttributes().size());
    client.destroyTicketGrantingTicket(profile, context);
    TestsHelper.expectException(() -> client.requestServiceTicket(PAC4J_BASE_URL, profile, context), TechnicalException.class, "Service ticket request for `#CasRestProfile# | id: " + USER + " | attributes: {} | roles: [] | permissions: [] | " + "isRemembered: false | clientName: CasRestBasicAuthClient | linkedId: null |` failed: (404) Not Found");
}
Also used : CasProfile(org.pac4j.cas.profile.CasProfile) MockWebContext(org.pac4j.core.context.MockWebContext) CasRestProfile(org.pac4j.cas.profile.CasRestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) TokenCredentials(org.pac4j.core.credentials.TokenCredentials)

Example 22 with CasProfile

use of org.pac4j.cas.profile.CasProfile 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 23 with CasProfile

use of org.pac4j.cas.profile.CasProfile in project cas by apereo.

the class OAuth20AuthorizeEndpointControllerTests method verifyRedirectToApproval.

@Test
public void verifyRedirectToApproval() 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));
    val modelAndView = oAuth20AuthorizeEndpointController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuth20Constants.CONFIRM_VIEW, modelAndView.getViewName());
    val model = modelAndView.getModel();
    assertEquals(AUTHORIZE_URL + '?' + OAuth20Constants.BYPASS_APPROVAL_PROMPT + "=true", model.get("callbackUrl"));
    assertEquals(SERVICE_NAME, model.get("serviceName"));
}
Also used : lombok.val(lombok.val) CasProfile(org.pac4j.cas.profile.CasProfile) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 24 with CasProfile

use of org.pac4j.cas.profile.CasProfile in project cas by apereo.

the class OAuth20AuthorizeEndpointControllerTests method verifyTokenRedirectToClientApproved.

@Test
public void verifyTokenRedirectToClientApproved() 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.TOKEN.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);
    assertTrue(redirectUrl.startsWith(REDIRECT_URI + "#access_token="));
    val code = StringUtils.substringBetween(redirectUrl, "#access_token=", "&token_type=bearer");
    val accessToken = (OAuth20AccessToken) this.ticketRegistry.getTicket(code);
    assertNotNull(accessToken);
    val principal = accessToken.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) OAuth20AccessToken(org.apereo.cas.ticket.accesstoken.OAuth20AccessToken) 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 25 with CasProfile

use of org.pac4j.cas.profile.CasProfile 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)

Aggregations

CasProfile (org.pac4j.cas.profile.CasProfile)29 HashMap (java.util.HashMap)20 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)20 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpSession (org.springframework.mock.web.MockHttpSession)16 RedirectView (org.springframework.web.servlet.view.RedirectView)14 lombok.val (lombok.val)13 Test (org.junit.Test)13 Test (org.junit.jupiter.api.Test)13 JEEContext (org.pac4j.core.context.JEEContext)13 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)11 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 Principal (org.apereo.cas.authentication.principal.Principal)6 CommonProfile (org.pac4j.core.profile.CommonProfile)6 View (org.springframework.web.servlet.View)6 MockWebContext (org.pac4j.core.context.MockWebContext)4 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)4 AccessToken (org.apereo.cas.ticket.accesstoken.AccessToken)3 OAuth20AccessToken (org.apereo.cas.ticket.accesstoken.OAuth20AccessToken)3