Search in sources :

Example 61 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class FormClientTests method testGetUserProfile.

@Test
public void testGetUserProfile() {
    final var formClient = getFormClient();
    formClient.setProfileCreator((credentials, context, session) -> {
        var username = ((UsernamePasswordCredentials) credentials).getUsername();
        final var profile = new CommonProfile();
        profile.setId(username);
        profile.addAttribute(Pac4jConstants.USERNAME, username);
        return Optional.of(profile);
    });
    final var context = MockWebContext.create();
    final var profile = (CommonProfile) formClient.getUserProfile(new UsernamePasswordCredentials(USERNAME, USERNAME), context, new MockSessionStore()).get();
    assertEquals(USERNAME, profile.getId());
    assertEquals(CommonProfile.class.getName() + Pac4jConstants.TYPED_ID_SEPARATOR + USERNAME, profile.getTypedId());
    assertTrue(ProfileHelper.isTypedIdOf(profile.getTypedId(), CommonProfile.class));
    assertEquals(USERNAME, profile.getUsername());
    assertEquals(1, profile.getAttributes().size());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) CommonProfile(org.pac4j.core.profile.CommonProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 62 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class IndirectBasicAuthClientTests method testMissingProfileCreator.

@Test
public void testMissingProfileCreator() {
    final var basicAuthClient = new IndirectBasicAuthClient(NAME, new SimpleTestUsernamePasswordAuthenticator());
    basicAuthClient.setCallbackUrl(CALLBACK_URL);
    basicAuthClient.setProfileCreator(null);
    TestsHelper.expectException(() -> basicAuthClient.getUserProfile(new UsernamePasswordCredentials(USERNAME, PASSWORD), MockWebContext.create(), new MockSessionStore()), TechnicalException.class, "profileCreator cannot be null");
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) SimpleTestUsernamePasswordAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 63 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class RestAuthenticatorIT method testNotFound.

@Test
public void testNotFound() {
    final var authenticator = new RestAuthenticator("http://localhost:" + PORT + "?r=notfound");
    final var credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
    authenticator.validate(credentials, MockWebContext.create(), new MockSessionStore());
    final var profile = (RestProfile) credentials.getUserProfile();
    assertNull(profile);
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) RestProfile(org.pac4j.http.profile.RestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 64 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class RestAuthenticatorIT method testProfileOk.

@Test
public void testProfileOk() {
    final var authenticator = new RestAuthenticator("http://localhost:" + PORT + "?r=ok");
    final var credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
    authenticator.validate(credentials, MockWebContext.create(), new MockSessionStore());
    final var profile = (RestProfile) credentials.getUserProfile();
    assertNotNull(profile);
    assertEquals(ID, profile.getId());
    assertEquals(1, profile.getRoles().size());
    assertEquals(ROLE, profile.getRoles().iterator().next());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) RestProfile(org.pac4j.http.profile.RestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 65 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class RestAuthenticatorIT method testHttps.

@Test
public void testHttps() {
    final var authenticator = new RestAuthenticator("https://www.google.com");
    final var credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
    authenticator.validate(credentials, MockWebContext.create(), new MockSessionStore());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)91 lombok.val (lombok.val)35 Test (org.junit.Test)25 JEEContext (org.pac4j.jee.context.JEEContext)24 CommonProfile (org.pac4j.core.profile.CommonProfile)23 Test (org.junit.jupiter.api.Test)21 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)21 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)21 MockSessionStore (org.pac4j.core.context.session.MockSessionStore)17 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)10 BasicAuthExtractor (org.pac4j.core.credentials.extractor.BasicAuthExtractor)10 OAuth20DefaultCode (org.apereo.cas.ticket.code.OAuth20DefaultCode)8 HardTimeoutExpirationPolicy (org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy)8 HashMap (java.util.HashMap)7 SimpleTestUsernamePasswordAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator)7 Map (java.util.Map)6 WebContext (org.pac4j.core.context.WebContext)5 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)4 CasRestProfile (org.pac4j.cas.profile.CasRestProfile)4 CredentialsException (org.pac4j.core.exception.CredentialsException)4