2022-11-23 21:22:49 +00:00
|
|
|
package twitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-02-25 20:20:08 +00:00
|
|
|
"slices"
|
2022-11-23 21:22:49 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nicklaw5/helix/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
Scope []string `json:"scope"`
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:20:08 +00:00
|
|
|
var scopes = []string{
|
|
|
|
"bits:read",
|
|
|
|
"channel:bot",
|
|
|
|
"channel:moderate",
|
|
|
|
"channel:read:hype_train",
|
|
|
|
"channel:read:polls",
|
|
|
|
"channel:read:predictions",
|
|
|
|
"channel:read:redemptions",
|
|
|
|
"channel:read:subscriptions",
|
|
|
|
"chat:edit",
|
|
|
|
"chat:read",
|
|
|
|
"moderator:manage:announcements",
|
|
|
|
"moderator:read:chatters",
|
|
|
|
"moderator:read:followers",
|
|
|
|
"user_read",
|
|
|
|
"user:bot",
|
|
|
|
"user:manage:whispers",
|
|
|
|
"user:read:chat",
|
|
|
|
"whispers:edit",
|
|
|
|
"whispers:read",
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:22:49 +00:00
|
|
|
func (c *Client) GetAuthorizationURL() string {
|
2022-12-22 12:35:30 +00:00
|
|
|
if c.API == nil {
|
|
|
|
return "twitch-not-configured"
|
|
|
|
}
|
2022-11-23 21:22:49 +00:00
|
|
|
return c.API.GetAuthorizationURL(&helix.AuthorizationURLParams{
|
|
|
|
ResponseType: "code",
|
2024-02-25 20:20:08 +00:00
|
|
|
Scopes: scopes,
|
2022-11-23 21:22:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:20:08 +00:00
|
|
|
// CheckScopes checks if the user has authorized all required scopes
|
|
|
|
// Normally this would be the case but between versions strimertul has changed
|
|
|
|
// the required scopes, and it's possible that some users have not re-authorized
|
|
|
|
// the application with the new scopes.
|
|
|
|
func (c *Client) CheckScopes() (bool, error) {
|
|
|
|
var authResp AuthResponse
|
|
|
|
if err := c.db.GetJSON(AuthKey, &authResp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort scopes for comparison
|
|
|
|
slices.Sort(authResp.Scope)
|
|
|
|
|
|
|
|
return slices.Equal(scopes, authResp.Scope), nil
|
|
|
|
}
|
|
|
|
|
2023-03-05 19:11:19 +00:00
|
|
|
func (c *Client) GetUserClient(forceRefresh bool) (*helix.Client, error) {
|
2022-11-23 21:22:49 +00:00
|
|
|
var authResp AuthResponse
|
2024-02-25 20:20:08 +00:00
|
|
|
if err := c.db.GetJSON(AuthKey, &authResp); err != nil {
|
2022-11-24 12:16:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-25 20:20:08 +00:00
|
|
|
|
2022-11-24 12:16:08 +00:00
|
|
|
// Handle token expiration
|
2023-03-05 19:11:19 +00:00
|
|
|
if forceRefresh || time.Now().After(authResp.Time.Add(time.Duration(authResp.ExpiresIn)*time.Second)) {
|
2022-11-24 12:16:08 +00:00
|
|
|
// Refresh tokens
|
2022-12-03 15:16:32 +00:00
|
|
|
refreshed, err := c.API.RefreshUserAccessToken(authResp.RefreshToken)
|
2022-11-24 12:16:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-03 15:16:32 +00:00
|
|
|
authResp.AccessToken = refreshed.Data.AccessToken
|
|
|
|
authResp.RefreshToken = refreshed.Data.RefreshToken
|
|
|
|
authResp.Time = time.Now().Add(time.Duration(refreshed.Data.ExpiresIn) * time.Second)
|
2022-11-24 12:16:08 +00:00
|
|
|
|
|
|
|
// Save new token pair
|
|
|
|
err = c.db.PutJSON(AuthKey, authResp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-23 21:22:49 +00:00
|
|
|
}
|
2022-11-24 12:16:08 +00:00
|
|
|
|
2022-12-03 15:16:32 +00:00
|
|
|
config := c.Config.Get()
|
2022-11-24 12:16:08 +00:00
|
|
|
return helix.NewClient(&helix.Options{
|
2022-12-03 15:16:32 +00:00
|
|
|
ClientID: config.APIClientID,
|
|
|
|
ClientSecret: config.APIClientSecret,
|
2022-11-23 21:22:49 +00:00
|
|
|
UserAccessToken: authResp.AccessToken,
|
|
|
|
})
|
2022-11-24 12:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetLoggedUser() (helix.User, error) {
|
2023-02-02 20:24:14 +00:00
|
|
|
if c.User.ID != "" {
|
|
|
|
return c.User, nil
|
|
|
|
}
|
|
|
|
|
2023-03-05 19:11:19 +00:00
|
|
|
client, err := c.GetUserClient(false)
|
2022-11-24 12:16:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return helix.User{}, fmt.Errorf("failed getting API client for user: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:22:49 +00:00
|
|
|
users, err := client.GetUsers(&helix.UsersParams{})
|
|
|
|
if err != nil {
|
|
|
|
return helix.User{}, fmt.Errorf("failed looking up user: %w", err)
|
|
|
|
}
|
|
|
|
if len(users.Data.Users) < 1 {
|
|
|
|
return helix.User{}, fmt.Errorf("no users found")
|
|
|
|
}
|
2023-02-02 20:24:14 +00:00
|
|
|
c.User = users.Data.Users[0]
|
2022-11-30 18:15:47 +00:00
|
|
|
|
2023-02-02 20:24:14 +00:00
|
|
|
return c.User, nil
|
2022-11-23 21:22:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 15:16:59 +00:00
|
|
|
func (c *Client) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2022-11-23 21:22:49 +00:00
|
|
|
// Get code from params
|
|
|
|
code := req.URL.Query().Get("code")
|
|
|
|
if code == "" {
|
|
|
|
// TODO Nice error page
|
|
|
|
http.Error(w, "missing code", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-03 15:16:32 +00:00
|
|
|
|
2022-11-23 21:22:49 +00:00
|
|
|
// Exchange code for access/refresh tokens
|
2022-12-03 15:16:32 +00:00
|
|
|
userTokenResponse, err := c.API.RequestUserAccessToken(code)
|
2022-11-23 21:22:49 +00:00
|
|
|
if err != nil {
|
2022-12-03 15:16:32 +00:00
|
|
|
http.Error(w, "failed auth token request: "+err.Error(), http.StatusInternalServerError)
|
2022-11-23 21:22:49 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-03 15:16:32 +00:00
|
|
|
|
|
|
|
err = c.db.PutJSON(AuthKey, AuthResponse{
|
|
|
|
AccessToken: userTokenResponse.Data.AccessToken,
|
|
|
|
RefreshToken: userTokenResponse.Data.RefreshToken,
|
|
|
|
ExpiresIn: userTokenResponse.Data.ExpiresIn,
|
|
|
|
Scope: userTokenResponse.Data.Scopes,
|
|
|
|
Time: time.Now(),
|
|
|
|
})
|
2022-11-23 21:22:49 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "error saving auth data for user: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
2024-02-25 20:20:08 +00:00
|
|
|
_, _ = fmt.Fprintf(w, `<html><body><h2>All done, you can close me now!</h2><script>window.close();</script></body></html>`)
|
2022-11-23 21:22:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 12:16:08 +00:00
|
|
|
type RefreshResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
Scope []string `json:"scope"`
|
|
|
|
}
|
|
|
|
|
2022-12-03 15:16:59 +00:00
|
|
|
func getRedirectURI(baseurl string) string {
|
|
|
|
return fmt.Sprintf("http://%s/twitch/callback", baseurl)
|
2022-11-23 21:22:49 +00:00
|
|
|
}
|