1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

refactor: use helix methods for user tokens

This commit is contained in:
Ash Keel 2022-12-03 16:16:32 +01:00
parent d0bf35d0b1
commit f42869b34e
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E

View file

@ -2,20 +2,15 @@ package twitch
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/url"
"time" "time"
jsoniter "github.com/json-iterator/go"
"github.com/nicklaw5/helix/v2" "github.com/nicklaw5/helix/v2"
) )
type AuthResponse struct { type AuthResponse struct {
AccessToken string `json:"access_token"` AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"` RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"` ExpiresIn int `json:"expires_in"`
Scope []string `json:"scope"` Scope []string `json:"scope"`
Time time.Time Time time.Time
@ -37,12 +32,13 @@ func (c *Client) GetUserClient() (*helix.Client, error) {
// Handle token expiration // Handle token expiration
if time.Now().After(authResp.Time.Add(time.Duration(authResp.ExpiresIn) * time.Second)) { if time.Now().After(authResp.Time.Add(time.Duration(authResp.ExpiresIn) * time.Second)) {
// Refresh tokens // Refresh tokens
refreshed, err := c.refreshAccessToken(authResp.RefreshToken) refreshed, err := c.API.RefreshUserAccessToken(authResp.RefreshToken)
if err != nil { if err != nil {
return nil, err return nil, err
} }
authResp.AccessToken = refreshed.AccessToken authResp.AccessToken = refreshed.Data.AccessToken
authResp.RefreshToken = refreshed.RefreshToken authResp.RefreshToken = refreshed.Data.RefreshToken
authResp.Time = time.Now().Add(time.Duration(refreshed.Data.ExpiresIn) * time.Second)
// Save new token pair // Save new token pair
err = c.db.PutJSON(AuthKey, authResp) err = c.db.PutJSON(AuthKey, authResp)
@ -51,9 +47,10 @@ func (c *Client) GetUserClient() (*helix.Client, error) {
} }
} }
config := c.Config.Get()
return helix.NewClient(&helix.Options{ return helix.NewClient(&helix.Options{
ClientID: c.Config.APIClientID, ClientID: config.APIClientID,
ClientSecret: c.Config.APIClientSecret, ClientSecret: config.APIClientSecret,
UserAccessToken: authResp.AccessToken, UserAccessToken: authResp.AccessToken,
}) })
} }
@ -83,38 +80,21 @@ func (c *Client) AuthorizeCallback(w http.ResponseWriter, req *http.Request) {
http.Error(w, "missing code", http.StatusBadRequest) http.Error(w, "missing code", http.StatusBadRequest)
return return
} }
redirectURI, err := c.getRedirectURI()
if err != nil {
http.Error(w, "failed getting redirect uri", http.StatusInternalServerError)
return
}
// Exchange code for access/refresh tokens // Exchange code for access/refresh tokens
query := url.Values{ userTokenResponse, err := c.API.RequestUserAccessToken(code)
"client_id": {c.Config.APIClientID},
"client_secret": {c.Config.APIClientSecret},
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {redirectURI},
}
authRequest, err := http.NewRequest("POST", "https://id.twitch.tv/oauth2/token?"+query.Encode(), nil)
if err != nil { if err != nil {
http.Error(w, "failed creating auth request: "+err.Error(), http.StatusInternalServerError) http.Error(w, "failed auth token request: "+err.Error(), http.StatusInternalServerError)
return return
} }
resp, err := http.DefaultClient.Do(authRequest)
if err != nil { err = c.db.PutJSON(AuthKey, AuthResponse{
http.Error(w, "failed sending auth request: "+err.Error(), http.StatusInternalServerError) AccessToken: userTokenResponse.Data.AccessToken,
return RefreshToken: userTokenResponse.Data.RefreshToken,
} ExpiresIn: userTokenResponse.Data.ExpiresIn,
defer resp.Body.Close() Scope: userTokenResponse.Data.Scopes,
var authResp AuthResponse Time: time.Now(),
err = json.NewDecoder(resp.Body).Decode(&authResp) })
if err != nil && err != io.EOF {
http.Error(w, "failed reading auth response: "+err.Error(), http.StatusInternalServerError)
return
}
authResp.Time = time.Now()
err = c.db.PutJSON(AuthKey, authResp)
if err != nil { if err != nil {
http.Error(w, "error saving auth data for user: "+err.Error(), http.StatusInternalServerError) http.Error(w, "error saving auth data for user: "+err.Error(), http.StatusInternalServerError)
return return