use of com.github.philippheuer.credentialmanager.domain.OAuth2Credential in project pgda by jonteohr.
the class Identity method refreshToken.
public static OAuth2Credential refreshToken(OAuth2Credential credential) {
OAuth2Credential newOauth = identityProvider.refreshCredential(getCredential(credential)).get();
PropertyHandler props = new PropertyHandler();
props.setProperty("access_token", newOauth.getAccessToken());
props.setProperty("refresh_token", newOauth.getRefreshToken());
// revoke the old one for safety
identityProvider.revokeCredential(getCredential(credential));
saveOAuth(props.getPropertyValue("access_token"), props.getPropertyValue("refresh_token"));
return newOauth;
}
use of com.github.philippheuer.credentialmanager.domain.OAuth2Credential in project pgda by jonteohr.
the class Identity method getAccessToken.
public static String getAccessToken(OAuth2Credential credential) {
Identity identity = new Identity();
OAuth2Credential oauth = identity.getCredential(credential);
return oauth.getAccessToken();
}
use of com.github.philippheuer.credentialmanager.domain.OAuth2Credential in project pgda by jonteohr.
the class RefreshToken method run.
@Override
public void run() {
OAuth2Credential oldOauth = Twitch.getOAuth2();
Twitch.setOAuth2(Identity.getCredential(Identity.refreshToken(oldOauth)));
System.out.println("###########");
System.out.println("Refreshed token!");
System.out.println("###########");
}
use of com.github.philippheuer.credentialmanager.domain.OAuth2Credential in project spotifybot by NotEchoDE.
the class CallbackController method twitchCallback.
@GetMapping("/twitch/callback")
public String twitchCallback(@RequestParam(name = "code", defaultValue = "null") String code, HttpServletResponse response, Model model) {
if (code.equals("null"))
return "redirect:/erorr?code=503";
OAuth2Credential credentialByCode = oAuth2IdentityProvider.getCredentialByCode(code);
List<User> users = twitchClient.getHelix().getUsers(credentialByCode.getAccessToken(), null, null).execute().getUsers();
if (users.size() == 0)
return "redirect:/erorr?code=503";
User twitchUser = users.get(0);
BotUser user = repository.findByTwitchId(twitchUser.getId());
if (user == null) {
user = new BotUser(0L, twitchUser.getId(), DefaultModules.defaultList(), Collections.singletonList(new TokenPair(0L, credentialByCode.getAccessToken(), credentialByCode.getRefreshToken(), TokenType.TWITCH)));
} else {
user.twitchTokens().setAccessToken(credentialByCode.getAccessToken());
user.twitchTokens().setRefreshToken(credentialByCode.getRefreshToken());
}
repository.saveAndFlush(user);
Cookie cookie = new Cookie("session", sessionManagementService.createSession(user));
cookie.setPath("/");
cookie.setMaxAge(3600);
response.addCookie(cookie);
return "redirect:/dashboard";
}
use of com.github.philippheuer.credentialmanager.domain.OAuth2Credential in project spotifybot by NotEchoDE.
the class BotInstance method updateTwitchToken.
private void updateTwitchToken(TokenPair tokenPair) {
OAuth2Credential credential = new OAuth2Credential("twitch", tokenPair.getAccessToken());
credential.setRefreshToken(tokenPair.getRefreshToken());
Optional<OAuth2Credential> credentialOptional = context.getBean(OAuth2IdentityProvider.class).refreshCredential(credential);
if (credentialOptional.isPresent()) {
credential = credentialOptional.get();
User twitchUser = client.getHelix().getUsers(credential.getAccessToken(), null, null).execute().getUsers().get(0);
if (tokenPair.getTokenType().equals(TokenType.TWITCH)) {
id = twitchUser.getId();
login = twitchUser.getLogin();
}
tokenPair.setAccessToken(credential.getAccessToken());
tokenPair.setRefreshToken(credential.getRefreshToken());
Logger.log(LogType.DEBUG, "[" + user.getId() + "] Got new Twitch Token(" + tokenPair.getTokenType().name() + ") for " + twitchUser.getLogin() + " | " + credential.getAccessToken(), "Twitch", tokenPair.getTokenType().name(), login, credential.getAccessToken());
}
}
Aggregations