Search in sources :

Example 11 with CasClient

use of org.pac4j.cas.client.CasClient in project cas by apereo.

the class CasOAuthConfiguration method oauthSecConfig.

@Bean
public Config oauthSecConfig() {
    final CasConfiguration cfg = new CasConfiguration(casProperties.getServer().getLoginUrl());
    final CasClient oauthCasClient = new CasClient(cfg);
    oauthCasClient.setRedirectActionBuilder(webContext -> oauthCasClientRedirectActionBuilder().build(oauthCasClient, webContext));
    oauthCasClient.setName(Authenticators.CAS_OAUTH_CLIENT);
    oauthCasClient.setUrlResolver(casCallbackUrlResolver());
    final Authenticator authenticator = oAuthClientAuthenticator();
    final DirectBasicAuthClient basicAuthClient = new DirectBasicAuthClient(authenticator);
    basicAuthClient.setName(Authenticators.CAS_OAUTH_CLIENT_BASIC_AUTHN);
    final DirectFormClient directFormClient = new DirectFormClient(authenticator);
    directFormClient.setName(Authenticators.CAS_OAUTH_CLIENT_DIRECT_FORM);
    directFormClient.setUsernameParameter(CLIENT_ID);
    directFormClient.setPasswordParameter(CLIENT_SECRET);
    final DirectFormClient userFormClient = new DirectFormClient(oAuthUserAuthenticator());
    userFormClient.setName(Authenticators.CAS_OAUTH_CLIENT_USER_FORM);
    final Config config = new Config(OAuth20Utils.casOAuthCallbackUrl(casProperties.getServer().getPrefix()), oauthCasClient, basicAuthClient, directFormClient, userFormClient);
    config.setSessionStore(new J2ESessionStore());
    return config;
}
Also used : J2ESessionStore(org.pac4j.core.context.session.J2ESessionStore) DirectFormClient(org.pac4j.http.client.direct.DirectFormClient) CasConfiguration(org.pac4j.cas.config.CasConfiguration) Config(org.pac4j.core.config.Config) DirectBasicAuthClient(org.pac4j.http.client.direct.DirectBasicAuthClient) OAuth20ClientAuthenticator(org.apereo.cas.support.oauth.authenticator.OAuth20ClientAuthenticator) Authenticator(org.pac4j.core.credentials.authenticator.Authenticator) OAuth20UserAuthenticator(org.apereo.cas.support.oauth.authenticator.OAuth20UserAuthenticator) CasClient(org.pac4j.cas.client.CasClient) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 12 with CasClient

use of org.pac4j.cas.client.CasClient in project pac4j by pac4j.

the class CasClientBuilder method tryCreateCasClient.

public void tryCreateCasClient(final List<Client> clients) {
    for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
        final String loginUrl = getProperty(CAS_LOGIN_URL, i);
        final String protocol = getProperty(CAS_PROTOCOL, i);
        if (isNotBlank(loginUrl)) {
            CasConfiguration configuration = new CasConfiguration();
            final CasClient casClient = new CasClient(configuration);
            configuration.setLoginUrl(loginUrl);
            if (isNotBlank(protocol)) {
                configuration.setProtocol(CasProtocol.valueOf(protocol));
            }
            casClient.setName(concat(casClient.getName(), i));
            clients.add(casClient);
        }
    }
}
Also used : CasConfiguration(org.pac4j.cas.config.CasConfiguration) CasClient(org.pac4j.cas.client.CasClient)

Example 13 with CasClient

use of org.pac4j.cas.client.CasClient in project cas by apereo.

the class OAuth20DefaultCasClientRedirectActionBuilderTests method verifyOperation.

@Test
public void verifyOperation() {
    val client = new CasClient(new CasConfiguration("https://example.org/cas/login"));
    client.setCallbackUrl("https://example.org/cas/callback");
    client.init();
    val context = new JEEContext(new MockHttpServletRequest(), new MockHttpServletResponse());
    val input = oauthCasClientRedirectActionBuilder.build(client, context);
    assertFalse(input.isEmpty());
}
Also used : lombok.val(lombok.val) CasConfiguration(org.pac4j.cas.config.CasConfiguration) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) CasClient(org.pac4j.cas.client.CasClient) Test(org.junit.jupiter.api.Test)

Example 14 with CasClient

