Taobin-Recipe-Manager/server/services/oauth/oauth.go

105 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-09-21 14:21:14 +07:00
package oauth
import (
"context"
"encoding/json"
"errors"
2023-09-28 14:12:09 +07:00
"log"
2023-09-21 14:21:14 +07:00
"recipe-manager/config"
2023-09-28 14:12:09 +07:00
"recipe-manager/helpers"
2023-09-21 14:21:14 +07:00
"recipe-manager/models"
"golang.org/x/oauth2"
)
type OAuthService interface {
AuthURL(state string, stateMap map[string]string) string
GetState(state string) (map[string]string, bool)
RemoveState(state string)
Exchange(ctx context.Context, code string) (*oauth2.Token, error)
GetUserInfo(ctx context.Context, token *oauth2.Token) (*models.User, error)
RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error)
}
type oauthService struct {
cfg *config.ServerConfig
gConfig *oauth2.Config
nonce map[string]map[string]string
}
func NewOAuthService(cfg *config.ServerConfig) OAuthService {
2023-09-28 14:12:09 +07:00
clientSecret, err := helpers.GetClientSecret("client_secret.json")
2023-09-21 14:21:14 +07:00
if err != nil {
2023-09-28 14:12:09 +07:00
log.Fatalf("Unable to get client secret: %v", err)
2023-09-21 14:21:14 +07:00
}
2023-09-28 14:12:09 +07:00
clientSecret.RedirectURL = cfg.ServerDomain + "/auth/google/callback"
2023-09-21 14:21:14 +07:00
return &oauthService{
2023-09-28 14:12:09 +07:00
cfg: cfg,
gConfig: clientSecret,
nonce: make(map[string]map[string]string),
2023-09-21 14:21:14 +07:00
}
}
func (o *oauthService) AuthURL(state string, stateMap map[string]string) string {
if stateMap != nil {
o.nonce[state] = stateMap
} else {
o.nonce[state] = make(map[string]string)
}
2024-01-25 16:54:57 +07:00
authCodeOptions := []oauth2.AuthCodeOption{
// oauth2.SetAuthURLParam("hd", "forth.co.th"),
oauth2.SetAuthURLParam("include_granted_scopes", "true"),
oauth2.AccessTypeOffline,
}
return o.gConfig.AuthCodeURL(state, authCodeOptions...)
2023-09-21 14:21:14 +07:00
}
func (o *oauthService) GetState(state string) (map[string]string, bool) {
val, ok := o.nonce[state]
return val, ok
}
func (o *oauthService) RemoveState(state string) {
delete(o.nonce, state)
}
func (o *oauthService) Exchange(ctx context.Context, code string) (*oauth2.Token, error) {
return o.gConfig.Exchange(ctx, code)
}
func (o *oauthService) GetUserInfo(ctx context.Context, token *oauth2.Token) (*models.User, error) {
client := o.gConfig.Client(ctx, token)
resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil {
return nil, err
}
defer resp.Body.Close()
var userInfo map[string]interface{}
2023-12-06 20:21:25 +07:00
if err := json.NewDecoder(resp.Body).Decode(&userInfo); err != nil {
return nil, err
}
2023-09-21 14:21:14 +07:00
if userInfo["error"] != nil {
2024-03-15 14:10:24 +07:00
return nil, errors.New("error getting user info")
2023-09-21 14:21:14 +07:00
}
return &models.User{
Name: userInfo["name"].(string),
Email: userInfo["email"].(string),
Picture: userInfo["picture"].(string),
}, nil
}
func (o *oauthService) RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error) {
return o.gConfig.TokenSource(ctx, token).Token()
}