-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathoauth.go
More file actions
104 lines (89 loc) · 2.55 KB
/
Copy pathoauth.go
File metadata and controls
104 lines (89 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package auth
import (
"context"
"encoding/json"
"fmt"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
type googleUser struct {
ID string `json:"id"`
Email string `json:"email"`
VerifiedEmail bool `json:"verified_email"`
Picture string `json:"picture"`
}
/*
OAuthInit initializes the Google OAuth configuration.
It uses sync.Once to safely set the configuration once for this Auth instance.
*/
func (a *Auth) OAuthInit(clientID, clientSecret, redirectURL string) error {
if clientID == "" || clientSecret == "" || redirectURL == "" {
return ErrEmptyInput
}
a.oauthOnce.Do(func() {
a.oauthConfig = &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirectURL,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
})
return nil
}
/*
GetGoogleLoginURL generates the URL to the Google consent screen.
*/
func (a *Auth) GetGoogleLoginURL(state string) (string, error) {
if state == "" {
return "", ErrEmptyInput
}
if a.oauthConfig == nil {
return "", ErrOAuthNotInitialized
}
return a.oauthConfig.AuthCodeURL(state), nil
}
/*
HandleGoogleCallback exchanges the auth code for a token, fetches user info,
upserts the user into the database, and returns a generated JWT string.
*/
func (a *Auth) HandleGoogleCallback(ctx context.Context, code string) (string, error) {
if code == "" {
return "", ErrEmptyInput
}
if a.oauthConfig == nil {
return "", ErrOAuthNotInitialized
}
token, err := a.oauthConfig.Exchange(ctx, code)
if err != nil {
return "", fmt.Errorf("%w: %v", ErrOAuthExchangeFailed, err)
}
client := a.oauthConfig.Client(ctx, token)
resp, err := client.Get("https://www.googleapis.com/oauth2/v2/userinfo")
if err != nil {
return "", fmt.Errorf("%w: %v", ErrOAuthProfileFetchFailed, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("%w: status code %d", ErrOAuthProfileFetchFailed, resp.StatusCode)
}
var user googleUser
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
return "", fmt.Errorf("%w: %v", ErrOAuthProfileFetchFailed, err)
}
if !user.VerifiedEmail {
return "", fmt.Errorf("oauth email is not verified by provider")
}
if err := a.upsertOAuthUser(ctx, user.Email); err != nil {
return "", err
}
jwtStr, err := a.GenerateToken(user.Email)
if err != nil {
return "", fmt.Errorf("failed to generate jwt after oauth login: %w", err)
}
return jwtStr, nil
}