use of org.pac4j.cas.client.CasClient in project cas by apereo.

the class RestfulDelegatedClientUserProfileProvisionerTests method verifyAllowedOperation.

@Test
public void verifyAllowedOperation() {
    val commonProfile = new CommonProfile();
    commonProfile.setClientName("CasClient");
    commonProfile.setId("testuser");
    val client = new CasClient(new CasConfiguration("http://cas.example.org"));
    try (val webServer = new MockWebServer(9192, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val props = new RestEndpointProperties();
        props.setUrl("http://localhost:9192");
        val service = new RestfulDelegatedClientUserProfileProvisioner(props);
        service.execute(CoreAuthenticationTestUtils.getPrincipal(), commonProfile, client, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword());
    }
}
Also used : lombok.val(lombok.val) RestEndpointProperties(org.apereo.cas.configuration.model.RestEndpointProperties) CommonProfile(org.pac4j.core.profile.CommonProfile) CasConfiguration(org.pac4j.cas.config.CasConfiguration) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) CasClient(org.pac4j.cas.client.CasClient) Test(org.junit.jupiter.api.Test)

Example 15 with CasClient

use of org.pac4j.cas.client.CasClient in project cas by apereo.

the class DefaultDelegatedClientFactory method configureCasClient.

/**
 * Configure cas client.
 *
 * @param properties the properties
 */
