Search in sources :

Example 1 with OAuth20Configuration

use of org.pac4j.oauth.config.OAuth20Configuration in project pac4j by pac4j.

the class GenericOAuth20ClientTest method setProfileAttrs.

@Test
public void setProfileAttrs() throws Exception {
    GenericOAuth20Client client = new GenericOAuth20Client();
    Map map = new HashMap();
    map.put(AGE, "Integer|age");
    // map.put("creation_time", "Date:|creation_time");
    map.put(IS_ADMIN, "Boolean|is_admin");
    map.put(BG_COLOR, "Color|bg_color");
    map.put(GENDER, "Gender|gender");
    map.put(BIRTHDAY, "Locale|birthday");
    map.put(ID, "Long|id");
    map.put(BLOG, "URI|blog");
    client.setProfileAttrs(map);
    client.setCallbackUrl(CALLBACK_URL);
    client.init();
    Field configurationField = OAuth20Client.class.getDeclaredField("configuration");
    configurationField.setAccessible(true);
    OAuth20Configuration configuration = (OAuth20Configuration) configurationField.get(client);
    GenericOAuth20ProfileDefinition profileDefinition = (GenericOAuth20ProfileDefinition) configuration.getProfileDefinition();
    Method getConverters = ProfileDefinition.class.getDeclaredMethod("getConverters");
    getConverters.setAccessible(true);
    Map<String, AttributeConverter<?>> converters = (Map<String, AttributeConverter<?>>) getConverters.invoke(profileDefinition);
    assertTrue(converters.get(AGE) instanceof IntegerConverter);
    assertTrue(converters.get(IS_ADMIN) instanceof BooleanConverter);
    assertTrue(converters.get(BG_COLOR) instanceof ColorConverter);
    assertTrue(converters.get(GENDER) instanceof GenderConverter);
    assertTrue(converters.get(BIRTHDAY) instanceof LocaleConverter);
    assertTrue(converters.get(ID) instanceof LongConverter);
    assertTrue(converters.get(BLOG) instanceof UrlConverter);
}
Also used : HashMap(java.util.HashMap) GenericOAuth20ProfileDefinition(org.pac4j.oauth.profile.generic.GenericOAuth20ProfileDefinition) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Map(java.util.Map) HashMap(java.util.HashMap) OAuth20Configuration(org.pac4j.oauth.config.OAuth20Configuration) Test(org.junit.Test)

Example 2 with OAuth20Configuration

use of org.pac4j.oauth.config.OAuth20Configuration in project pac4j by pac4j.

the class RunOAuth20Client method getClient.

@Override
protected IndirectClient getClient() {
    final OAuth20Configuration config = new OAuth20Configuration();
    config.setApi(GitHubApi.instance());
    config.setProfileDefinition(new GitHubProfileDefinition());
    config.setScope("user");
    config.setKey("62374f5573a89a8f9900");
    config.setSecret("01dd26d60447677ceb7399fb4c744f545bb86359");
    final OAuth20Client client = new OAuth20Client();
    client.setConfiguraton(config);
    client.setCallbackUrl(PAC4J_BASE_URL);
    return client;
}
Also used : OAuth20Client(org.pac4j.oauth.client.OAuth20Client) OAuth20Configuration(org.pac4j.oauth.config.OAuth20Configuration) GitHubProfileDefinition(org.pac4j.oauth.profile.github.GitHubProfileDefinition)

Example 3 with OAuth20Configuration

use of org.pac4j.oauth.config.OAuth20Configuration in project pac4j by pac4j.

the class FacebookProfileCreator method retrieveUserProfileFromToken.

