-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
272 lines (212 loc) · 6.44 KB
/
server.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
package webauthn
import (
"encoding/gob"
"sync"
"github.com/admpub/log"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/webx-top/echo"
)
func init() {
gob.Register(&webauthn.SessionData{})
}
func New(user UserHandler, lazyInit ...func(echo.Context) *webauthn.Config) *Server {
a := &Server{
user: user,
}
if len(lazyInit) > 0 {
a.lazyInit = lazyInit[0]
}
return a
}
type Server struct {
webAuthn *webauthn.WebAuthn
lazyInit func(echo.Context) *webauthn.Config
onceInit sync.Once
user UserHandler
lock sync.RWMutex
}
func (s *Server) Init(cfg *webauthn.Config) error {
var err error
s.lock.Lock()
SetConfigDefaults(cfg)
s.webAuthn, err = webauthn.New(cfg)
s.lock.Unlock()
return err
}
func (s *Server) Object(ctx echo.Context) *webauthn.WebAuthn {
s.lock.RLock()
a := s.webAuthn
s.lock.RUnlock()
if a == nil && s.lazyInit != nil {
s.onceInit.Do(func() {
cfg := s.lazyInit(ctx)
err := s.Init(cfg)
if err != nil {
log.Error(err)
return
}
a = s.webAuthn
})
}
return a
}
func (s *Server) RegisterRoute(r echo.RouteRegister) {
g := r.Group(`/webauthn`)
g.Post("/register/begin/:username", s.handleBeginRegistration).SetName(`webauthn.beginRegister`)
g.Post("/register/finish/:username", s.handleFinishRegistration).SetName(`webauthn.finishRegister`)
g.Post("/login/begin/:username", s.handleBeginLogin).SetName(`webauthn.beginLogin`)
g.Post("/login/finish/:username", s.handleFinishLogin).SetName(`webauthn.finishLogin`)
g.Post("/unbind/begin/:username", s.handleBeginUnbind).SetName(`webauthn.beginUnbind`)
g.Post("/unbind/finish/:username", s.handleFinishUnbind).SetName(`webauthn.finishUnbind`)
}
func (s *Server) RegisterRouteForLogin(r echo.RouteRegister) {
g := r.Group(`/webauthn`)
g.Post("/login/begin/:username", s.handleBeginLogin).SetName(`webauthn.beginLogin`)
g.Post("/login/finish/:username", s.handleFinishLogin).SetName(`webauthn.finishLogin`)
}
// - register -
func (s *Server) handleBeginRegistration(ctx echo.Context) error {
// get username/friendly name
username := ctx.Param("username")
// get user
user, err := s.user.GetUser(ctx, username, TypeRegister, StageBegin)
if err != nil {
return err
}
registerOptions := func(credCreationOpts *protocol.PublicKeyCredentialCreationOptions) {
credCreationOpts.CredentialExcludeList = makeCredentialDescriptorList(ctx, user)
}
// generate PublicKeyCredentialCreationOptions, session data
options, sessionData, err := s.Object(ctx).BeginRegistration(
user,
registerOptions,
)
if err != nil {
return err
}
ctx.Session().Set(sessionKeyRegister, sessionData)
return ctx.JSON(options)
}
func (s *Server) handleFinishRegistration(ctx echo.Context) error {
// get username
username := ctx.Param("username")
// get user
user, err := s.user.GetUser(ctx, username, TypeRegister, StageFinish)
if err != nil {
return err
}
// load the session data
sessionData, ok := ctx.Session().Get(sessionKeyRegister).(*webauthn.SessionData)
if !ok {
return echo.ErrBadRequest
}
credential, err := s.Object(ctx).FinishRegistration(user, *sessionData, ctx.Request().StdRequest())
if err != nil {
return err
}
err = s.user.Register(ctx, user, credential)
if err != nil {
return err
}
ctx.Session().Delete(sessionKeyRegister)
return ctx.JSON("Registration Success")
}
// - login -
func (s *Server) handleBeginLogin(ctx echo.Context) error {
// get username
username := ctx.Param("username")
// get user
user, err := s.user.GetUser(ctx, username, TypeLogin, StageBegin)
if err != nil {
return err
}
loginOptions := func(credCreationOpts *protocol.PublicKeyCredentialRequestOptions) {
credCreationOpts.AllowedCredentials = makeCredentialDescriptorList(ctx, user)
}
// generate PublicKeyCredentialRequestOptions, session data
options, sessionData, err := s.Object(ctx).BeginLogin(user, loginOptions)
if err != nil {
return err
}
// store session data as marshaled JSON
ctx.Session().Set(sessionKeyLogin, sessionData)
return ctx.JSON(options)
}
func (s *Server) handleFinishLogin(ctx echo.Context) error {
// get username
username := ctx.Param("username")
// get user
user, err := s.user.GetUser(ctx, username, TypeLogin, StageFinish)
if err != nil {
return err
}
// load the session data
sessionData, ok := ctx.Session().Get(sessionKeyLogin).(*webauthn.SessionData)
if !ok {
return echo.ErrBadRequest
}
// 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
credential, err := s.Object(ctx).FinishLogin(user, *sessionData, ctx.Request().StdRequest())
if err != nil {
return err
}
// handle successful login
err = s.user.Login(ctx, user, credential)
if err != nil {
return err
}
ctx.Session().Delete(sessionKeyLogin)
return ctx.JSON("Login Success")
}
// - unbind -
func (s *Server) handleBeginUnbind(ctx echo.Context) error {
// get username
username := ctx.Param("username")
// get user
user, err := s.user.GetUser(ctx, username, TypeUnbind, StageBegin)
if err != nil {
return err
}
loginOptions := func(credCreationOpts *protocol.PublicKeyCredentialRequestOptions) {
credCreationOpts.AllowedCredentials = makeCredentialDescriptorList(ctx, user)
}
// generate PublicKeyCredentialRequestOptions, session data
options, sessionData, err := s.Object(ctx).BeginLogin(user, loginOptions)
if err != nil {
return err
}
// store session data as marshaled JSON
ctx.Session().Set(sessionKeyUnbind, sessionData)
return ctx.JSON(options)
}
func (s *Server) handleFinishUnbind(ctx echo.Context) error {
// get username
username := ctx.Param("username")
// get user
user, err := s.user.GetUser(ctx, username, TypeUnbind, StageFinish)
if err != nil {
return err
}
// load the session data
sessionData, ok := ctx.Session().Get(sessionKeyUnbind).(*webauthn.SessionData)
if !ok {
return echo.ErrBadRequest
}
// 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
credential, err := s.Object(ctx).FinishLogin(user, *sessionData, ctx.Request().StdRequest())
if err != nil {
return err
}
// handle successful unbind
err = s.user.Unbind(ctx, user, credential)
if err != nil {
return err
}
ctx.Session().Delete(sessionKeyUnbind)
return ctx.JSON("Unbind Success")
}