continued auth system
This commit is contained in:
50
ttv/auth.go
50
ttv/auth.go
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/adeithe/go-twitch/api"
|
||||
"zomo.dev/largehadroncollider/db"
|
||||
"zomo.dev/largehadroncollider/db/db_cold"
|
||||
"zomo.dev/largehadroncollider/util"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
func initAuth(conf *util.Config, dbConn *db.DBConn) (*TwitchAuth, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
tokens, err := getTokensFromDB(dbConn)
|
||||
tokens, err := dbConn.Cold.GetAllUserAuth()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -26,27 +27,24 @@ func initAuth(conf *util.Config, dbConn *db.DBConn) (*TwitchAuth, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type TwitchAuth struct {
|
||||
Ctx context.Context
|
||||
Client *api.Client
|
||||
Accounts []TwitchAuthAccount
|
||||
Accounts []db_cold.UserAuth
|
||||
}
|
||||
|
||||
type TwitchAuthAccount struct {
|
||||
api.User
|
||||
Token string
|
||||
}
|
||||
|
||||
func getTokensFromDB(dbConn *db.DBConn) ([]string, error) {
|
||||
// TODO db cold
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
func testTokens(ctx context.Context, client *api.Client, tokens []string) ([]TwitchAuthAccount, error) {
|
||||
accounts := make([]TwitchAuthAccount, 0)
|
||||
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 {
|
||||
@@ -57,22 +55,30 @@ func testTokens(ctx context.Context, client *api.Client, tokens []string) ([]Twi
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func testToken(ctx context.Context, client *api.Client, token string) (TwitchAuthAccount, error) {
|
||||
users, err := client.Users.List().Do(ctx, api.WithBearerToken(token))
|
||||
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))
|
||||
if err != nil {
|
||||
return TwitchAuthAccount{}, err
|
||||
return db_cold.UserAuth{}, err
|
||||
}
|
||||
|
||||
usersData := users.Data
|
||||
|
||||
if len(usersData) <= 0 {
|
||||
return TwitchAuthAccount{}, errors.New("user data returned an empty array")
|
||||
return db_cold.UserAuth{}, 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 TwitchAuthAccount{
|
||||
mainUser,
|
||||
token,
|
||||
}, nil
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user