Search in sources :

Example 26 with RegisteredService

use of org.apereo.cas.services.RegisteredService in project cas by apereo.

the class GoogleAccountsServiceResponseBuilder method constructSamlResponse.

/**
     * Construct SAML response.
     * <a href="http://bit.ly/1uI8Ggu">See this reference for more info.</a>
     *
     * @param service the service
     * @return the SAML response
     */
protected String constructSamlResponse(final GoogleAccountsService service) {
    final ZonedDateTime currentDateTime = ZonedDateTime.now(ZoneOffset.UTC);
    final ZonedDateTime notBeforeIssueInstant = ZonedDateTime.parse("2003-04-17T00:46:02Z");
    final RegisteredService registeredService = servicesManager.findServiceBy(service);
    if (registeredService == null || !registeredService.getAccessStrategy().isServiceAccessAllowed()) {
        throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE);
    }
    final String userId = registeredService.getUsernameAttributeProvider().resolveUsername(service.getPrincipal(), service);
    final org.opensaml.saml.saml2.core.Response response = this.samlObjectBuilder.newResponse(this.samlObjectBuilder.generateSecureRandomId(), currentDateTime, service.getId(), service);
    response.setStatus(this.samlObjectBuilder.newStatus(StatusCode.SUCCESS, null));
    final String sessionIndex = '_' + String.valueOf(Math.abs(new SecureRandom().nextLong()));
    final AuthnStatement authnStatement = this.samlObjectBuilder.newAuthnStatement(AuthnContext.PASSWORD_AUTHN_CTX, currentDateTime, sessionIndex);
    final Assertion assertion = this.samlObjectBuilder.newAssertion(authnStatement, casServerPrefix, notBeforeIssueInstant, this.samlObjectBuilder.generateSecureRandomId());
    final Conditions conditions = this.samlObjectBuilder.newConditions(notBeforeIssueInstant, currentDateTime.plusSeconds(this.skewAllowance), service.getId());
    assertion.setConditions(conditions);
    final Subject subject = this.samlObjectBuilder.newSubject(NameID.EMAIL, userId, service.getId(), currentDateTime.plusSeconds(this.skewAllowance), service.getRequestId());
    assertion.setSubject(subject);
    response.getAssertions().add(assertion);
    final StringWriter writer = new StringWriter();
    this.samlObjectBuilder.marshalSamlXmlObject(response, writer);
    final String result = writer.toString();
    LOGGER.debug("Generated Google SAML response: [{}]", result);
    return result;
}
Also used : RegisteredService(org.apereo.cas.services.RegisteredService) Assertion(org.opensaml.saml.saml2.core.Assertion) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) SecureRandom(java.security.SecureRandom) Conditions(org.opensaml.saml.saml2.core.Conditions) Subject(org.opensaml.saml.saml2.core.Subject) StringWriter(java.io.StringWriter) ZonedDateTime(java.time.ZonedDateTime) AuthnStatement(org.opensaml.saml.saml2.core.AuthnStatement)

Example 27 with RegisteredService

use of org.apereo.cas.services.RegisteredService in project cas by apereo.

the class Saml10SuccessResponseView method prepareSamlAttributes.

/**
     * Prepare saml attributes. Combines both principal and authentication
     * attributes. If the authentication is to be remembered, uses {@link #rememberMeAttributeName}
     * for the remember-me attribute name.
     *
     * @param model the model
     * @return the final map
     * @since 4.1.0
     */
