-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_web_authn.go
269 lines (221 loc) · 7.95 KB
/
rest_web_authn.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
package main
// import (
// "encoding/binary"
// "encoding/json"
// "math/rand"
// "net/http"
// "github.com/duo-labs/webauthn/protocol"
// "github.com/duo-labs/webauthn/webauthn"
// "github.com/gorilla/sessions"
// "github.com/pkg/errors"
// )
// // WebauthnSession is web authn session name
// const WebauthnSession = "webauthn-session"
// // ErrMarshal is returned if unexpected data is present in a webauthn session.
// var ErrMarshal = errors.New("error unmarshaling data")
// var webAuthn *webauthn.WebAuthn
// var sessionStore *Store
// func init() {
// var err error
// webAuthn, err = webauthn.New(&webauthn.Config{
// RPDisplayName: "KnowlGraph", // Display Name for your site
// RPID: "localhost", // Generally the domain name for your site
// RPOrigin: "http://localhost:20010", // The origin URL for WebAuthn requests
// // RPIcon: "https://duo.com/logo.png", // Optional icon URL for your site
// })
// if err != nil {
// panicIfErrNotNil(err)
// }
// sessionStore = NewStore()
// }
// var device *WebAuthnDevice
// func beginRegistration(c *Context) error {
// // init user
// device = NewUser("", "")
// registerOptions := func(credCreationOpts *protocol.PublicKeyCredentialCreationOptions) {
// credCreationOpts.CredentialExcludeList = device.CredentialExcludeList()
// credCreationOpts.AuthenticatorSelection.AuthenticatorAttachment = protocol.Platform
// }
// // generate PublicKeyCredentialCreationOptions, session data
// options, sessionData, err := webAuthn.BeginRegistration(
// device,
// registerOptions,
// )
// if err != nil {
// return c.InternalServerError(err.Error())
// }
// // store session data as marshaled JSON
// err = sessionStore.SaveWebauthnSession("registration", sessionData, c.Request, c.Writer)
// if err != nil {
// return c.InternalServerError(err.Error())
// }
// return c.Ok(&options)
// }
// func finishRegistration(c *Context) error {
// // load the session data
// sessionData, err := sessionStore.GetWebauthnSession("registration", c.Request, c.Writer)
// if err != nil {
// c.BadRequest(err.Error())
// }
// c.SetCookie(WebauthnSession, "", -1, "/", "", !config.Debug, false)
// credential, err := webAuthn.FinishRegistration(device, sessionData, c.Request)
// if err != nil {
// return c.InternalServerError(err.Error())
// }
// device.AddCredential(*credential)
// if setAuthorization(c.Context, 1) != nil {
// return c.InternalServerError(err.Error())
// }
// return c.Ok(true)
// }
// func beginLogin(c *Context) error {
// // get username
// // webUser := NewUser("", "")
// loginOption := func(credCreationOpts *protocol.PublicKeyCredentialRequestOptions) {
// credCreationOpts.UserVerification = protocol.VerificationPreferred
// }
// // generate PublicKeyCredentialRequestOptions, session data
// options, sessionData, err := webAuthn.BeginLogin(device, loginOption)
// if err != nil {
// return c.InternalServerError(err.Error())
// }
// // store session data as marshaled JSON
// err = sessionStore.SaveWebauthnSession("authentication", sessionData, c.Request, c.Writer)
// if err != nil {
// return c.InternalServerError(err.Error())
// }
// return c.Ok(&options)
// }
// func finishLogin(c *Context) error {
// // load the session data
// sessionData, err := sessionStore.GetWebauthnSession("authentication", c.Request, c.Writer)
// if err != nil {
// return c.BadRequest(err.Error())
// }
// c.SetCookie(WebauthnSession, "", -1, "/", "", !config.Debug, false)
// // in an actual implementation, we should perform additional checks on
// // the returned 'credential', i.e. check 'credential.Authenticator.CloneWarning'
// // and then increment the credentials counter
// _, err = webAuthn.FinishLogin(device, sessionData, c.Request)
// if err != nil {
// return c.BadRequest(err.Error())
// }
// return c.Ok(true)
// }
// // WebAuthnDevice represents the user model
// type WebAuthnDevice struct {
// id uint64
// name string
// displayName string
// credentials []webauthn.Credential
// }
// // NewUser creates and returns a new User
// func NewUser(name string, displayName string) *WebAuthnDevice {
// user := &WebAuthnDevice{}
// user.id = randomUint64()
// user.name = name
// user.displayName = displayName
// // user.credentials = []webauthn.Credential{}
// return user
// }
// func randomUint64() uint64 {
// buf := make([]byte, 8)
// rand.Read(buf)
// return binary.LittleEndian.Uint64(buf)
// }
// // WebAuthnID returns the user's ID
// func (u WebAuthnDevice) WebAuthnID() []byte {
// buf := make([]byte, binary.MaxVarintLen64)
// binary.PutUvarint(buf, uint64(u.id))
// return buf
// }
// // WebAuthnName returns the user's username
// func (u WebAuthnDevice) WebAuthnName() string {
// return u.name
// }
// // WebAuthnDisplayName returns the user's display name
// func (u WebAuthnDevice) WebAuthnDisplayName() string {
// return u.displayName
// }
// // WebAuthnIcon is not (yet) implemented
// func (u WebAuthnDevice) WebAuthnIcon() string {
// return ""
// }
// // AddCredential associates the credential to the user
// func (u *WebAuthnDevice) AddCredential(cred webauthn.Credential) {
// u.credentials = append(u.credentials, cred)
// }
// // WebAuthnCredentials returns credentials owned by the user
// func (u WebAuthnDevice) WebAuthnCredentials() []webauthn.Credential {
// return u.credentials
// }
// // CredentialExcludeList returns a CredentialDescriptor array filled
// // with all the user's credentials
// func (u WebAuthnDevice) CredentialExcludeList() []protocol.CredentialDescriptor {
// credentialExcludeList := []protocol.CredentialDescriptor{}
// for _, cred := range u.credentials {
// descriptor := protocol.CredentialDescriptor{
// Type: protocol.PublicKeyCredentialType,
// CredentialID: cred.ID,
// }
// credentialExcludeList = append(credentialExcludeList, descriptor)
// }
// return credentialExcludeList
// }
// // =======================================================================
// // Store is a wrapper around sessions.CookieStore which provides some helper
// // methods related to webauthn operations.
// type Store struct {
// *sessions.CookieStore
// }
// // NewStore returns a new session store.
// func NewStore(keyPairs ...[]byte) *Store {
// // Generate a default encryption key if one isn't provided
// if len(keyPairs) == 0 {
// key := []byte(RandNdigMbitString(32))
// keyPairs = append(keyPairs, key)
// }
// store := &Store{
// sessions.NewCookieStore(keyPairs...),
// }
// return store
// }
// // Set stores a value to the session with the provided key.
// func (store *Store) Set(key string, value interface{}, r *http.Request, w http.ResponseWriter) error {
// session, err := store.Get(r, WebauthnSession)
// if err != nil {
// return err
// }
// session.Values[key] = value
// session.Save(r, w)
// return nil
// }
// // SaveWebauthnSession marhsals and saves the webauthn data to the provided
// // key given the request and responsewriter
// func (store *Store) SaveWebauthnSession(key string, data *webauthn.SessionData, r *http.Request, w http.ResponseWriter) error {
// marshaledData, err := json.Marshal(data)
// if err != nil {
// return err
// }
// return store.Set(key, marshaledData, r, w)
// }
// // GetWebauthnSession unmarshals and returns the webauthn session information
// // from the session cookie.
// func (store *Store) GetWebauthnSession(key string, r *http.Request, w http.ResponseWriter) (webauthn.SessionData, error) {
// sessionData := webauthn.SessionData{}
// session, err := store.Get(r, WebauthnSession)
// if err != nil {
// return sessionData, err
// }
// assertion, ok := session.Values[key].([]byte)
// if !ok {
// return sessionData, ErrMarshal
// }
// err = json.Unmarshal(assertion, &sessionData)
// if err != nil {
// return sessionData, err
// }
// // Delete the value from the session now that it's been read
// delete(session.Values, key)
// return sessionData, nil
// }