hackathon/db/oauth.go

100 lines
1.9 KiB
Go

package db
import (
2021-09-30 10:32:18 +07:00
"context"
"fmt"
"os"
"sync"
"time"
2021-09-30 10:32:18 +07:00
"github.com/hhhapz/codequest/models"
2021-09-30 10:32:18 +07:00
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
2021-09-30 10:32:18 +07:00
const (
base = "https://www.googleapis.com"
scopeEmail = base + "/auth/userinfo.email"
scopeProfile = base + "/auth/userinfo.profile"
)
type OAuthState struct {
2022-04-28 23:31:09 +07:00
config *oauth2.Config
redirectURI string
states map[string]oauthEntry
m sync.Mutex
}
2021-09-30 10:32:18 +07:00
type oauthEntry struct {
created time.Time
callback string
}
2022-04-28 23:31:09 +07:00
func NewOAuthState(path string, redirectURI string) (*OAuthState, error) {
2021-09-30 10:32:18 +07:00
key, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not open file: %w", err)
}
config, err := google.ConfigFromJSON(key, scopeEmail, scopeProfile)
if err != nil {
return nil, fmt.Errorf("could not load config: %w", err)
}
return &OAuthState{
2022-04-28 23:31:09 +07:00
config: config,
redirectURI: redirectURI,
states: make(map[string]oauthEntry),
m: sync.Mutex{},
2021-09-30 10:32:18 +07:00
}, nil
}
func (o *OAuthState) Create(callback string) string {
o.m.Lock()
defer o.m.Unlock()
var state string
ok := true
for ok {
state = createToken(32)
_, ok = o.states[state]
}
o.states[state] = oauthEntry{
2022-04-28 23:31:09 +07:00
created: time.Now(),
}
return o.config.AuthCodeURL(state, oauth2.AccessTypeOffline)
}
func (o *OAuthState) Validate(ctx context.Context, state string, code string) (*oauth2.Token, string, error) {
o.m.Lock()
defer o.m.Unlock()
2022-04-28 23:31:09 +07:00
_, ok := o.states[state]
2021-09-30 10:32:18 +07:00
if !ok {
return nil, "", models.NewUserError("invalid state: %q", state)
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
delete(o.states, state)
2021-09-30 10:32:18 +07:00
tk, err := o.config.Exchange(ctx, code, oauth2.AccessTypeOffline)
2022-04-28 23:31:09 +07:00
return tk, o.redirectURI, err
}
func (o *OAuthState) GarbageCycle(period time.Duration, duration time.Duration) {
tick := time.NewTicker(period)
for range tick.C {
o.m.Lock()
now := time.Now()
for state, entry := range o.states {
if now.After(entry.created.Add(duration)) {
delete(o.states, state)
}
}
o.m.Unlock()
}
}