Search in sources :

Example 36 with WebContext

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

the class RedirectSAML2ClientTests method testNameIdPolicyFormat.

@Test
public void testNameIdPolicyFormat() {
    final SAML2Client client = getClient();
    client.getConfiguration().setNameIdPolicyFormat("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
    final WebContext context = new J2EContext(new MockHttpServletRequest(), new MockHttpServletResponse());
    final RedirectAction action = client.getRedirectAction(context);
    final String loc = action.getLocation();
    assertTrue(getInflatedAuthnRequest(loc).contains("<saml2p:NameIDPolicy AllowCreate=\"true\" " + "Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress\"/></saml2p:AuthnRequest>"));
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) J2EContext(org.pac4j.core.context.J2EContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) RedirectAction(org.pac4j.core.redirect.RedirectAction) Test(org.junit.Test)

Example 37 with WebContext

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

the class RedirectSAML2ClientTests method testAuthnContextClassRef.

@Test
public void testAuthnContextClassRef() {
    final SAML2Client client = getClient();
    client.getConfiguration().setComparisonType(AuthnContextComparisonTypeEnumeration.EXACT.toString());
    client.getConfiguration().setAuthnContextClassRef("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");
    final WebContext context = new J2EContext(new MockHttpServletRequest(), new MockHttpServletResponse());
    final RedirectAction action = client.getRedirectAction(context);
    final String checkClass = "<saml2p:RequestedAuthnContext Comparison=\"exact\"><saml2:AuthnContextClassRef " + "xmlns:saml2=\"urn:oasis:names:tc:SAML:2.0:assertion\">" + "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml2:AuthnContextClassRef>" + "</saml2p:RequestedAuthnContext>";
    assertTrue(getInflatedAuthnRequest(action.getLocation()).contains(checkClass));
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) J2EContext(org.pac4j.core.context.J2EContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) RedirectAction(org.pac4j.core.redirect.RedirectAction) Test(org.junit.Test)

Example 38 with WebContext

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

the class DefaultSecurityClientFinderTests method testClientOnRequestNotAllowed.

@Test
public void testClientOnRequestNotAllowed() {
    final MockIndirectClient client1 = new MockIndirectClient(NAME, RedirectAction.redirect(LOGIN_URL), (Credentials) null, new CommonProfile());
    final MockIndirectClient client2 = new MockIndirectClient(CLIENT_NAME, RedirectAction.redirect(LOGIN_URL), (Credentials) null, new CommonProfile());
    final Clients clients = new Clients(client1, client2);
    final WebContext context = MockWebContext.create().addRequestParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, NAME);
    TestsHelper.expectException(() -> finder.find(clients, context, CLIENT_NAME), TechnicalException.class, "Client not allowed: " + NAME);
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockWebContext(org.pac4j.core.context.MockWebContext) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) CommonProfile(org.pac4j.core.profile.CommonProfile) Clients(org.pac4j.core.client.Clients) Test(org.junit.Test)

Example 39 with WebContext

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

the class DefaultSecurityClientFinderTests method internalTestNoClientOnRequestList.

private void internalTestNoClientOnRequestList(final String names) {
    final MockIndirectClient client1 = new MockIndirectClient(NAME, RedirectAction.redirect(LOGIN_URL), (Credentials) null, new CommonProfile());
    final MockIndirectClient client2 = new MockIndirectClient(CLIENT_NAME, RedirectAction.redirect(LOGIN_URL), (Credentials) null, new CommonProfile());
    final Clients clients = new Clients(client1, client2);
    final WebContext context = MockWebContext.create();
    final List<Client> currentClients = finder.find(clients, context, names);
    assertEquals(2, currentClients.size());
    assertEquals(client2, currentClients.get(0));
    assertEquals(client1, currentClients.get(1));
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockWebContext(org.pac4j.core.context.MockWebContext) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) CommonProfile(org.pac4j.core.profile.CommonProfile) Clients(org.pac4j.core.client.Clients) Client(org.pac4j.core.client.Client) MockIndirectClient(org.pac4j.core.client.MockIndirectClient)

Example 40 with WebContext

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

the class DefaultSecurityClientFinderTests method internalTestClientOnRequestAllowedList.

private void internalTestClientOnRequestAllowedList(final String parameterName, final String names) {
    final MockIndirectClient client = new MockIndirectClient(NAME, RedirectAction.redirect(LOGIN_URL), (Credentials) null, new CommonProfile());
    final Clients clients = new Clients(client);
    final WebContext context = MockWebContext.create().addRequestParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, parameterName);
    final List<Client> currentClients = finder.find(clients, context, names);
    assertEquals(1, currentClients.size());
    assertEquals(client, currentClients.get(0));
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockWebContext(org.pac4j.core.context.MockWebContext) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) CommonProfile(org.pac4j.core.profile.CommonProfile) Clients(org.pac4j.core.client.Clients) Client(org.pac4j.core.client.Client) MockIndirectClient(org.pac4j.core.client.MockIndirectClient)

Aggregations

WebContext (org.pac4j.core.context.WebContext)58 Test (org.junit.Test)31 MockWebContext (org.pac4j.core.context.MockWebContext)15 Slf4j (lombok.extern.slf4j.Slf4j)11 J2EContext (org.pac4j.core.context.J2EContext)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)11 lombok.val (lombok.val)10 CommonProfile (org.pac4j.core.profile.CommonProfile)10 RedirectAction (org.pac4j.core.redirect.RedirectAction)10 Optional (java.util.Optional)9 Clients (org.pac4j.core.client.Clients)9 SessionStore (org.pac4j.core.context.session.SessionStore)8 JWT (com.nimbusds.jwt.JWT)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 Client (org.pac4j.core.client.Client)7 MockIndirectClient (org.pac4j.core.client.MockIndirectClient)7 UserProfile (org.pac4j.core.profile.UserProfile)7 SignedJWT (com.nimbusds.jwt.SignedJWT)6 StringUtils (org.apache.commons.lang3.StringUtils)6