use of de.notecho.spotify.database.user.entities.TokenPair in project spotifybot by NotEchoDE.
the class CallbackController method spotifyCallback.
@SneakyThrows
@GetMapping("/spotify/callback")
public String spotifyCallback(@RequestParam(name = "code", defaultValue = "null") String code, @CookieValue(name = "session", defaultValue = "null") String session, Model model) {
if (code.equals("null"))
return "redirect:/erorr?code=503";
if (sessionManagementService.getUser(session) == null)
return "redirect:/erorr?code=503";
BotUser user = sessionManagementService.getUser(session);
AuthorizationCodeCredentials codeCredentials = spotifyApi.authorizationCode(code).build().execute();
if (user.spotifyTokens() != null) {
user.spotifyTokens().setAccessToken(codeCredentials.getAccessToken());
user.spotifyTokens().setRefreshToken(codeCredentials.getRefreshToken());
} else
user.addTokenPair(new TokenPair(0L, codeCredentials.getAccessToken(), codeCredentials.getRefreshToken(), TokenType.SPOTIFY));
repository.saveAndFlush(user);
botInstanceManagementService.startInstance(user);
return "redirect:/dashboard";
}
use of de.notecho.spotify.database.user.entities.TokenPair 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 de.notecho.spotify.database.user.entities.TokenPair in project spotifybot by NotEchoDE.
the class BotInstance method updateTokens.
private void updateTokens() {
if (user.spotifyTokens() == null) {
context.getBean(BotInstanceManagementService.class).stopInstance(user);
return;
}
updateTwitchToken(user.twitchTokens());
if (user.chatAccountTokens() != null)
updateTwitchToken(user.chatAccountTokens());
try {
this.spotifyApi.setRefreshToken(user.spotifyTokens().getRefreshToken());
AuthorizationCodeRefreshRequest authorizationCodeRefreshRequest = spotifyApi.authorizationCodeRefresh().build();
final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeRefreshRequest.execute();
spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
spotifyApi.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
user.spotifyTokens().setAccessToken(authorizationCodeCredentials.getAccessToken());
Logger.log(LogType.DEBUG, "[" + user.getId() + "] Got new Spotify Token for " + login + ", expires in: " + authorizationCodeCredentials.getExpiresIn() + " | " + authorizationCodeCredentials.getAccessToken().substring(0, 10) + "...", "Spotify", login, String.valueOf(authorizationCodeCredentials.getExpiresIn()), authorizationCodeCredentials.getAccessToken().substring(0, 10) + "...");
} catch (BadRequestException e) {
TokenPair spotifyTokens = user.spotifyTokens();
user.getTokenPairs().remove(spotifyTokens);
context.getBean(UserRepository.class).saveAndFlush(user);
context.getBean(TokenPairRepository.class).delete(spotifyTokens);
Logger.log(LogType.INFO, "[" + user.getId() + "] " + login + " revoked his access token so it was removed from the database.", login, "revoked", "database");
for (BaseModule module : this.modules) module.unregister(client);
client.getChat().leaveChannel(this.login);
context.getBean(BotInstanceManagementService.class).stopInstance(user);
return;
} catch (IOException | SpotifyWebApiException | ParseException e) {
e.printStackTrace();
}
context.getBean(UserRepository.class).saveAndFlush(user);
nextCheck = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(50);
}
Aggregations