-
Notifications
You must be signed in to change notification settings - Fork 6
/
oauth.go
261 lines (239 loc) · 7.52 KB
/
oauth.go
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"golang.org/x/oauth2"
"golang.org/x/oauth2/jws"
"v.io/v23/context"
)
const (
cookieName = "VanadiumAllocatorCookie"
// Payload for cookie during the oauth flow. Should never match a valid
// email address.
csrfCookieValue = "csrf"
csrfTokenLen = 16
// This parameter name is hardcorded in static/dash.js,
// and should be changed in tandem.
paramCSRF = "csrf"
)
var errOauthInProgress = errors.New("oauth login in process")
type claimSet struct {
jws.ClaimSet
Email string `json:"email"`
}
// decodeToken is modeled after golang.org/x/oauth2/jws.Decode. The only
// difference lies in using claimSet instead of jws.ClaimSet (as of May 2016,
// the latter does not contain the Email field which we need).
func decodeToken(payload string) (*claimSet, error) {
s := strings.Split(payload, ".")
if len(s) < 2 {
return nil, errors.New("jws: invalid token received")
}
decoded, err := base64Decode(s[1])
if err != nil {
return nil, err
}
c := &claimSet{}
err = json.NewDecoder(bytes.NewBuffer(decoded)).Decode(c)
return c, err
}
// base64Decode is copied from golang.org/x/oauth2/jws.base64Decode.
func base64Decode(s string) ([]byte, error) {
// Add back missing padding.
switch len(s) % 4 {
case 1:
s += "==="
case 2:
s += "=="
case 3:
s += "="
}
return base64.URLEncoding.DecodeString(s)
}
type oauthCredentials struct {
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
// HashKey is not strictly part of the OAuth credentials, but for
// convenience we put it in the same object.
//
// TODO(caprita): Consider signing cookies with the server's private key
// (and verify signatures using blessings) instead of maintaining a
// separate signing key.
HashKey string `json:"hashKey"`
}
func (c *oauthCredentials) validate() error {
switch {
case c.ClientID == "":
return errors.New("clientId empty")
case c.ClientSecret == "":
return errors.New("clientSecret empty")
case c.HashKey == "":
return errors.New("hashKey empty")
default:
return nil
}
}
func clientCredsFromFile(f string) (*oauthCredentials, error) {
jsonData, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}
creds := new(oauthCredentials)
if err := json.Unmarshal(jsonData, creds); err != nil {
return nil, err
}
return creds, nil
}
func oauthConfig(externalURL string, oauthCreds *oauthCredentials) *oauth2.Config {
return &oauth2.Config{
ClientID: oauthCreds.ClientID,
ClientSecret: oauthCreds.ClientSecret,
RedirectURL: strings.TrimRight(externalURL, "/") + routeOauth,
Scopes: []string{"email"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
},
}
}
type oauthState struct {
CSRFToken, RedirectURL string
}
func (s oauthState) encode() (string, error) {
b, err := json.Marshal(s)
if err != nil {
return "", fmt.Errorf("failed to encode %v: %v", s, err)
}
return string(b), nil
}
func (s *oauthState) decode(enc string) error {
if err := json.Unmarshal([]byte(enc), s); err != nil {
return fmt.Errorf("failed to decode %v: %v", enc, err)
}
return nil
}
func generateCSRFToken(ctx *context.T) string {
b := make([]byte, csrfTokenLen)
if _, err := rand.Read(b); err != nil {
ctx.Errorf("Failed to generate csrf cookie: %v", err)
return ""
}
return base64.URLEncoding.EncodeToString(b)
}
func validateCSRF(ctx *context.T, req *http.Request, baker cookieBaker, csrfToken string) bool {
cookieToken, cookieCSRFToken, err := baker.get(req, cookieName)
if cookieToken == "" && err == nil {
err = errors.New("missing cookie")
}
if err != nil {
ctx.Errorf("Failed to read csrf cookie: %v", err)
return false
}
return cookieToken == csrfCookieValue && cookieCSRFToken == csrfToken
}
func checkSession(baker cookieBaker, r *http.Request, mutating bool) (string, string, error) {
email, csrfToken, err := baker.get(r, cookieName)
switch {
case err == nil && email != "" && email != csrfCookieValue:
// The user is already logged in.
if mutating && r.FormValue(paramCSRF) != csrfToken {
return "", "", errors.New("bad CSRF token for mutating request")
} else {
return email, csrfToken, nil
}
case err == nil && email == csrfCookieValue:
// The user is in the middle of the oauth flow.
//
// TODO(caprita): Rather then presenting an error page to the
// user, we can gracefully ask the client to retry a bit later
// (via a redirect or by sending a 503 with retry-after). This
// will invariably trigger a new oauth sequence (since the
// request's CSRF token will no longer match on the retry).
return "", "", errOauthInProgress
case err == nil:
return "", "", errors.New("missing cookie")
default:
return "", "", fmt.Errorf("bad cookie: %v", err)
}
}
func requireSession(ctx *context.T, oauthCfg *oauth2.Config, baker cookieBaker, w http.ResponseWriter, r *http.Request, mutating bool) (string, string, error) {
if email, csrfToken, err := checkSession(baker, r, mutating); err == nil || err == errOauthInProgress {
return email, csrfToken, err
} else {
ctx.Infof("Re-authenticating: %v", err)
}
// Proceed with the oauth flow.
csrfToken := generateCSRFToken(ctx)
if csrfToken == "" {
return "", "", errors.New("failed to generate CSRF token")
}
redirectTo := makeURL(ctx, routeHome, params{paramMessage: "Re-authentication was required."})
if !mutating {
redirectTo = r.URL.String()
}
s, err := oauthState{CSRFToken: csrfToken, RedirectURL: redirectTo}.encode()
if err != nil {
return "", "", fmt.Errorf("failed to encode state: %v", err)
}
if err := baker.set(w, cookieName, csrfCookieValue, csrfToken); err != nil {
return "", "", fmt.Errorf("failed to set CSRF cookie: %v", err)
}
authURL := oauthCfg.AuthCodeURL(s)
http.Redirect(w, r, authURL, http.StatusFound)
return "", "", nil
}
func handleOauth(ctx *context.T, args httpArgs, baker cookieBaker, w http.ResponseWriter, r *http.Request) {
const (
paramState = "state"
paramCode = "code"
)
var state oauthState
if err := state.decode(r.FormValue(paramState)); err != nil {
args.assets.badRequest(ctx, w, r, fmt.Errorf("invalid state: %v", err))
return
}
if token := state.CSRFToken; !validateCSRF(ctx, r, baker, token) {
args.assets.badRequest(ctx, w, r, fmt.Errorf("invalid csrf token: %v", token))
return
}
code := r.FormValue(paramCode)
oauthCfg := oauthConfig(args.externalURL, args.oauthCreds)
t, err := oauthCfg.Exchange(oauth2.NoContext, code)
if err != nil {
args.assets.badRequest(ctx, w, r, fmt.Errorf("exchange failed: %v", err))
return
}
idToken, ok := t.Extra("id_token").(string)
if !ok {
args.assets.badRequest(ctx, w, r, errors.New("invalid id token"))
return
}
claimSet, err := decodeToken(idToken)
if err != nil {
ctx.Errorf("oauth2: error decoding JWT token: %v", err)
args.assets.errorOccurred(ctx, w, r, routeHome, err)
return
}
email := claimSet.Email
csrfToken := generateCSRFToken(ctx)
if err := baker.set(w, cookieName, email, csrfToken); err != nil {
ctx.Errorf("Failed to set email cookie: %v", err)
args.assets.errorOccurred(ctx, w, r, routeHome, err)
return
}
if state.RedirectURL == "" {
args.assets.badRequest(ctx, w, r, errors.New("no redirect url provided"))
return
}
http.Redirect(w, r, state.RedirectURL, http.StatusFound)
}