Search in sources :

Example 1 with TokenPair

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";
}
Also used : BotUser(de.notecho.spotify.database.user.entities.BotUser) TokenPair(de.notecho.spotify.database.user.entities.TokenPair) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials) GetMapping(org.springframework.web.bind.annotation.GetMapping) SneakyThrows(lombok.SneakyThrows)

Example 2 with TokenPair

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";
}
Also used : Cookie(javax.servlet.http.Cookie) BotUser(de.notecho.spotify.database.user.entities.BotUser) OAuth2Credential(com.github.philippheuer.credentialmanager.domain.OAuth2Credential) BotUser(de.notecho.spotify.database.user.entities.BotUser) User(com.github.twitch4j.helix.domain.User) TokenPair(de.notecho.spotify.database.user.entities.TokenPair) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with TokenPair

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);
}
Also used : UserRepository(de.notecho.spotify.database.user.repository.UserRepository) BaseModule(de.notecho.spotify.bot.modules.BaseModule) BadRequestException(se.michaelthelin.spotify.exceptions.detailed.BadRequestException) IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) AuthorizationCodeRefreshRequest(se.michaelthelin.spotify.requests.authorization.authorization_code.AuthorizationCodeRefreshRequest) TokenPair(de.notecho.spotify.database.user.entities.TokenPair) BotInstanceManagementService(de.notecho.spotify.bot.BotInstanceManagementService) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)

Aggregations

TokenPair (de.notecho.spotify.database.user.entities.TokenPair)3 BotUser (de.notecho.spotify.database.user.entities.BotUser)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 AuthorizationCodeCredentials (se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)2 OAuth2Credential (com.github.philippheuer.credentialmanager.domain.OAuth2Credential)1 User (com.github.twitch4j.helix.domain.User)1 BotInstanceManagementService (de.notecho.spotify.bot.BotInstanceManagementService)1 BaseModule (de.notecho.spotify.bot.modules.BaseModule)1 UserRepository (de.notecho.spotify.database.user.repository.UserRepository)1 IOException (java.io.IOException)1 Cookie (javax.servlet.http.Cookie)1 SneakyThrows (lombok.SneakyThrows)1 ParseException (org.apache.hc.core5.http.ParseException)1 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)1 BadRequestException (se.michaelthelin.spotify.exceptions.detailed.BadRequestException)1 AuthorizationCodeRefreshRequest (se.michaelthelin.spotify.requests.authorization.authorization_code.AuthorizationCodeRefreshRequest)1