Search in sources :

Example 1 with OAuth2Credential

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;
}
Also used : OAuth2Credential(com.github.philippheuer.credentialmanager.domain.OAuth2Credential) PropertyHandler(com.github.jonteohr.tejbz.PropertyHandler)

Example 2 with OAuth2Credential

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();
}
Also used : OAuth2Credential(com.github.philippheuer.credentialmanager.domain.OAuth2Credential)

Example 3 with OAuth2Credential

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("###########");
}
Also used : OAuth2Credential(com.github.philippheuer.credentialmanager.domain.OAuth2Credential)

Example 4 with OAuth2Credential

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";
}
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 5 with OAuth2Credential

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());
    }
}
Also used : OAuth2Credential(com.github.philippheuer.credentialmanager.domain.OAuth2Credential) BotUser(de.notecho.spotify.database.user.entities.BotUser) User(com.github.twitch4j.helix.domain.User) OAuth2IdentityProvider(com.github.philippheuer.credentialmanager.identityprovider.OAuth2IdentityProvider)

Aggregations

OAuth2Credential (com.github.philippheuer.credentialmanager.domain.OAuth2Credential)5 User (com.github.twitch4j.helix.domain.User)2 BotUser (de.notecho.spotify.database.user.entities.BotUser)2 PropertyHandler (com.github.jonteohr.tejbz.PropertyHandler)1 OAuth2IdentityProvider (com.github.philippheuer.credentialmanager.identityprovider.OAuth2IdentityProvider)1 TokenPair (de.notecho.spotify.database.user.entities.TokenPair)1 Cookie (javax.servlet.http.Cookie)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1