@Override
protected FacebookProfile retrieveUserProfileFromToken(final WebContext context, final OAuth2AccessToken accessToken) {
    final OAuth20ProfileDefinition<FacebookProfile, OAuth20Configuration> profileDefinition = (OAuth20ProfileDefinition<FacebookProfile, OAuth20Configuration>) configuration.getProfileDefinition();
    final FacebookConfiguration facebookConfiguration = (FacebookConfiguration) configuration;
    final String profileUrl = profileDefinition.getProfileUrl(accessToken, configuration);
    final OAuth20Service service = this.configuration.buildService(context, client, null);
    String body = sendRequestForData(service, accessToken, profileUrl, Verb.GET);
    if (body == null) {
        throw new HttpCommunicationException("Not data found for accessToken: " + accessToken);
    }
    final FacebookProfile profile = profileDefinition.extractUserProfile(body);
    addAccessTokenToProfile(profile, accessToken);
    if (profile != null && facebookConfiguration.isRequiresExtendedToken()) {
        String url = CommonHelper.addParameter(EXCHANGE_TOKEN_URL, OAuthConstants.CLIENT_ID, configuration.getKey());
        url = CommonHelper.addParameter(url, OAuthConstants.CLIENT_SECRET, configuration.getSecret());
        url = addExchangeToken(url, accessToken);
        final OAuthRequest request = createOAuthRequest(url, Verb.GET);
        final long t0 = System.currentTimeMillis();
        final Response response;
        final int code;
        try {
            response = service.execute(request);
            body = response.getBody();
            code = response.getCode();
        } catch (final IOException | InterruptedException | ExecutionException e) {
            throw new HttpCommunicationException("Error getting body:" + e.getMessage());
        }
        final long t1 = System.currentTimeMillis();
        logger.debug("Request took: " + (t1 - t0) + " ms for: " + url);
        logger.debug("response code: {} / response body: {}", code, body);
        if (code == 200) {
            logger.debug("Retrieve extended token from  {}", body);
            final OAuth2AccessToken extendedAccessToken;
            try {
                extendedAccessToken = ((DefaultApi20) configuration.getApi()).getAccessTokenExtractor().extract(response);
            } catch (IOException | OAuthException ex) {
                throw new HttpCommunicationException("Error extracting token: " + ex.getMessage());
            }
            logger.debug("Extended token: {}", extendedAccessToken);
            addAccessTokenToProfile(profile, extendedAccessToken);
        } else {
            logger.error("Cannot get extended token: {} / {}", code, body);
        }
    }
    return profile;
}
Also used : HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) OAuthException(com.github.scribejava.core.exceptions.OAuthException) IOException(java.io.IOException) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) OAuth20ProfileDefinition(org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition) DefaultApi20(com.github.scribejava.core.builder.api.DefaultApi20) ExecutionException(java.util.concurrent.ExecutionException) OAuth20Configuration(org.pac4j.oauth.config.OAuth20Configuration)

Example 4 with OAuth20Configuration

use of org.pac4j.oauth.config.OAuth20Configuration in project cas by apereo.

the class DefaultDelegatedClientAuthenticationWebflowManagerTests method verifyOAuth2StoreOperation.

@Test
public void verifyOAuth2StoreOperation() throws Exception {
    val config = new OAuth20Configuration();
    config.setKey(UUID.randomUUID().toString());
    config.setSecret(UUID.randomUUID().toString());
    val client = new OAuth20Client();
    client.setConfiguration(config);
    val ticket = delegatedClientAuthenticationWebflowManager.store(context, client);
    assertNotNull(ticketRegistry.getTicket(ticket.getId()));
    val service = delegatedClientAuthenticationWebflowManager.retrieve(requestContext, context, client);
    assertNotNull(service);
    assertNull(ticketRegistry.getTicket(ticket.getId()));
}
Also used : lombok.val(lombok.val) OAuth20Client(org.pac4j.oauth.client.OAuth20Client) OAuth20Configuration(org.pac4j.oauth.config.OAuth20Configuration) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

OAuth20Configuration (org.pac4j.oauth.config.OAuth20Configuration)4 OAuth20Client (org.pac4j.oauth.client.OAuth20Client)2 DefaultApi20 (com.github.scribejava.core.builder.api.DefaultApi20)1 OAuthException (com.github.scribejava.core.exceptions.OAuthException)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 lombok.val (lombok.val)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 HttpCommunicationException (org.pac4j.core.exception.HttpCommunicationException)1 OAuth20ProfileDefinition (org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition)1 GenericOAuth20ProfileDefinition (org.pac4j.oauth.profile.generic.GenericOAuth20ProfileDefinition)1 GitHubProfileDefinition (org.pac4j.oauth.profile.github.GitHubProfileDefinition)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1