Search in sources :

Example 66 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class DirectBasicAuthClientTests method testAuthentication.

@Test
public void testAuthentication() {
    final DirectBasicAuthClient client = new DirectBasicAuthClient(new SimpleTestUsernamePasswordAuthenticator());
    final MockWebContext context = MockWebContext.create();
    final String header = USERNAME + ":" + USERNAME;
    context.addRequestHeader(HttpConstants.AUTHORIZATION_HEADER, "Basic " + Base64.getEncoder().encodeToString(header.getBytes(StandardCharsets.UTF_8)));
    final UsernamePasswordCredentials credentials = client.getCredentials(context);
    final CommonProfile profile = client.getUserProfile(credentials, context);
    assertEquals(USERNAME, profile.getId());
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) CommonProfile(org.pac4j.core.profile.CommonProfile) SimpleTestUsernamePasswordAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 67 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class DirectDigestAuthClientTests method testAuthentication.

@Test
public void testAuthentication() {
    final DirectDigestAuthClient client = new DirectDigestAuthClient(new SimpleTestDigestAuthenticator());
    client.setRealm(REALM);
    final MockWebContext context = MockWebContext.create();
    context.addRequestHeader(AUTHORIZATION_HEADER, DIGEST_AUTHORIZATION_HEADER_VALUE);
    context.setRequestMethod(HTTP_METHOD.GET.name());
    final DigestCredentials credentials = client.getCredentials(context);
    final CommonProfile profile = client.getUserProfile(credentials, context);
    String ha1 = CredentialUtil.encryptMD5(USERNAME + ":" + REALM + ":" + PASSWORD);
    String serverDigest1 = credentials.calculateServerDigest(true, ha1);
    String serverDigest2 = credentials.calculateServerDigest(false, PASSWORD);
    assertEquals(DIGEST_RESPONSE, serverDigest1);
    assertEquals(DIGEST_RESPONSE, serverDigest2);
    assertEquals(USERNAME, profile.getId());
}
Also used : DigestCredentials(org.pac4j.http.credentials.DigestCredentials) MockWebContext(org.pac4j.core.context.MockWebContext) SimpleTestDigestAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestDigestAuthenticator) CommonProfile(org.pac4j.core.profile.CommonProfile) Test(org.junit.Test)

Example 68 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class ParameterExtractorTests method testRetrieveGetParameterOk.

@Test
public void testRetrieveGetParameterOk() {
    final MockWebContext context = MockWebContext.create().setRequestMethod(HTTP_METHOD.GET.name()).addRequestParameter(GOOD_PARAMETER, VALUE);
    final TokenCredentials credentials = getExtractor.extract(context);
    assertEquals(VALUE, credentials.getToken());
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Example 69 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class ParameterExtractorTests method testRetrievePostParameterNotSupported.

@Test
public void testRetrievePostParameterNotSupported() {
    final MockWebContext context = MockWebContext.create().setRequestMethod(HTTP_METHOD.POST.name()).addRequestParameter(GOOD_PARAMETER, VALUE);
    TestsHelper.expectException(() -> getExtractor.extract(context), CredentialsException.class, "POST requests not supported");
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) Test(org.junit.Test)

Example 70 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class ParameterExtractorTests method testRetrieveNoPostParameter.

@Test
public void testRetrieveNoPostParameter() {
    final MockWebContext context = MockWebContext.create().setRequestMethod(HTTP_METHOD.POST.name());
    final TokenCredentials credentials = postExtractor.extract(context);
    assertNull(credentials);
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Aggregations

MockWebContext (org.pac4j.core.context.MockWebContext)96 Test (org.junit.Test)91 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)20 CommonProfile (org.pac4j.core.profile.CommonProfile)18 CasConfiguration (org.pac4j.cas.config.CasConfiguration)10 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)5 HttpAction (org.pac4j.core.exception.HttpAction)5 CasProfile (org.pac4j.cas.profile.CasProfile)4 SimpleTestTokenAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator)4 DigestCredentials (org.pac4j.http.credentials.DigestCredentials)3 AssertionImpl (org.jasig.cas.client.validation.AssertionImpl)2 CasRestProfile (org.pac4j.cas.profile.CasRestProfile)2 Credentials (org.pac4j.core.credentials.Credentials)2 TechnicalException (org.pac4j.core.exception.TechnicalException)2 KerberosCredentials (org.pac4j.kerberos.credentials.KerberosCredentials)2 URL (java.net.URL)1 IndirectClient (org.pac4j.core.client.IndirectClient)1 Cookie (org.pac4j.core.context.Cookie)1 PathParameterCallbackUrlResolver (org.pac4j.core.http.callback.PathParameterCallbackUrlResolver)1 JavaSerializationHelper (org.pac4j.core.util.JavaSerializationHelper)1