Search in sources :

Example 51 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class DirectCasClientTests method testTicketExistsValidationOccurs.

@Test
public void testTicketExistsValidationOccurs() {
    final var configuration = new CasConfiguration();
    configuration.setLoginUrl(LOGIN_URL);
    configuration.setDefaultTicketValidator((ticket, service) -> {
        if (TICKET.equals(ticket) && CALLBACK_URL.equals(service)) {
            return new AssertionImpl(TICKET);
        }
        throw new TechnicalException("Bad ticket or service");
    });
    final var client = new DirectCasClient(configuration);
    final var context = MockWebContext.create();
    context.setFullRequestURL(CALLBACK_URL + "?" + CasConfiguration.TICKET_PARAMETER + "=" + TICKET);
    context.addRequestParameter(CasConfiguration.TICKET_PARAMETER, TICKET);
    final var credentials = (TokenCredentials) client.getCredentials(context, new MockSessionStore()).get();
    assertEquals(TICKET, credentials.getToken());
    final var profile = credentials.getUserProfile();
    assertTrue(profile instanceof CasProfile);
    assertEquals(TICKET, profile.getId());
}
Also used : AssertionImpl(org.jasig.cas.client.validation.AssertionImpl) CasProfile(org.pac4j.cas.profile.CasProfile) TechnicalException(org.pac4j.core.exception.TechnicalException) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) CasConfiguration(org.pac4j.cas.config.CasConfiguration) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Example 52 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class CasRestClientIT method internalTestRestForm.

private void internalTestRestForm(final Authenticator authenticator) {
    final var client = new CasRestFormClient();
    client.setConfiguration(getConfig());
    client.setAuthenticator(authenticator);
    final var context = MockWebContext.create();
    context.addRequestParameter(client.getUsernameParameter(), USER);
    context.addRequestParameter(client.getPasswordParameter(), USER);
    final var credentials = (UsernamePasswordCredentials) client.getCredentials(context, new MockSessionStore()).get();
    final var profile = (CasRestProfile) client.getUserProfile(credentials, context, new MockSessionStore()).get();
    assertEquals(USER, profile.getId());
    assertNotNull(profile.getTicketGrantingTicketId());
    final var casCreds = client.requestServiceTicket(PAC4J_BASE_URL, profile, context);
    final var casProfile = client.validateServiceTicket(PAC4J_BASE_URL, casCreds, context);
    assertNotNull(casProfile);
    assertEquals(USER, casProfile.getId());
    assertTrue(casProfile.getAttributes().size() > 0);
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) CasRestProfile(org.pac4j.cas.profile.CasRestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 53 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class CasRedirectionActionBuilderTest method testRedirectRenewAttribute.

@Test
public void testRedirectRenewAttribute() {
    final var builder = newBuilder(new CasConfiguration());
    final var context = MockWebContext.create();
    context.setRequestAttribute(RedirectionActionBuilder.ATTRIBUTE_FORCE_AUTHN, true);
    final var action = builder.getRedirectionAction(context, new MockSessionStore()).get();
    assertTrue(action instanceof FoundAction);
    assertTrue(((FoundAction) action).getLocation().contains("renew=true"));
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) FoundAction(org.pac4j.core.exception.http.FoundAction) CasConfiguration(org.pac4j.cas.config.CasConfiguration) Test(org.junit.Test)

Example 54 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class DefaultCasAuthorizationGeneratorTests method testIsRemembered.

@Test
public void testIsRemembered() {
    final AuthorizationGenerator generator = new DefaultCasAuthorizationGenerator();
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(DefaultCasAuthorizationGenerator.DEFAULT_REMEMBER_ME_ATTRIBUTE_NAME, "true");
    final var profile = new CasProfile();
    profile.build(ID, attributes);
    generator.generate(null, new MockSessionStore(), profile);
    assertEquals(true, profile.isRemembered());
}
Also used : CasProfile(org.pac4j.cas.profile.CasProfile) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) HashMap(java.util.HashMap) AuthorizationGenerator(org.pac4j.core.authorization.generator.AuthorizationGenerator) Test(org.junit.Test)

Example 55 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class DefaultCasAuthorizationGeneratorTests method testBadAttributeValue.

@Test
public void testBadAttributeValue() {
    final AuthorizationGenerator generator = new DefaultCasAuthorizationGenerator();
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(DefaultCasAuthorizationGenerator.DEFAULT_REMEMBER_ME_ATTRIBUTE_NAME, "yes");
    final var profile = new CasProfile();
    profile.build(ID, attributes);
    generator.generate(null, new MockSessionStore(), profile);
    assertEquals(false, profile.isRemembered());
}
Also used : CasProfile(org.pac4j.cas.profile.CasProfile) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) HashMap(java.util.HashMap) AuthorizationGenerator(org.pac4j.core.authorization.generator.AuthorizationGenerator) Test(org.junit.Test)

Aggregations

MockSessionStore (org.pac4j.core.context.session.MockSessionStore)164 Test (org.junit.Test)151 FoundAction (org.pac4j.core.exception.http.FoundAction)29 SessionStore (org.pac4j.core.context.session.SessionStore)22 CommonProfile (org.pac4j.core.profile.CommonProfile)20 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)19 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)17 MockWebContext (org.pac4j.core.context.MockWebContext)15 WebContext (org.pac4j.core.context.WebContext)15 CasConfiguration (org.pac4j.cas.config.CasConfiguration)14 HttpAction (org.pac4j.core.exception.http.HttpAction)12 SimpleTestTokenAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator)11 AnonymousProfile (org.pac4j.core.profile.AnonymousProfile)9 SimpleTestUsernamePasswordAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator)7 CasProfile (org.pac4j.cas.profile.CasProfile)6 HashMap (java.util.HashMap)5 Authorizer (org.pac4j.core.authorization.authorizer.Authorizer)5 RequireAnyRoleAuthorizer (org.pac4j.core.authorization.authorizer.RequireAnyRoleAuthorizer)5 OkAction (org.pac4j.core.exception.http.OkAction)5 URL (java.net.URL)4