Search in sources :

Example 76 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class PrincipalBearingCredentialsToPrincipalResolverTests method verifySupports.

@Test
public void verifySupports() {
    val credential = new PrincipalBearingCredential(PrincipalFactoryUtils.newPrincipalFactory().createPrincipal("test"));
    assertTrue(this.resolver.supports(credential));
    assertFalse(this.resolver.supports(new UsernamePasswordCredential()));
    assertFalse(this.resolver.supports(null));
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Test(org.junit.jupiter.api.Test)

Example 77 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class DefaultScimV2PrincipalAttributeMapper method map.

@Override
public void map(final UserResource user, final Principal principal, final Credential credential) {
    user.setUserName(principal.getId());
    if (credential instanceof UsernamePasswordCredential) {
        user.setPassword(UsernamePasswordCredential.class.cast(credential).getPassword());
    }
    user.setActive(Boolean.TRUE);
    user.setNickName(getPrincipalAttributeValue(principal, "nickName"));
    user.setDisplayName(getPrincipalAttributeValue(principal, "displayName"));
    val name = new Name();
    name.setGivenName(getPrincipalAttributeValue(principal, "givenName"));
    name.setFamilyName(getPrincipalAttributeValue(principal, "familyName"));
    name.setMiddleName(getPrincipalAttributeValue(principal, "middleName"));
    user.setName(name);
    val email = new Email();
    email.setPrimary(Boolean.TRUE);
    email.setValue(getPrincipalAttributeValue(principal, "email"));
    user.setEmails(CollectionUtils.wrap(email));
    val phone = new PhoneNumber();
    phone.setPrimary(Boolean.TRUE);
    phone.setValue(getPrincipalAttributeValue(principal, "phoneNumber"));
    user.setPhoneNumbers(CollectionUtils.wrap(phone));
    user.setExternalId(getPrincipalAttributeValue(principal, "externalId", principal.getId()));
    if (user.getMeta() == null) {
        val meta = new Meta();
        meta.setCreated(Calendar.getInstance(TimeZone.getTimeZone(ZoneOffset.UTC)));
        meta.setResourceType(user.getUserType());
        user.setMeta(meta);
    }
}
Also used : lombok.val(lombok.val) Meta(com.unboundid.scim2.common.types.Meta) Email(com.unboundid.scim2.common.types.Email) PhoneNumber(com.unboundid.scim2.common.types.PhoneNumber) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Name(com.unboundid.scim2.common.types.Name)

Example 78 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class SamlValidateEndpoint method handle.

/**
 * Handle validation request and produce saml1 payload.
 *
 * @param username the username
 * @param password the password
 * @param service  the service
 * @return the map
 */
@ReadOperation
@Operation(summary = "Handle validation request and produce saml1 payload.", parameters = { @Parameter(name = "username", required = true), @Parameter(name = "password", required = true), @Parameter(name = "service", required = true) })
public Map<String, Object> handle(final String username, final String password, final String service) {
    val credential = new UsernamePasswordCredential(username, password);
    val selectedService = this.serviceFactory.createService(service);
    val result = this.authenticationSystemSupport.finalizeAuthenticationTransaction(selectedService, credential);
    val authentication = result.getAuthentication();
    val registeredService = this.servicesManager.findServiceBy(selectedService);
    val audit = AuditableContext.builder().service(selectedService).authentication(authentication).registeredService(registeredService).build();
    val accessResult = registeredServiceAccessStrategyEnforcer.execute(audit);
    accessResult.throwExceptionIfNeeded();
    val principal = authentication.getPrincipal();
    val context = RegisteredServiceAttributeReleasePolicyContext.builder().registeredService(registeredService).service(selectedService).principal(principal).build();
    val attributesToRelease = registeredService.getAttributeReleasePolicy().getAttributes(context);
    val principalId = registeredService.getUsernameAttributeProvider().resolveUsername(principal, selectedService, registeredService);
    val modifiedPrincipal = this.principalFactory.createPrincipal(principalId, attributesToRelease);
    val builder = DefaultAuthenticationBuilder.newInstance(authentication);
    builder.setPrincipal(modifiedPrincipal);
    val finalAuthentication = builder.build();
    val samlResponse = this.samlResponseBuilder.createResponse(selectedService.getId(), selectedService);
    samlResponseBuilder.prepareSuccessfulResponse(samlResponse, selectedService, finalAuthentication, principal, finalAuthentication.getAttributes(), principal.getAttributes());
    val resValidation = new LinkedHashMap<String, Object>();
    val encoded = SamlUtils.transformSamlObject(this.openSamlConfigBean, samlResponse).toString();
    resValidation.put(CasViewConstants.MODEL_ATTRIBUTE_NAME_ASSERTION, encoded);
    resValidation.put(CasViewConstants.MODEL_ATTRIBUTE_NAME_SERVICE, selectedService);
    resValidation.put("registeredService", registeredService);
    return resValidation;
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) LinkedHashMap(java.util.LinkedHashMap) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation) Operation(io.swagger.v3.oas.annotations.Operation)

Example 79 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class ChainingCasProtocolValidationSpecificationTests method getAssertion.

private static Assertion getAssertion() {
    val assertion = mock(Assertion.class);
    val principal = CoreAuthenticationTestUtils.getPrincipal("casuser");
    val handlers = (Map) Map.of(new UsernamePasswordCredential(), new SimpleTestUsernamePasswordAuthenticationHandler());
    val authentication = CoreAuthenticationTestUtils.getAuthenticationBuilder(principal, handlers, Map.of()).build();
    when(assertion.getPrimaryAuthentication()).thenReturn(authentication);
    when(assertion.getChainedAuthentications()).thenReturn(List.of(authentication));
    return assertion;
}
Also used : lombok.val(lombok.val) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) Map(java.util.Map) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential)

Example 80 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class RedisAuthenticationHandlerTests method verifySuccessful.

@Test
public void verifySuccessful() throws Exception {
    val result = authenticationHandler.authenticate(new UsernamePasswordCredential("casuser", "caspassword"));
    assertNotNull(result);
    val principal = result.getPrincipal();
    assertNotNull(principal);
    assertNotNull(principal.getAttributes());
    assertTrue(principal.getAttributes().containsKey("name"));
    assertTrue(principal.getAttributes().containsKey("group"));
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

lombok.val (lombok.val)111 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)111 Test (org.junit.jupiter.api.Test)74 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)30 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)12 HashMap (java.util.HashMap)8 Map (java.util.Map)8 BasicCredentialMetaData (org.apereo.cas.authentication.metadata.BasicCredentialMetaData)8 LinkedHashMap (java.util.LinkedHashMap)7 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)7 Executable (org.junit.jupiter.api.function.Executable)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)6 MockRequestContext (org.springframework.webflow.test.MockRequestContext)6 ArrayList (java.util.ArrayList)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 MockServletContext (org.springframework.mock.web.MockServletContext)5 FailedLoginException (javax.security.auth.login.FailedLoginException)4 SurrogateUsernamePasswordCredential (org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)4