go-twitch/auth/token.go

46 lines
939 B
Go

package auth
import (
"time"
"golang.org/x/oauth2"
)
type Token struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
Expiry time.Time `json:"-"`
// Only present when using OIDC.
IdToken *string `json:"id_token"`
RefreshToken string `json:"refresh_token"`
Scope []string `json:"scope"`
TokenType string `json:"token_type"`
}
func (t *Token) Valid() bool {
return t.AccessToken != "" && t.Expiry.After(time.Now())
}
func (t *Token) Underlying() *oauth2.Token {
return &oauth2.Token{
AccessToken: t.AccessToken,
TokenType: t.TokenType,
RefreshToken: t.RefreshToken,
Expiry: t.Expiry,
}
}
type TokenHandler interface {
Handle(state string, token string)
}
type TokenHandlerFunc func(state string, token string)
var _ TokenHandler = (*TokenHandlerFunc)(nil)
func (f TokenHandlerFunc) Handle(state string, token string) {
f(state, token)
}