forked from pomerium/verify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_webauthn.go
113 lines (94 loc) · 3.05 KB
/
http_webauthn.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
package verify
import (
"encoding/json"
"io"
"net/http"
"github.com/pomerium/verify/internal/storage"
"github.com/pomerium/webauthn"
"github.com/rs/zerolog/log"
)
const maxBodySize = 4 * 1024 * 1024
func (srv *Server) serveAPIWebAuthnAuthenticate(w http.ResponseWriter, r *http.Request) {
var req storage.WebAuthnAuthenticateRequest
err := decodeJSONBody(r, &req)
if err != nil {
log.Error().Err(err).Msg("bad request for webauthn authenticate")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = srv.storage.SetAuthenticateRequest(r.Context(), &req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
origin := getRPOrigin(r)
if clientData, err := req.Credential.Response.UnmarshalClientData(); err == nil {
// use the provided origin
origin = clientData.Origin
}
rp := webauthn.NewRelyingParty(origin, srv.storage)
// This verification is insecure because we trust the options provided by the client. A real implementation
// should generate the options (particularly the challenge) on the server.
credential, err := rp.VerifyAuthenticationCeremony(r.Context(), req.Options, req.Credential)
if err != nil {
log.Error().Err(err).Msg("webauthn: invalid authentication ceremony")
http.Error(w, err.Error(), http.StatusForbidden)
return
}
_ = credential
}
func (srv *Server) serveAPIWebAuthnRegister(w http.ResponseWriter, r *http.Request) {
var req storage.WebAuthnRegisterRequest
err := decodeJSONBody(r, &req)
if err != nil {
log.Error().Err(err).Msg("bad request for webauthn register")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = srv.storage.SetRegisterRequest(r.Context(), &req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
origin := getRPOrigin(r)
if clientData, err := req.Credential.Response.UnmarshalClientData(); err == nil {
// use the provided origin
origin = clientData.Origin
}
rp := webauthn.NewRelyingParty(origin, srv.storage)
// This verification is insecure because we trust the options provided by the client. A real implementation
// should generate the options (particularly the challenge) on the server.
credential, err := rp.VerifyRegistrationCeremony(r.Context(), req.Options, req.Credential)
if err != nil {
log.Error().Err(err).Msg("webauthn: invalid registration ceremony")
http.Error(w, err.Error(), http.StatusForbidden)
return
}
err = srv.storage.SetCredential(r.Context(), credential)
if err != nil {
log.Error().Err(err).Msg("webauthn: invalid registration ceremony")
http.Error(w, err.Error(), http.StatusForbidden)
return
}
}
func decodeJSONBody(r *http.Request, dsts ...interface{}) error {
defer func() { _ = r.Body.Close() }()
bs, err := io.ReadAll(io.LimitReader(r.Body, maxBodySize))
if err != nil {
return err
}
for _, dst := range dsts {
err = json.Unmarshal(bs, dst)
if err != nil {
return err
}
}
return nil
}
func getRPOrigin(r *http.Request) string {
scheme := "https"
if r.TLS == nil {
scheme = "http"
}
return scheme + "://" + r.Host
}