-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathadd_identity_fedcm.go
168 lines (135 loc) · 3.71 KB
/
add_identity_fedcm.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
package obligator
import (
"context"
"fmt"
"html/template"
"io"
"net/http"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/lestrrat-go/jwx/v2/jwt/openid"
)
type AddIdentityFedCmHandler struct {
mux *http.ServeMux
}
func NewAddIdentityFedCmHandler(db Database, tmpl *template.Template, jose *JOSE) *AddIdentityFedCmHandler {
mux := http.NewServeMux()
h := &AddIdentityFedCmHandler{
mux: mux,
}
mux.HandleFunc("/login-fedcm", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
data := struct {
*commonData
}{
commonData: newCommonData(nil, db, r),
}
err := tmpl.ExecuteTemplate(w, "login-fedcm.html", data)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
})
mux.HandleFunc("/complete-login-fedcm", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(405)
io.WriteString(w, "Invalid method")
return
}
r.ParseForm()
// TODO: probably need to have a HTTP-only JWT cookie with a
// PKCE code or something so we know this flow initiated from
// us
fedCmToken := r.Form.Get("fedcm-token")
// Parse the token without verifying to get the issuer, then get the JWK for full validation
unverifiedToken, err := jwt.ParseInsecure([]byte(fedCmToken), jwt.WithToken(openid.New()))
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
issuer := unverifiedToken.Issuer()
ctx := context.Background()
// TODO: Maybe this can be reused. Might be tricky in
// distributed setups though. Could probably at least prime it
// when we first see a new client_id so we don't have to
// fetch the OIDC config here which slows things down
c := jwk.NewCache(ctx)
oidcMeta, err := GetOidcConfiguration(issuer)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
err = c.Register(oidcMeta.JwksUri)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
keyset, err := c.Refresh(ctx, oidcMeta.JwksUri)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
verifiedToken, err := jwt.Parse([]byte(fedCmToken), jwt.WithKeySet(keyset), jwt.WithToken(openid.New()))
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
oidcToken, ok := verifiedToken.(openid.Token)
if !ok {
w.WriteHeader(500)
io.WriteString(w, "FedCM: Not a valid OpenId Connect token")
return
}
if len(oidcToken.Audience()) == 0 {
w.WriteHeader(400)
io.WriteString(w, "FedCM: Missing aud in OIDC token")
return
}
if oidcToken.Audience()[0] != domainToUri(r.Host) {
w.WriteHeader(401)
io.WriteString(w, "FedCM: Wrong aud in OIDC token")
return
}
cookieValue := ""
loginKeyCookie, err := getLoginCookie(db, r)
if err == nil {
cookieValue = loginKeyCookie.Value
}
email := oidcToken.Email()
newIdent := &Identity{
IdType: "email",
Id: email,
ProviderName: issuer,
Name: oidcToken.Name(),
Email: email,
EmailVerified: true,
}
cookie, err := addIdentToCookie(r.Host, db, cookieValue, newIdent, jose)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
returnUri, err := getReturnUriCookie(db, r)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
deleteReturnUriCookie(r.Host, db, w)
w.Header().Add("Set-Login", "logged-in")
http.SetCookie(w, cookie)
redirUrl := fmt.Sprintf("%s", returnUri)
http.Redirect(w, r, redirUrl, http.StatusSeeOther)
})
return h
}
func (h *AddIdentityFedCmHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}