-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmultipass.go
440 lines (405 loc) · 12.5 KB
/
multipass.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright 2016 Lars Wiegman. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.
package multipass
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"errors"
"html/template"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/gorilla/csrf"
jose "gopkg.in/square/go-jose.v1"
)
// Portable errors
var (
ErrInvalidToken = errors.New("invalid token")
ErrForbidden = errors.New(http.StatusText(http.StatusForbidden))
ErrUnauthorized = errors.New(http.StatusText(http.StatusUnauthorized))
)
// options contains the optional settings for the Multipass instance.
type options struct {
Expires time.Duration
Basepath string
Service UserService
Template template.Template
CSRF bool
}
// Multipass implements the http.Handler interface which can handle
// authentication and authorization of users and resources using signed JWT.
type Multipass struct {
siteaddr string
handler http.Handler
opts options
}
// New returns a new instance of Multipass with the given site address.
//
// The site address must point to the absolute base URL of the site.
//
// Multipass is initialized with the following defaults:
// 2048 bit key size
// /multipass Basepath
// 24h token lifespan
func New(siteaddr string, opts ...Option) *Multipass {
m := parseOptions(opts...)
m.siteaddr = siteaddr
// Generate and set a private key if none is set
if k, err := PrivateKeyFromEnvironment(); k != nil && err == nil {
log.Printf("Use private key from environment variable %s\n", PKENV)
} else {
key, err := rsa.GenerateKey(rand.Reader, DefaultKeySize)
if err != nil {
panic(err)
}
buf := new(bytes.Buffer)
if err := pemEncodePrivateKey(buf, key); err != nil {
panic(err)
}
os.Setenv(PKENV, buf.String())
}
switch m.opts.CSRF {
case false:
m.handler = http.HandlerFunc(m.routeHandler)
default:
m.handler = csrfProtect(http.HandlerFunc(m.routeHandler), m)
}
return m
}
// Basepath return the base path.
func (m *Multipass) Basepath() string {
return m.opts.Basepath
}
// ServeHTTP satisfies the ServeHTTP interface
func (m *Multipass) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.handler.ServeHTTP(w, r)
}
// routeHandler routes request to the appropriate handler.
func (m *Multipass) routeHandler(w http.ResponseWriter, r *http.Request) {
if s := strings.TrimSuffix(r.URL.Path, "/"); len(r.URL.Path) > len(s) {
http.Redirect(w, r, s, http.StatusMovedPermanently)
}
var fn func(w http.ResponseWriter, r *http.Request)
if p := strings.TrimPrefix(r.URL.Path, m.opts.Basepath); len(p) < len(r.URL.Path) {
switch p {
case "":
fn = m.rootHandler
case "/login":
fn = m.loginHandler
case "/signout":
fn = m.signoutHandler
case "/confirm":
fn = m.confirmHandler
case "/pub.cer":
fn = m.publicKeyHandler
}
}
if fn == nil {
fn = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
p := &page{
Page: notfoundPage,
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
m.opts.Template.ExecuteTemplate(w, "page", p)
}
}
fn(w, r)
}
// csrfProtect wraps the given http.Handler to protect against CSRF attacks.
// The CSRF key is persisted in the environment to not break CSRF
// validation between application restarts.
func csrfProtect(h http.Handler, m *Multipass) http.Handler {
key, err := base64.StdEncoding.DecodeString(os.Getenv("MULTIPASS_CSRF_KEY"))
if err != nil || len(key) != 32 {
key = make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
panic(err)
}
// Persist key on reloads
os.Setenv("MULTIPASS_CSRF_KEY", base64.StdEncoding.EncodeToString(key))
}
errorHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(403)
p := &page{
Page: errorPage,
ErrorMessage: "Sorry, your CSRF token is invalid",
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
m.opts.Template.ExecuteTemplate(w, "page", p)
})
opts := []csrf.Option{
csrf.Secure(os.Getenv("MULTIPASS_DEV") == ""),
csrf.Path(m.opts.Basepath),
csrf.FieldName("csrf.token"),
csrf.ErrorHandler(errorHandler),
}
return csrf.Protect(key, opts...)(h)
}
// rootHandler handles the "/" path of the Multipass handler.
// Shows login page when no JWT present
// Show continue or signout page when JWT is valid
// Show token invalid page when JWT is invalid
func (m *Multipass) rootHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
// Regular login page
p := &page{
Page: loginPage,
LoginPath: path.Join(m.opts.Basepath, "login"),
SignoutPath: path.Join(m.opts.Basepath, "signout"),
CSRFField: csrf.TemplateField(r),
}
// Show login page when there is no token
tokenStr := GetTokenRequest(r)
if len(tokenStr) == 0 {
if s := r.URL.Query().Get("next"); !strings.HasPrefix(s, m.opts.Basepath) {
p.NextURL = s
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
m.opts.Template.ExecuteTemplate(w, "page", p)
return
}
pk, err := PrivateKeyFromEnvironment()
if err != nil || pk == nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
claims, err := validateToken(tokenStr, pk.PublicKey)
if err != nil {
p.Page = tokenInvalidPage
if s := r.URL.Query().Get("next"); !strings.HasPrefix(s, m.opts.Basepath) {
p.NextURL = s
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
m.opts.Template.ExecuteTemplate(w, "page", p)
return
}
// Authorize handle claim
if ok := m.opts.Service.Listed(claims.Handle); !ok {
p.Page = tokenInvalidPage
w.Header().Add("Content-Type", "text/html; charset=utf-8")
m.opts.Template.ExecuteTemplate(w, "page", p)
return
}
if cookie, err := r.Cookie("next"); err == nil {
p.NextURL = cookie.Value
}
p.Page = continueOrSignoutPage
w.Header().Add("Content-Type", "text/html; charset=utf-8")
m.opts.Template.ExecuteTemplate(w, "page", p)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
func (m *Multipass) loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
if tokenStr := r.URL.Query().Get("token"); len(tokenStr) > 0 {
cookie := &http.Cookie{
Name: "jwt_token",
Value: tokenStr,
Path: "/",
}
http.SetCookie(w, cookie)
}
if nexturl := r.URL.Query().Get("next"); len(nexturl) > 0 {
cookie := &http.Cookie{
Name: "next",
Value: nexturl,
Path: "/",
}
http.SetCookie(w, cookie)
}
http.Redirect(w, r, m.opts.Basepath, http.StatusSeeOther)
return
}
if r.Method == "POST" {
r.ParseForm()
handle := r.PostForm.Get("handle")
if len(handle) > 0 {
if m.opts.Service.Listed(handle) {
token, err := m.AccessToken(handle)
if err != nil {
log.Print(err)
}
values := url.Values{}
if s := r.PostForm.Get("next"); len(s) > 0 {
values.Set("next", s)
}
loginURL, err := NewLoginURL(m.siteaddr, m.opts.Basepath, token, values)
if err != nil {
log.Print(err)
}
if err := m.opts.Service.Notify(handle, loginURL.String()); err != nil {
log.Print(err)
}
}
// Redirect even when the handle is not listed in order to prevent guessing
location := path.Join(m.opts.Basepath, "confirm")
http.Redirect(w, r, location, http.StatusSeeOther)
return
}
http.Redirect(w, r, m.opts.Basepath, http.StatusSeeOther)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
func (m *Multipass) confirmHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
p := &page{
Page: tokenSentPage,
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
m.opts.Template.ExecuteTemplate(w, "page", p)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
// signoutHandler deletes the jwt_token cookie and redirect to the login
// location.
func (m *Multipass) signoutHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
if cookie, err := r.Cookie("jwt_token"); err == nil {
cookie.Expires = time.Now().AddDate(-1, 0, 0)
cookie.MaxAge = -1
cookie.Path = "/"
http.SetCookie(w, cookie)
}
http.Redirect(w, r, m.opts.Basepath, http.StatusSeeOther)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
// publicKeyHandler writes the public key to the given ResponseWriter to allow
// other to validate Multipass signed tokens.
func (m *Multipass) publicKeyHandler(w http.ResponseWriter, r *http.Request) {
key, err := PrivateKeyFromEnvironment()
if err != nil || key == nil {
w.WriteHeader(http.StatusInternalServerError)
}
err = pemEncodePublicKey(w, &key.PublicKey)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/pkix-cert")
}
// AccessToken returns a new signed and serialized token with the given handle
// as a claim.
func (m *Multipass) AccessToken(handle string) (tokenStr string, err error) {
claims := &Claims{
Handle: handle,
Expires: time.Now().Add(m.opts.Expires).Unix(),
}
key, err := PrivateKeyFromEnvironment()
if err != nil || key == nil {
return "", err
}
signer, err := jose.NewSigner(jose.PS512, key)
if err != nil {
return "", err
}
return accessToken(signer, claims)
}
// NewLoginURL returns a login url which can be used as a time limited login.
// Optional values will be encoded in the login URL.
func NewLoginURL(siteaddr, Basepath, token string, v url.Values) (*url.URL, error) {
u, err := url.Parse(siteaddr)
if err != nil {
return u, err
}
u.Path = path.Join(Basepath, "login")
v.Set("token", token)
u.RawQuery = v.Encode()
return u, nil
}
// ResourceHandler validates the token in the request before it writes the response.
// It adds the user handle if the user is authenticated and signs the any
// Multipass specific headers.
func ResourceHandler(w http.ResponseWriter, r *http.Request, m *Multipass) (int, error) {
var key *rsa.PrivateKey
var handle string
var header = make(http.Header)
token := GetTokenRequest(r)
if len(token) > 0 {
k, err := PrivateKeyFromEnvironment()
if err != nil || k == nil {
return http.StatusInternalServerError, errors.New("parsing private key from env failed")
}
key = k
claims, err := validateToken(token, key.PublicKey)
if err == nil {
handle = claims.Handle
header.Add("Multipass-Handle", handle)
}
}
// Verify if user identified by handle is authorized to access resource
if ok := m.opts.Service.Authorized(handle, r.Method, r.URL.String()); !ok {
if handle == "" && token != "" {
return http.StatusForbidden, ErrInvalidToken
}
if handle == "" {
w.Header().Set("Www-Authenticate", "Bearer token_type=\"JWT\"")
return http.StatusUnauthorized, ErrUnauthorized
}
return http.StatusForbidden, ErrForbidden
}
// Sign header
if len(header) > 0 {
if key == nil {
key, err := PrivateKeyFromEnvironment()
if err != nil || key == nil {
return http.StatusInternalServerError, errors.New("parsing private key from env failed")
}
}
SignHeader(header, key)
copyHeader(r.Header, header)
}
// Pass on authorized handle to downstream handlers
return http.StatusOK, nil
}
// AuthHandler wraps any http.Handler to provide authentication using the
// given Multipass instance.
// Handlers from other http routers can be wrapped with little effort by
// copying the AuthHandler and make minor changes.
func AuthHandler(next http.Handler, m *Multipass) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if ok := strings.HasPrefix(r.URL.Path, m.Basepath()); ok {
m.ServeHTTP(w, r)
return
}
if _, err := ResourceHandler(w, r, m); err != nil {
v := url.Values{"next": []string{r.URL.String()}}
u := &url.URL{
Path: m.Basepath(),
RawQuery: v.Encode(),
}
location := u.String()
http.Redirect(w, r, location, http.StatusSeeOther)
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// UserService is an interface used by the Multipass instance to query about
// listed handles, authorized resource access and to notify users about login
// urls. A handle is a unique user identifier, e.g. username, email address.
type UserService interface {
// Listed returns true when the given handle is listed with the
// service.
Listed(handle string) bool
// Authorized returns true when the user identified by the given handle is
// authorized to access the given resource at rawurl with the given method.
Authorized(handle, method, rawurl string) bool
// Notify returns nil when the given login URL is successfully
// communicated to the given handle.
Notify(handle, loginurl string) error
// Close closes any open connections.
Close() error
}