use of com.github.scribejava.core.oauth.OAuth20Service in project sonarqube by SonarSource.
the class GitHubIdentityProvider method init.
@Override
public void init(InitContext context) {
String state = context.generateCsrfState();
OAuth20Service scribe = newScribeBuilder(context).defaultScope(getScope()).build(scribeApi);
String url = scribe.getAuthorizationUrl(state);
context.redirectTo(url);
}
use of com.github.scribejava.core.oauth.OAuth20Service in project sonarqube by SonarSource.
the class GitLabIdentityProvider method onCallback.
private void onCallback(CallbackContext context) throws InterruptedException, ExecutionException, IOException {
HttpServletRequest request = context.getRequest();
OAuth20Service scribe = newScribeBuilder(context, gitLabSettings.syncUserGroups()).build(scribeApi);
String code = request.getParameter(OAuthConstants.CODE);
OAuth2AccessToken accessToken = scribe.getAccessToken(code);
GsonUser user = gitLabRestClient.getUser(scribe, accessToken);
UserIdentity.Builder builder = UserIdentity.builder().setProviderId(Long.toString(user.getId())).setProviderLogin(user.getUsername()).setName(user.getName()).setEmail(user.getEmail());
if (gitLabSettings.syncUserGroups()) {
builder.setGroups(getGroups(scribe, accessToken));
}
context.authenticate(builder.build());
context.redirectToRequestedPage();
}
use of com.github.scribejava.core.oauth.OAuth20Service in project scribejava by scribejava.
the class OkHttpHttpClientTest method setUp.
@Before
public void setUp() {
final HttpClient client = new OkHttpHttpClient(new OkHttpClient());
oAuthService = new OAuth20Service(null, new OAuthConfig("test", "test", null, null, null, null, null, null, null, null, client));
}
use of com.github.scribejava.core.oauth.OAuth20Service 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 com.github.scribejava.core.oauth.OAuth20Service in project dataverse by IQSS.
the class OrcidOAuth2AP method getUserRecord.
@Override
public OAuth2UserRecord getUserRecord(String code, String state, String redirectUrl) throws IOException, OAuth2Exception {
OAuth20Service service = getService(state, redirectUrl);
OAuth2AccessToken accessToken = service.getAccessToken(code);
if (!accessToken.getScope().contains(scope)) {
// We did not get the permissions on the scope we need. Abort and inform the user.
throw new OAuth2Exception(200, BundleUtil.getStringFromBundle("auth.providers.orcid.insufficientScope"), "");
}
String orcidNumber = extractOrcidNumber(accessToken.getRawResponse());
final String userEndpoint = getUserEndpoint(accessToken);
final OAuthRequest request = new OAuthRequest(Verb.GET, userEndpoint, service);
request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken());
request.setCharset("UTF-8");
final Response response = request.send();
int responseCode = response.getCode();
final String body = response.getBody();
logger.log(Level.FINE, "In getUserRecord. Body: {0}", body);
if (responseCode == 200) {
final ParsedUserResponse parsed = parseUserResponse(body);
AuthenticatedUserDisplayInfo orgData = getOrganizationalData(userEndpoint, accessToken.getAccessToken(), service);
parsed.displayInfo.setAffiliation(orgData.getAffiliation());
parsed.displayInfo.setPosition(orgData.getPosition());
return new OAuth2UserRecord(getId(), orcidNumber, parsed.username, OAuth2TokenData.from(accessToken), parsed.displayInfo, parsed.emails);
} else {
throw new OAuth2Exception(responseCode, body, "Error getting the user info record.");
}
}
Aggregations