protected void configureCasClient(final Collection<IndirectClient> properties) {
    val pac4jProperties = casProperties.getAuthn().getPac4j();
    val index = new AtomicInteger();
    pac4jProperties.getCas().stream().filter(cas -> cas.isEnabled() && StringUtils.isNotBlank(cas.getLoginUrl())).forEach(cas -> {
        val cfg = new CasConfiguration(cas.getLoginUrl(), CasProtocol.valueOf(cas.getProtocol()));
        val prefix = PATTERN_LOGIN_URL.matcher(cas.getLoginUrl()).replaceFirst("/");
        cfg.setPrefixUrl(StringUtils.appendIfMissing(prefix, "/"));
        cfg.setHostnameVerifier(casSSLContext.getHostnameVerifier());
        cfg.setSslSocketFactory(casSSLContext.getSslContext().getSocketFactory());
        val client = new CasClient(cfg);
        if (StringUtils.isBlank(cas.getClientName())) {
            val count = index.intValue();
            client.setName(client.getClass().getSimpleName() + count);
        }
        configureClient(client, cas);
        index.incrementAndGet();
        LOGGER.debug("Created client [{}]", client);
        properties.add(client);
    });
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Pac4jBaseClientProperties(org.apereo.cas.configuration.model.support.pac4j.Pac4jBaseClientProperties) RandomUtils(org.apereo.cas.util.RandomUtils) CasConfiguration(org.pac4j.cas.config.CasConfiguration) CasClient(org.pac4j.cas.client.CasClient) SneakyThrows(lombok.SneakyThrows) Google2Client(org.pac4j.oauth.client.Google2Client) OidcConfiguration(org.pac4j.oidc.config.OidcConfiguration) RequiredArgsConstructor(lombok.RequiredArgsConstructor) SAML2Client(org.pac4j.saml.client.SAML2Client) HttpSessionStoreFactory(org.pac4j.saml.store.HttpSessionStoreFactory) Beans(org.apereo.cas.configuration.support.Beans) StringUtils(org.apache.commons.lang3.StringUtils) PrivateKeyFactoryBean(org.apereo.cas.util.crypto.PrivateKeyFactoryBean) YahooClient(org.pac4j.oauth.client.YahooClient) AzureAdOidcConfiguration(org.pac4j.oidc.config.AzureAdOidcConfiguration) ClassUtils(org.apache.commons.lang3.ClassUtils) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) LinkedIn2Client(org.pac4j.oauth.client.LinkedIn2Client) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HiOrgServerClient(org.pac4j.oauth.client.HiOrgServerClient) IndirectClient(org.pac4j.core.client.IndirectClient) Pac4jOidcClientProperties(org.apereo.cas.configuration.model.support.pac4j.oidc.Pac4jOidcClientProperties) Synchronized(lombok.Synchronized) ResourceUtils(org.apereo.cas.util.ResourceUtils) Unchecked(org.jooq.lambda.Unchecked) Verb(com.github.scribejava.core.model.Verb) Collection(java.util.Collection) Set(java.util.Set) JWSAlgorithm(com.nimbusds.jose.JWSAlgorithm) BasePac4jOidcClientProperties(org.apereo.cas.configuration.model.support.pac4j.oidc.BasePac4jOidcClientProperties) EmptyStoreFactory(org.pac4j.saml.store.EmptyStoreFactory) Slf4j(lombok.extern.slf4j.Slf4j) ClientCustomPropertyConstants(org.apereo.cas.authentication.principal.ClientCustomPropertyConstants) QueryParameterCallbackUrlResolver(org.pac4j.core.http.callback.QueryParameterCallbackUrlResolver) XMLSecSAML2MetadataSigner(org.pac4j.saml.metadata.XMLSecSAML2MetadataSigner) DisposableBean(org.springframework.beans.factory.DisposableBean) CasSSLContext(org.apereo.cas.authentication.CasSSLContext) CasWebflowConfigurer(org.apereo.cas.web.flow.CasWebflowConfigurer) CasProtocol(org.pac4j.cas.config.CasProtocol) FoursquareClient(org.pac4j.oauth.client.FoursquareClient) GitHubClient(org.pac4j.oauth.client.GitHubClient) SAML2Configuration(org.pac4j.saml.config.SAML2Configuration) Pattern(java.util.regex.Pattern) PathParameterCallbackUrlResolver(org.pac4j.core.http.callback.PathParameterCallbackUrlResolver) WindowsLiveClient(org.pac4j.oauth.client.WindowsLiveClient) AzureAdClient(org.pac4j.oidc.client.AzureAdClient) Getter(lombok.Getter) BitbucketClient(org.pac4j.oauth.client.BitbucketClient) WordPressClient(org.pac4j.oauth.client.WordPressClient) SAML2ServiceProviderRequestedAttribute(org.pac4j.saml.metadata.SAML2ServiceProviderRequestedAttribute) OidcClient(org.pac4j.oidc.client.OidcClient) CollectionUtils(org.apereo.cas.util.CollectionUtils) PayPalClient(org.pac4j.oauth.client.PayPalClient) ECPrivateKey(java.security.interfaces.ECPrivateKey) LinkedHashSet(java.util.LinkedHashSet) NoParameterCallbackUrlResolver(org.pac4j.core.http.callback.NoParameterCallbackUrlResolver) Period(java.time.Period) GoogleOidcClient(org.pac4j.oidc.client.GoogleOidcClient) FacebookClient(org.pac4j.oauth.client.FacebookClient) KeycloakOidcClient(org.pac4j.oidc.client.KeycloakOidcClient) lombok.val(lombok.val) ApplicationContext(org.springframework.context.ApplicationContext) AppleOidcConfiguration(org.pac4j.oidc.config.AppleOidcConfiguration) GenericOAuth20Client(org.pac4j.oauth.client.GenericOAuth20Client) AppleClient(org.pac4j.oidc.client.AppleClient) TwitterClient(org.pac4j.oauth.client.TwitterClient) SAMLMessageStoreFactory(org.pac4j.saml.store.SAMLMessageStoreFactory) KeycloakOidcConfiguration(org.pac4j.oidc.config.KeycloakOidcConfiguration) DropBoxClient(org.pac4j.oauth.client.DropBoxClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CasConfiguration(org.pac4j.cas.config.CasConfiguration) CasClient(org.pac4j.cas.client.CasClient)

Aggregations

CasClient (org.pac4j.cas.client.CasClient)21 CasConfiguration (org.pac4j.cas.config.CasConfiguration)15 lombok.val (lombok.val)14 Test (org.junit.jupiter.api.Test)11 OidcClient (org.pac4j.oidc.client.OidcClient)6 SAML2Client (org.pac4j.saml.client.SAML2Client)6 JEEContext (org.pac4j.core.context.JEEContext)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)5 FacebookClient (org.pac4j.oauth.client.FacebookClient)4 TwitterClient (org.pac4j.oauth.client.TwitterClient)4 GoogleOidcClient (org.pac4j.oidc.client.GoogleOidcClient)4 Verb (com.github.scribejava.core.model.Verb)3 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)3 Collection (java.util.Collection)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StringUtils (org.apache.commons.lang3.StringUtils)3 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)3