private Map<String, Object> prepareSamlAttributes(final Map<String, Object> model, final Service service) {
    final Map<String, Object> authnAttributes = new HashMap<>(getAuthenticationAttributesAsMultiValuedAttributes(model));
    if (isRememberMeAuthentication(model)) {
        authnAttributes.remove(RememberMeCredential.AUTHENTICATION_ATTRIBUTE_REMEMBER_ME);
        authnAttributes.put(this.rememberMeAttributeName, Boolean.TRUE.toString());
    }
    LOGGER.debug("Retrieved authentication attributes [{}] from the model", authnAttributes);
    final RegisteredService registeredService = this.servicesManager.findServiceBy(service);
    final Map<String, Object> attributesToReturn = new HashMap<>();
    attributesToReturn.putAll(getPrincipalAttributesAsMultiValuedAttributes(model));
    attributesToReturn.putAll(authnAttributes);
    decideIfCredentialPasswordShouldBeReleasedAsAttribute(attributesToReturn, model, registeredService);
    decideIfProxyGrantingTicketShouldBeReleasedAsAttribute(attributesToReturn, model, registeredService);
    LOGGER.debug("Beginning to encode attributes [{}] for service [{}]", attributesToReturn, registeredService.getServiceId());
    final Map<String, Object> finalAttributes = this.protocolAttributeEncoder.encodeAttributes(attributesToReturn, registeredService);
    LOGGER.debug("Final collection of attributes are [{}]", finalAttributes);
    return finalAttributes;
}
Also used : RegisteredService(org.apereo.cas.services.RegisteredService) HashMap(java.util.HashMap)

Example 28 with RegisteredService

use of org.apereo.cas.services.RegisteredService in project cas by apereo.

the class RegisteredServiceSimpleFormControllerTests method verifyEditMockRegisteredService.

@Test
public void verifyEditMockRegisteredService() throws Exception {
    this.registeredServiceFactory = new DefaultRegisteredServiceFactory(new DefaultAccessStrategyMapper(), policyMapper, new DefaultProxyPolicyMapper(), new MockRegisteredServiceMapper(), new DefaultUsernameAttributeProviderMapper(), Collections.singletonList(new AttributeFormDataPopulator(this.repository)));
    this.controller = new RegisteredServiceSimpleFormController(this.manager, this.registeredServiceFactory);
    final MockRegisteredService r = new MockRegisteredService();
    r.setId(1000);
    r.setName("Test Service");
    r.setServiceId(TEST_ID);
    r.setDescription(DESCRIPTION);
    this.manager.save(r);
    r.setServiceId("serviceId1");
    final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(r);
    this.controller.saveService(new MockHttpServletRequest(), new MockHttpServletResponse(), data, mock(BindingResult.class));
    assertFalse(this.manager.getAllServices().isEmpty());
    final RegisteredService r2 = this.manager.findServiceBy(1000);
    assertEquals("serviceId1", r2.getServiceId());
    assertTrue(r2 instanceof MockRegisteredService);
}
Also used : BindingResult(org.springframework.validation.BindingResult) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) AbstractRegisteredService(org.apereo.cas.services.AbstractRegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultAccessStrategyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultAccessStrategyMapper) RegisteredServiceEditBean(org.apereo.cas.mgmt.services.web.beans.RegisteredServiceEditBean) DefaultProxyPolicyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultProxyPolicyMapper) RegisteredServiceSimpleFormController(org.apereo.cas.mgmt.services.web.RegisteredServiceSimpleFormController) DefaultRegisteredServiceFactory(org.apereo.cas.mgmt.services.web.factory.DefaultRegisteredServiceFactory) DefaultUsernameAttributeProviderMapper(org.apereo.cas.mgmt.services.web.factory.DefaultUsernameAttributeProviderMapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AttributeFormDataPopulator(org.apereo.cas.mgmt.services.web.factory.AttributeFormDataPopulator) Test(org.junit.Test)

Example 29 with RegisteredService

use of org.apereo.cas.services.RegisteredService in project cas by apereo.

the class RegisteredServiceSimpleFormControllerTests method verifyAddRegexRegisteredService.

@Test
public void verifyAddRegexRegisteredService() throws Exception {
    final RegexRegisteredService svc = new RegexRegisteredService();
    svc.setDescription(DESCRIPTION);
    svc.setServiceId("^serviceId");
    svc.setName(NAME);
    svc.setId(1000);
    svc.setEvaluationOrder(1000);
    final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(svc);
    this.controller.saveService(new MockHttpServletRequest(), new MockHttpServletResponse(), data, mock(BindingResult.class));
    final Collection<RegisteredService> services = this.manager.getAllServices();
    assertEquals(1, services.size());
    this.manager.getAllServices().forEach(rs -> assertTrue(rs instanceof RegexRegisteredService));
}
Also used : BindingResult(org.springframework.validation.BindingResult) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) AbstractRegisteredService(org.apereo.cas.services.AbstractRegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) RegisteredServiceEditBean(org.apereo.cas.mgmt.services.web.beans.RegisteredServiceEditBean) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 30 with RegisteredService

