-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.go
More file actions
225 lines (209 loc) · 6.85 KB
/
Copy pathoauth.go
File metadata and controls
225 lines (209 loc) · 6.85 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Package oauth adds OAuth2 social login (Google, GitHub, Facebook) to togo auth.
// It registers /api/auth/oauth/{provider} + callback routes; on success it
// find-or-creates the user via auth and issues a togo session. Depends on the
// auth plugin. Install: `togo install togo-framework/auth-oauth`.
package oauth
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/togo-framework/auth"
"github.com/togo-framework/togo"
)
type provider struct {
authURL, exchangeURL, userinfoURL, scope, emailField string
}
var providers = map[string]provider{
"google": {
authURL: "https://accounts.google.com/o/oauth2/v2/auth",
exchangeURL: "https://oauth2.googleapis.com/token",
userinfoURL: "https://www.googleapis.com/oauth2/v2/userinfo",
scope: "openid email profile", emailField: "email",
},
"github": {
authURL: "https://github.com/login/oauth/authorize",
exchangeURL: "https://github.com/login/oauth/access_token",
userinfoURL: "https://api.github.com/user", scope: "read:user user:email", emailField: "email",
},
"facebook": {
authURL: "https://www.facebook.com/v18.0/dialog/oauth",
exchangeURL: "https://graph.facebook.com/v18.0/oauth/access_token",
userinfoURL: "https://graph.facebook.com/me?fields=email", scope: "email", emailField: "email",
},
}
var httpClient = &http.Client{Timeout: 15 * time.Second}
func init() {
togo.RegisterProviderFunc("auth-oauth", togo.PriorityLate+20, func(k *togo.Kernel) error {
svc, ok := auth.FromKernel(k)
if !ok {
if k.Log != nil {
k.Log.Warn("auth-oauth: auth plugin not installed; skipping")
}
return nil
}
k.Router.Get("/api/auth/oauth/{provider}", redirectHandler())
k.Router.Get("/api/auth/oauth/{provider}/callback", callbackHandler(svc))
// Advertise only the providers that are actually configured.
for name := range providers {
if id, _ := clientCreds(name); id != "" {
auth.RegisterLoginMethod(auth.LoginMethod{
Name: name,
Label: "Continue with " + strings.ToUpper(name[:1]) + name[1:],
Type: "oauth",
URL: "/api/auth/oauth/" + name,
})
}
}
return nil
})
}
func clientCreds(name string) (id, secret string) {
p := "OAUTH_" + strings.ToUpper(name)
return os.Getenv(p + "_CLIENT_ID"), os.Getenv(p + "_CLIENT_SECRET")
}
func redirectURI(name string) string {
return strings.TrimRight(os.Getenv("APP_URL"), "/") + "/api/auth/oauth/" + name + "/callback"
}
func redirectHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "provider")
p, ok := providers[name]
if !ok {
http.Error(w, "unknown provider", http.StatusNotFound)
return
}
id, _ := clientCreds(name)
if id == "" {
http.Error(w, "provider not configured", http.StatusServiceUnavailable)
return
}
state := randHex()
http.SetCookie(w, &http.Cookie{Name: "oauth_state", Value: state, Path: "/", HttpOnly: true, MaxAge: 600, SameSite: http.SameSiteLaxMode}) //#nosec G124 -- short-lived CSRF state cookie (HttpOnly+SameSite); Secure handled by TLS/proxy in prod
q := url.Values{
"client_id": {id},
"redirect_uri": {redirectURI(name)},
"response_type": {"code"},
"scope": {p.scope},
"state": {state},
}
http.Redirect(w, r, p.authURL+"?"+q.Encode(), http.StatusFound)
}
}
func callbackHandler(svc *auth.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "provider")
p, ok := providers[name]
if !ok {
http.Error(w, "unknown provider", http.StatusNotFound)
return
}
// CSRF: state cookie must match the query.
c, err := r.Cookie("oauth_state")
if err != nil || c.Value == "" || c.Value != r.URL.Query().Get("state") {
http.Error(w, "invalid oauth state", http.StatusBadRequest)
return
}
code := r.URL.Query().Get("code")
token, err := exchange(r.Context(), name, p, code)
if err != nil {
http.Error(w, "token exchange failed", http.StatusBadGateway)
return
}
email, err := fetchEmail(r.Context(), name, p, token)
if err != nil || email == "" {
http.Error(w, "could not resolve email from provider", http.StatusBadGateway)
return
}
id, err := svc.FindOrCreateByEmail(r.Context(), email)
if err != nil {
http.Error(w, "login failed", http.StatusInternalServerError)
return
}
if _, err := svc.IssueSession(w, *id); err != nil {
http.Error(w, "session failed", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/dashboard", http.StatusFound)
}
}
func exchange(ctx context.Context, name string, p provider, code string) (string, error) {
id, secret := clientCreds(name)
form := url.Values{
"client_id": {id}, "client_secret": {secret}, "code": {code},
"redirect_uri": {redirectURI(name)}, "grant_type": {"authorization_code"},
}
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, p.exchangeURL, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var out struct {
AccessToken string `json:"access_token"`
}
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil || out.AccessToken == "" {
return "", fmt.Errorf("no access_token")
}
return out.AccessToken, nil
}
func fetchEmail(ctx context.Context, name string, p provider, token string) (string, error) {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, p.userinfoURL, nil)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var m map[string]any
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
return "", err
}
if e, ok := m[p.emailField].(string); ok && e != "" {
return e, nil
}
// GitHub: email may be private — fall back to /user/emails.
if name == "github" {
return githubPrimaryEmail(ctx, token)
}
return "", fmt.Errorf("email not present")
}
func githubPrimaryEmail(ctx context.Context, token string) (string, error) {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/user/emails", nil)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var emails []struct {
Email string `json:"email"`
Primary bool `json:"primary"`
Verified bool `json:"verified"`
}
_ = json.Unmarshal(body, &emails)
for _, e := range emails {
if e.Primary && e.Verified {
return e.Email, nil
}
}
return "", fmt.Errorf("no verified primary email")
}
func randHex() string {
b := make([]byte, 16)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}