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);
}
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;
}
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;
}
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()));
}
Aggregations