Search in sources :

Example 16 with Client

use of org.pac4j.core.client.Client in project pac4j by pac4j.

the class DefaultCallbackClientFinder method find.

public List<Client> find(final Clients clients, final WebContext context, final String clientNames) {
    final List<Client> result = new ArrayList<>();
    final List<Client> indirectClients = new ArrayList<>();
    for (final Client client : clients.findAllClients()) {
        if (client instanceof IndirectClient) {
            final IndirectClient indirectClient = (IndirectClient) client;
            indirectClients.add(client);
            indirectClient.init();
            if (indirectClient.getCallbackUrlResolver().matches(indirectClient.getName(), context)) {
                result.add(indirectClient);
            }
        }
    }
    logger.debug("result: {}", result.stream().map(c -> c.getName()).collect(Collectors.toList()));
    // fallback: we didn't find any client on the URL
    if (result.isEmpty()) {
        // we have a default client, use it
        if (CommonHelper.isNotBlank(clientNames)) {
            final Client defaultClient = clients.findClient(clientNames);
            logger.debug("Defaulting to the configured client: {}", defaultClient);
            result.add(defaultClient);
        // or we only have one indirect client, use it
        } else if (indirectClients.size() == 1) {
            logger.debug("Defaulting to the only client: {}", indirectClients.get(0));
            result.addAll(indirectClients);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) IndirectClient(org.pac4j.core.client.IndirectClient) Client(org.pac4j.core.client.Client) IndirectClient(org.pac4j.core.client.IndirectClient)

Example 17 with Client

use of org.pac4j.core.client.Client in project pac4j by pac4j.

the class DefaultSecurityClientFinder method find.

public List<Client> find(final Clients clients, final WebContext context, final String clientNames) {
    final List<Client> result = new ArrayList<>();
    String securityClientNames = clientNames;
    // we don't have defined clients to secure the URL, use the general default security ones from the Clients if they exist
    // we check the nullity and not the blankness to allow the blank string to mean no client
    // so no clients parameter -> use the default security ones; clients=blank string -> no clients defined
    logger.debug("Provided clientNames: {}", securityClientNames);
    if (clientNames == null) {
        securityClientNames = clients.getDefaultSecurityClients();
        logger.debug("Default security clients: {}", securityClientNames);
        // still no clients defined and we only have one client, use it
        if (securityClientNames == null && clients.findAllClients().size() == 1) {
            securityClientNames = clients.getClients().get(0).getName();
            logger.debug("Only client: {}", securityClientNames);
        }
    }
    if (CommonHelper.isNotBlank(securityClientNames)) {
        final List<String> names = Arrays.asList(securityClientNames.split(Pac4jConstants.ELEMENT_SEPRATOR));
        // if a "client_name" parameter is provided on the request, get the client
        // and check if it is allowed (defined in the list of the clients)
        final String clientNameOnRequest = context.getRequestParameter(clientNameParameter);
        logger.debug("clientNameOnRequest: {}", clientNameOnRequest);
        if (clientNameOnRequest != null) {
            // from the request
            final Client client = clients.findClient(clientNameOnRequest);
            final String nameFound = client.getName();
            // if allowed -> return it
            boolean found = false;
            for (final String name : names) {
                if (CommonHelper.areEqualsIgnoreCaseAndTrim(name, nameFound)) {
                    result.add(client);
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new TechnicalException("Client not allowed: " + nameFound);
            }
        } else {
            // no client provided, return all
            for (final String name : names) {
                // from its name
                final Client client = clients.findClient(name);
                result.add(client);
            }
        }
    }
    logger.debug("result: {}", result.stream().map(c -> c.getName()).collect(Collectors.toList()));
    return result;
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) ArrayList(java.util.ArrayList) Client(org.pac4j.core.client.Client)

Example 18 with Client

use of org.pac4j.core.client.Client in project pac4j by pac4j.

the class DefaultCallbackLogic method renewSession.

protected void renewSession(final C context, final Config config) {
    final SessionStore<C> sessionStore = context.getSessionStore();
    if (sessionStore != null) {
        final String oldSessionId = sessionStore.getOrCreateSessionId(context);
        final boolean renewed = sessionStore.renewSession(context);
        if (renewed) {
            final String newSessionId = sessionStore.getOrCreateSessionId(context);
            logger.debug("Renewing session: {} -> {}", oldSessionId, newSessionId);
            final Clients clients = config.getClients();
            if (clients != null) {
                final List<Client> clientList = clients.getClients();
                for (final Client client : clientList) {
                    final BaseClient baseClient = (BaseClient) client;
                    baseClient.notifySessionRenewal(oldSessionId, context);
                }
            }
        } else {
            logger.error("Unable to renew the session. The session store may not support this feature");
        }
    } else {
        logger.error("No session store available for this web context");
    }
}
Also used : Clients(org.pac4j.core.client.Clients) BaseClient(org.pac4j.core.client.BaseClient) Client(org.pac4j.core.client.Client) BaseClient(org.pac4j.core.client.BaseClient)

Example 19 with Client

use of org.pac4j.core.client.Client 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 20 with Client

use of org.pac4j.core.client.Client in project pac4j by pac4j.

the class DefaultSecurityClientFinderTests method testBlankClientName.

@Test
public void testBlankClientName() {
    final List<Client> currentClients = finder.find(new Clients(), MockWebContext.create(), "  ");
    assertEquals(0, currentClients.size());
}
Also used : Client(org.pac4j.core.client.Client) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) Clients(org.pac4j.core.client.Clients) Test(org.junit.Test)

Aggregations

Client (org.pac4j.core.client.Client)25 Clients (org.pac4j.core.client.Clients)14 CommonProfile (org.pac4j.core.profile.CommonProfile)9 WebContext (org.pac4j.core.context.WebContext)8 MockIndirectClient (org.pac4j.core.client.MockIndirectClient)7 lombok.val (lombok.val)6 Test (org.junit.Test)5 Credentials (org.pac4j.core.credentials.Credentials)5 List (java.util.List)4 Optional (java.util.Optional)4 IndirectClient (org.pac4j.core.client.IndirectClient)4 HttpAction (org.pac4j.core.exception.HttpAction)4 UserProfile (org.pac4j.core.profile.UserProfile)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Slf4j (lombok.extern.slf4j.Slf4j)3 SessionStore (org.pac4j.core.context.session.SessionStore)3 ProfileManager (org.pac4j.core.profile.ProfileManager)3 ImmutableList (com.google.common.collect.ImmutableList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2