-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.go
147 lines (128 loc) · 3.07 KB
/
auth.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
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"io"
"net/http"
"strings"
"github.com/coreos/go-oidc"
"golang.org/x/oauth2"
)
// Cookie names.
const (
cookieIDToken = "api_id_token"
cookieAuthState = "api_auth_state"
)
func newGoogleVerifier(clientID, clientSecret string) (*oauth2.Config, *oidc.IDTokenVerifier, error) {
provider, err := oidc.NewProvider(context.Background(), "https://accounts.google.com")
if err != nil {
return nil, nil, err
}
conf := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: absURL("/auth"),
Scopes: []string{"email"},
}
verifier := provider.Verifier(&oidc.Config{
ClientID: clientID,
})
return conf, verifier, nil
}
func isAdmin(email string) bool {
return email == "[email protected]" ||
strings.HasSuffix(email, "@directactioneverywhere.com")
}
func (s *server) googleEmail() (string, error) {
c, err := s.r.Cookie(cookieIDToken)
if err != nil {
return "", err
}
token, err := s.verifier.Verify(s.r.Context(), c.Value)
if err != nil {
return "", err
}
var claims struct {
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
}
err = token.Claims(&claims)
if err != nil {
return "", err
}
if !claims.EmailVerified {
return "", errors.New("email not verified")
}
return claims.Email, nil
}
func (s *server) login() {
state, err := nonce()
if err != nil {
s.serveJSON(nil, err)
return
}
http.SetCookie(s.w, &http.Cookie{
Name: cookieAuthState,
Value: state,
MaxAge: 36000,
SameSite: http.SameSiteLaxMode,
HttpOnly: true,
})
var opts []oauth2.AuthCodeOption
if s.r.URL.Query()["force"] != nil {
// If the user is currently only signed into one
// Google Account, we need to set
// prompt=select_account to force the account chooser
// dialog to appear. Otherwise, Google will just
// redirect back to us again immediately.
opts = append(opts, oauth2.SetAuthURLParam("prompt", "select_account"))
}
s.redirect(s.conf.AuthCodeURL(state, opts...))
}
func (s *server) logout() {
http.SetCookie(s.w, &http.Cookie{
Name: cookieAuthState,
MaxAge: -1,
})
http.SetCookie(s.w, &http.Cookie{
Name: cookieIDToken,
MaxAge: -1,
})
s.redirect(absURL("/admin"))
}
func (s *server) auth() {
c, err := s.r.Cookie(cookieAuthState)
if err != nil {
s.serveJSON(nil, err)
return
}
if c.Value != s.r.FormValue("state") {
s.serveJSON(nil, errors.New("state mismatch"))
return
}
token, err := s.conf.Exchange(s.r.Context(), s.r.FormValue("code"))
if err != nil {
s.serveJSON(nil, err)
return
}
idToken := token.Extra("id_token").(string)
http.SetCookie(s.w, &http.Cookie{
Name: cookieIDToken,
Value: idToken,
MaxAge: 36000,
SameSite: http.SameSiteLaxMode,
HttpOnly: true,
})
s.redirect(absURL("/admin"))
}
// nonce returns a 256-bit random hex string.
func nonce() (string, error) {
var buf [32]byte
if _, err := io.ReadFull(rand.Reader, buf[:]); err != nil {
return "", err
}
return hex.EncodeToString(buf[:]), nil
}