goext/googleapi/oAuth.go

92 lines
1.9 KiB
Go
Raw Normal View History

2023-12-01 18:33:04 +01:00
package googleapi
import (
"encoding/json"
2023-12-04 13:48:11 +01:00
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
2023-12-01 18:33:04 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"io"
"net/http"
"sync"
"time"
)
type GoogleOAuth interface {
2023-12-04 13:48:11 +01:00
AccessToken() (string, error)
2023-12-01 18:33:04 +01:00
}
type oauth struct {
clientID string
clientSecret string
refreshToken string
lock sync.RWMutex
accessToken *string
expiryDate *time.Time
}
func NewGoogleOAuth(clientid string, clientsecret, refreshtoken string) GoogleOAuth {
return &oauth{
clientID: clientid,
clientSecret: clientsecret,
refreshToken: refreshtoken,
}
}
func (c *oauth) AccessToken() (string, error) {
c.lock.RLock()
if c.accessToken != nil && c.expiryDate != nil && (*c.expiryDate).After(time.Now()) {
c.lock.RUnlock()
return *c.accessToken, nil // still valid
}
c.lock.RUnlock()
httpclient := http.Client{}
2023-12-04 13:48:11 +01:00
url := fmt.Sprintf("https://oauth2.googleapis.com/token?client_id=%s&client_secret=%s&grant_type=%s&refresh_token=%s",
c.clientID,
c.clientSecret,
"refresh_token",
c.refreshToken)
req, err := http.NewRequest(http.MethodPost, url, nil)
2023-12-01 18:33:04 +01:00
if err != nil {
return "", err
}
reqStartTime := time.Now()
res, err := httpclient.Do(req)
type response struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
}
var r response
data, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(data, &r)
if err != nil {
return "", err
}
2023-12-04 13:48:11 +01:00
if r.ExpiresIn == 0 || r.AccessToken == "" {
return "", exerr.New(exerr.TypeGoogleResponse, "google oauth returned no response").Str("body", string(data)).Build()
}
2023-12-01 18:33:04 +01:00
c.lock.Lock()
c.expiryDate = langext.Ptr(reqStartTime.Add(timeext.FromSeconds(r.ExpiresIn - 10)))
c.accessToken = langext.Ptr(r.AccessToken)
c.lock.Unlock()
return r.AccessToken, nil
}