user authentication structure in place
This commit is contained in:
115
ttv/auth.go
115
ttv/auth.go
@@ -2,83 +2,96 @@ package ttv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/adeithe/go-twitch/api"
|
||||
"zomo.dev/largehadroncollider/db"
|
||||
"zomo.dev/largehadroncollider/db/db_cold"
|
||||
"zomo.dev/largehadroncollider/util"
|
||||
)
|
||||
|
||||
// sign in to twitch with each saved tokens
|
||||
const TWITCH_AUTH_URL = "https://id.twitch.tv/oauth2/token"
|
||||
|
||||
func initAuth(conf *util.Config, dbConn *db.DBConn) (*TwitchAuth, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
tokens, err := dbConn.Cold.GetAllUserAuth()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := api.New(conf.ClientID)
|
||||
twitchAuth := &TwitchAuth{ctx, client}
|
||||
|
||||
accounts, err := testTokens(ctx, client, tokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// run once synchronously then start looping in a thread
|
||||
twitchAuth.updateDetailsRefreshTokens(conf, dbConn)
|
||||
go twitchAuth.loopUpdateDetailsRefreshTokens(conf, dbConn)
|
||||
|
||||
for _, account := range accounts {
|
||||
err := dbConn.Cold.UpdateUserAuth(account.UserID, account.UserName, account.UserLogin, account.AccessToken, account.RefreshToken, account.TokenExpires)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &TwitchAuth{ ctx, client, accounts }, nil
|
||||
return twitchAuth, nil
|
||||
}
|
||||
|
||||
type TwitchAuth struct {
|
||||
Ctx context.Context
|
||||
Ctx context.Context
|
||||
Client *api.Client
|
||||
Accounts []db_cold.UserAuth
|
||||
}
|
||||
|
||||
func testTokens(ctx context.Context, client *api.Client, tokens []db_cold.UserAuth) ([]db_cold.UserAuth, error) {
|
||||
accounts := make([]db_cold.UserAuth, 0)
|
||||
for _, token := range tokens {
|
||||
account, err := testToken(ctx, client, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accounts = append(accounts, account)
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func testToken(ctx context.Context, client *api.Client, token db_cold.UserAuth) (db_cold.UserAuth, error) {
|
||||
// TODO check refresh time, refresh token if needed
|
||||
|
||||
users, err := client.Users.List().Do(ctx, api.WithBearerToken(token.AccessToken))
|
||||
func (twitch *TwitchAuth) doAuth(formData url.Values) (TwitchAuthTokenResp, error) {
|
||||
resp, err := http.PostForm(TWITCH_AUTH_URL, formData)
|
||||
if err != nil {
|
||||
return db_cold.UserAuth{}, err
|
||||
return TwitchAuthTokenResp{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return TwitchAuthTokenResp{}, err
|
||||
}
|
||||
|
||||
var authResp TwitchAuthTokenResp
|
||||
err = json.Unmarshal(bodyBytes, &authResp)
|
||||
if err != nil {
|
||||
return TwitchAuthTokenResp{}, err
|
||||
}
|
||||
|
||||
return authResp, nil
|
||||
}
|
||||
|
||||
func (twitch *TwitchAuth) DoAuth(conf *util.Config, code string) (TwitchAuthTokenResp, error) {
|
||||
formData := url.Values{
|
||||
"client_id": {conf.ClientID},
|
||||
"client_secret": {conf.ClientSecret},
|
||||
"redirect_uri": {conf.RedirectURI},
|
||||
"code": {code},
|
||||
"grant_type": {"authorization_code"},
|
||||
}
|
||||
|
||||
return twitch.doAuth(formData)
|
||||
}
|
||||
|
||||
func (twitch *TwitchAuth) DoRefresh(conf *util.Config, refreshToken string) (TwitchAuthTokenResp, error) {
|
||||
formData := url.Values{
|
||||
"client_id": {conf.ClientID},
|
||||
"client_secret": {conf.ClientSecret},
|
||||
"refresh_token": {refreshToken},
|
||||
"grant_type": {"refresh_token"},
|
||||
}
|
||||
|
||||
return twitch.doAuth(formData)
|
||||
}
|
||||
|
||||
func (twitch *TwitchAuth) GetTokenUser(accessToken string) (api.User, error) {
|
||||
return getTokenUser(twitch.Ctx, twitch.Client, accessToken)
|
||||
}
|
||||
|
||||
func getTokenUser(ctx context.Context, client *api.Client, accessToken string) (api.User, error) {
|
||||
users, err := client.Users.List().Do(ctx, api.WithBearerToken(accessToken))
|
||||
if err != nil {
|
||||
return api.User{}, err
|
||||
}
|
||||
|
||||
usersData := users.Data
|
||||
|
||||
if len(usersData) <= 0 {
|
||||
return db_cold.UserAuth{}, errors.New("user data returned an empty array")
|
||||
return api.User{}, errors.New("user data returned an empty array")
|
||||
}
|
||||
|
||||
// from twitch
|
||||
mainUser := usersData[0]
|
||||
token.UserLogin = mainUser.UserLogin
|
||||
token.UserName = mainUser.UserName
|
||||
token.UserEmail = mainUser.Email
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func refreshToken(token db_cold.UserAuth) (db_cold.UserAuth, error) {
|
||||
// TODO get new access token using refresh token
|
||||
// TODO this should be called regularly, as needed based on Expires
|
||||
return usersData[0], nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user