use of com.github.scribejava.core.model.OAuthRequest in project dataverse by IQSS.
the class AbstractOAuth2AuthenticationProvider method getUserRecord.
public OAuth2UserRecord getUserRecord(String code, String state, String redirectUrl) throws IOException, OAuth2Exception {
OAuth20Service service = getService(state, redirectUrl);
OAuth2AccessToken accessToken = service.getAccessToken(code);
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);
return new OAuth2UserRecord(getId(), parsed.userIdInProvider, parsed.username, OAuth2TokenData.from(accessToken), parsed.displayInfo, parsed.emails);
} else {
throw new OAuth2Exception(responseCode, body, "Error getting the user info record.");
}
}
use of com.github.scribejava.core.model.OAuthRequest in project dataverse by IQSS.
the class OrcidOAuth2AP method getOrganizationalData.
protected AuthenticatedUserDisplayInfo getOrganizationalData(String userEndpoint, String accessToken, OAuth20Service service) throws IOException {
final OAuthRequest request = new OAuthRequest(Verb.GET, userEndpoint.replace("/person", "/employments"), service);
request.addHeader("Authorization", "Bearer " + accessToken);
request.setCharset("UTF-8");
final Response response = request.send();
int responseCode = response.getCode();
final String responseBody = response.getBody();
if (responseCode != 200) {
// This is bad, but not bad enough to stop a signup/in process.
logger.log(Level.WARNING, "Cannot get affiliation data from ORCiD. Response code: {0} body:\n{1}\n/body", new Object[] { responseCode, responseBody });
return null;
} else {
return parseActivitiesResponse(responseBody);
}
}
use of com.github.scribejava.core.model.OAuthRequest in project legendarybot by greatman.
the class WoWLinkPlugin method start.
@Override
public void start() {
// Load the configuration
props = new Properties();
try {
props.load(new FileInputStream("app.properties"));
} catch (java.io.IOException e) {
e.printStackTrace();
getBot().getStacktraceHandler().sendStacktrace(e);
}
path("/auth", () -> get("/battlenetcallback", (req, res) -> {
String state = req.queryParams("state");
String region = state.split(":")[0];
OAuth20Service service = new ServiceBuilder(props.getProperty("battlenetoauth.key")).apiSecret(props.getProperty("battlenetoauth.secret")).scope("wow.profile").callback("https://legendarybot.greatmancode.com/auth/battlenetcallback").build(new OAuthBattleNetApi(region));
String oAuthCode = req.queryParams("code");
// TODO: Save oauth code to do a character refresh.
OAuth2AccessToken token = service.getAccessToken(oAuthCode);
OAuthRequest request = new OAuthRequest(Verb.GET, "https://" + region + ".api.battle.net/wow/user/characters");
service.signRequest(token, request);
Response response = service.execute(request);
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(response.getBody());
JSONArray charactersArray = (JSONArray) obj.get("characters");
List<WoWCharacter> characterList = new ArrayList<>();
charactersArray.forEach((c) -> {
JSONObject jsonObject = (JSONObject) c;
if (jsonObject.containsKey("guild")) {
characterList.add(new WoWCharacter((String) jsonObject.get("name"), ((String) jsonObject.get("realm")).toLowerCase(), (String) jsonObject.get("guild"), region, HeroClass.values()[((Long) jsonObject.get("class")).intValue()]));
log.info("User " + state.split(":")[1] + " user have the character " + jsonObject.get("name") + " in guild " + jsonObject.get("guild"));
}
});
if (characterList.size() > 0) {
MongoCollection<Document> collection = getBot().getMongoDatabase().getCollection(MONGO_WOW_CHARACTERS_COLLECTION);
characterList.forEach((c) -> collection.updateOne(and(eq("region", c.getRegion()), eq("realm", c.getRealm()), eq("name", c.getCharacterName())), and(set("guild", c.getGuild()), set("owner", state.split(":")[1])), new UpdateOptions().upsert(true)));
}
return "Your WoW characters are now synced to LegendaryBot!";
}));
getBot().getCommandHandler().addCommand("linkwowchars", new LinkWoWCharsCommand(this), "World of Warcraft Character");
getBot().getCommandHandler().addCommand("guildchars", new GuildCharsCommand(this), "World of Warcraft Character");
getBot().getCommandHandler().addCommand("setmainchar", new SetMainCharacterCommand(this), "World of Warcraft Character");
getBot().getCommandHandler().addCommand("enableautorank", new EnableAutoRankCommand(this), "WoW Admin Commands");
getBot().getCommandHandler().addCommand("disableautorank", new DisableAutoRankCommand(this), "WoW Admin Commands");
getBot().getCommandHandler().addCommand("setwowrank", new SetWoWRankCommand(this), "WoW Admin Commands");
getBot().getCommandHandler().addCommand("syncrank", new SyncRankCommand(this), "World of Warcraft Character");
getBot().getCommandHandler().addCommand("syncguild", new SyncGuildCommand(this), "WoW Admin Commands");
getBot().getCommandHandler().addCommand("enableautorankupdate", new EnableAutoRankUpdateCommand(this), "WoW Admin Commands");
getBot().getCommandHandler().addCommand("disableautorankupdate", new DisableAutoRankUpdateCommand(this), "WoW Admin Commands");
// We load the scheduler
getBot().getJDA().forEach((jda -> {
jda.getGuilds().forEach(guild -> {
if (getBot().getGuildSettings(guild).getSetting(SETTING_SCHEDULER) != null && getBot().getGuildSettings(guild).getSetting(SETTING_RANKSET_ENABLED) != null) {
scheduler.put(guild.getId(), new SyncRankScheduler(this, guild));
}
});
}));
}
use of com.github.scribejava.core.model.OAuthRequest in project sonarqube by SonarSource.
the class BitbucketIdentityProvider method requestUser.
private GsonUser requestUser(OAuth20Service service, OAuth2AccessToken accessToken) throws InterruptedException, ExecutionException, IOException {
OAuthRequest userRequest = new OAuthRequest(Verb.GET, settings.apiURL() + "2.0/user");
service.signRequest(accessToken, userRequest);
Response userResponse = service.execute(userRequest);
if (!userResponse.isSuccessful()) {
throw new IllegalStateException(format("Can not get Bitbucket user profile. HTTP code: %s, response: %s", userResponse.getCode(), userResponse.getBody()));
}
String userResponseBody = userResponse.getBody();
return GsonUser.parse(userResponseBody);
}
use of com.github.scribejava.core.model.OAuthRequest in project sonarqube by SonarSource.
the class BitbucketIdentityProvider method requestEmails.
@CheckForNull
private GsonEmails requestEmails(OAuth20Service service, OAuth2AccessToken accessToken) throws InterruptedException, ExecutionException, IOException {
OAuthRequest userRequest = new OAuthRequest(Verb.GET, settings.apiURL() + "2.0/user/emails");
service.signRequest(accessToken, userRequest);
Response emailsResponse = service.execute(userRequest);
if (emailsResponse.isSuccessful()) {
return GsonEmails.parse(emailsResponse.getBody());
}
return null;
}
Aggregations