use of com.github.scribejava.core.oauth.OAuth20Service in project sonarqube by SonarSource.
the class BitbucketIdentityProvider method init.
@Override
public void init(InitContext context) {
String state = context.generateCsrfState();
OAuth20Service scribe = newScribeBuilder(context).build(scribeApi);
String url = scribe.getAuthorizationUrl(state);
context.redirectTo(url);
}
use of com.github.scribejava.core.oauth.OAuth20Service in project alf.io by alfio-event.
the class MollieConnectManager method storeConnectedAccountId.
@Override
public AccessTokenResponseDetails storeConnectedAccountId(String code, int organizationId) {
try {
ConfigurationLevel configurationLevel = ConfigurationLevel.organization(organizationId);
var options = configurationManager.getFor(Set.of(MOLLIE_API_KEY, MOLLIE_CONNECT_CLIENT_ID, MOLLIE_CONNECT_CLIENT_SECRET, MOLLIE_CONNECT_CALLBACK, BASE_URL), configurationLevel);
OAuth20Service service = new ServiceBuilder(options.get(MOLLIE_CONNECT_CLIENT_ID).getRequiredValue()).apiSecret(options.get(MOLLIE_CONNECT_CLIENT_SECRET).getRequiredValue()).callback(options.get(MOLLIE_CONNECT_CALLBACK).getRequiredValue()).build(new MollieConnectApi());
OAuth2AccessToken accessTokenResponse = service.getAccessToken(code);
var refreshToken = accessTokenResponse.getRefreshToken();
if (refreshToken != null) {
configurationManager.saveConfig(Configuration.from(organizationId, MOLLIE_CONNECT_REFRESH_TOKEN), refreshToken);
}
return new AccessTokenResponseDetails(accessTokenResponse.getAccessToken(), refreshToken, null, true);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.warn("Request interrupted while retrieving access token", e);
return new AccessTokenResponseDetails(null, null, e.getMessage(), false);
} catch (Exception e) {
log.warn("Got exception while retrieving access token", e);
return new AccessTokenResponseDetails(null, null, e.getMessage(), false);
}
}
use of com.github.scribejava.core.oauth.OAuth20Service in project android-oauth-handler by codepath.
the class OAuthAsyncHttpClient method fetchAccessToken.
// Get the access token by exchanging the requestToken to the defined URL
// Once receiving the access token, fires the onReceivedAccessToken method on the handler
public void fetchAccessToken(final Token requestToken, final Uri uri) {
new AsyncSimpleTask(new AsyncSimpleTask.AsyncSimpleTaskHandler() {
Exception e = null;
public void doInBackground() {
// Fetch the verifier code from redirect url parameters
Uri authorizedUri = uri;
try {
if (service.getVersion() == "1.0") {
if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
String oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
OAuth10aService oAuth10aService = (OAuth10aService) service;
accessToken = oAuth10aService.getAccessToken(oAuth1RequestToken, oauth_verifier);
} else {
// verifier was null
throw new OAuthException("No verifier code was returned with uri '" + uri + "' " + "and access token cannot be retrieved");
}
} else if (service.getVersion() == "2.0") {
if (authorizedUri.getQuery().contains(OAuthConstants.CODE)) {
String code = authorizedUri.getQueryParameter(OAuthConstants.CODE);
OAuth20Service oAuth20Service = (OAuth20Service) service;
accessToken = oAuth20Service.getAccessToken(code);
} else {
// verifier was null
throw new OAuthException("No code was returned with uri '" + uri + "' " + "and access token cannot be retrieved");
}
}
} catch (Exception e) {
this.e = e;
}
}
public void onPostExecute() {
if (e != null) {
handler.onFailure(e);
} else {
setAccessToken(accessToken);
handler.onReceivedAccessToken(accessToken, service.getVersion());
}
}
});
}
use of com.github.scribejava.core.oauth.OAuth20Service in project runelite by runelite.
the class AccountService method login.
@RequestMapping("/login")
public OAuthResponse login() {
UUID uuid = UUID.randomUUID();
State state = new State();
state.setUuid(uuid);
state.setApiVersion(RuneLiteAPI.getVersion());
OAuth20Service service = new ServiceBuilder().apiKey(oauthClientId).apiSecret(oauthClientSecret).scope(SCOPE).callback(RL_OAUTH_URL).state(gson.toJson(state)).build(GoogleApi20.instance());
String authorizationUrl = service.getAuthorizationUrl();
OAuthResponse lr = new OAuthResponse();
lr.setOauthUrl(authorizationUrl);
lr.setUid(uuid);
return lr;
}
use of com.github.scribejava.core.oauth.OAuth20Service in project runelite by runelite.
the class AccountService method callback.
@RequestMapping("/callback")
public Object callback(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String error, @RequestParam String code, @RequestParam("state") String stateStr) throws InterruptedException, ExecutionException, IOException {
if (error != null) {
logger.info("Error in oauth callback: {}", error);
return null;
}
State state = gson.fromJson(stateStr, State.class);
logger.info("Got authorization code {} for uuid {}", code, state.getUuid());
OAuth20Service service = new ServiceBuilder().apiKey(oauthClientId).apiSecret(oauthClientSecret).scope(SCOPE).callback(RL_OAUTH_URL).state(gson.toJson(state)).build(GoogleApi20.instance());
OAuth2AccessToken accessToken = service.getAccessToken(code);
// Access user info
OAuthRequest orequest = new OAuthRequest(Verb.GET, USERINFO);
service.signRequest(accessToken, orequest);
Response oresponse = service.execute(orequest);
if (oresponse.getCode() / 100 != 2) {
// Could be a forged result
return null;
}
UserInfo userInfo = gson.fromJson(oresponse.getBody(), UserInfo.class);
logger.info("Got user info: {}", userInfo);
try (Connection con = sql2o.open()) {
con.createQuery("insert ignore into users (username) values (:username)").addParameter("username", userInfo.getEmail()).executeUpdate();
UserEntry user = con.createQuery("select id from users where username = :username").addParameter("username", userInfo.getEmail()).executeAndFetchFirst(UserEntry.class);
if (user == null) {
logger.warn("Unable to find newly created user session");
// that's weird
return null;
}
// insert session
con.createQuery("insert ignore into sessions (user, uuid) values (:user, :uuid)").addParameter("user", user.getId()).addParameter("uuid", state.getUuid().toString()).executeUpdate();
logger.info("Created session for user {}", userInfo.getEmail());
}
response.sendRedirect(RL_REDIR);
notifySession(state.getUuid(), userInfo.getEmail());
return "";
}
Aggregations