use of org.apereo.cas.services.RegisteredService in project cas by apereo.

the class RegisteredServiceSimpleFormControllerTests method verifyAddMockRegisteredService.

@Test
public void verifyAddMockRegisteredService() throws Exception {
    this.registeredServiceFactory = new DefaultRegisteredServiceFactory(new DefaultAccessStrategyMapper(), policyMapper, new DefaultProxyPolicyMapper(), new MockRegisteredServiceMapper(), new DefaultUsernameAttributeProviderMapper(), Collections.singletonList(new AttributeFormDataPopulator(this.repository)));
    this.controller = new RegisteredServiceSimpleFormController(this.manager, this.registeredServiceFactory);
    final MockRegisteredService svc = new MockRegisteredService();
    svc.setDescription(DESCRIPTION);
    svc.setServiceId("^serviceId");
    svc.setName(NAME);
    svc.setId(1000);
    svc.setEvaluationOrder(1000);
    final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(svc);
    this.controller.saveService(new MockHttpServletRequest(), new MockHttpServletResponse(), data, mock(BindingResult.class));
    final Collection<RegisteredService> services = this.manager.getAllServices();
    assertEquals(1, services.size());
    this.manager.getAllServices().forEach(rs -> assertTrue(rs instanceof MockRegisteredService));
}
Also used : BindingResult(org.springframework.validation.BindingResult) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) AbstractRegisteredService(org.apereo.cas.services.AbstractRegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultAccessStrategyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultAccessStrategyMapper) RegisteredServiceEditBean(org.apereo.cas.mgmt.services.web.beans.RegisteredServiceEditBean) DefaultProxyPolicyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultProxyPolicyMapper) RegisteredServiceSimpleFormController(org.apereo.cas.mgmt.services.web.RegisteredServiceSimpleFormController) DefaultRegisteredServiceFactory(org.apereo.cas.mgmt.services.web.factory.DefaultRegisteredServiceFactory) DefaultUsernameAttributeProviderMapper(org.apereo.cas.mgmt.services.web.factory.DefaultUsernameAttributeProviderMapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AttributeFormDataPopulator(org.apereo.cas.mgmt.services.web.factory.AttributeFormDataPopulator) Test(org.junit.Test)

Aggregations

RegisteredService (org.apereo.cas.services.RegisteredService)109 Test (org.junit.Test)39 Authentication (org.apereo.cas.authentication.Authentication)35 Service (org.apereo.cas.authentication.principal.Service)30 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)27 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)22 Principal (org.apereo.cas.authentication.principal.Principal)21 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)18 RegexRegisteredService (org.apereo.cas.services.RegexRegisteredService)17 AbstractRegisteredService (org.apereo.cas.services.AbstractRegisteredService)13 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 Event (org.springframework.webflow.execution.Event)12 ServicesManager (org.apereo.cas.services.ServicesManager)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 MultifactorAuthenticationProvider (org.apereo.cas.services.MultifactorAuthenticationProvider)10 OAuthCode (org.apereo.cas.ticket.code.OAuthCode)10 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 Collection (java.util.Collection)8