-
Notifications
You must be signed in to change notification settings - Fork 3
/
passkey.go
181 lines (146 loc) Β· 4.01 KB
/
passkey.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
package passkey
import (
"fmt"
"net/http"
"time"
"github.com/egregors/passkey/deps"
"github.com/go-webauthn/webauthn/webauthn"
logger "github.com/egregors/passkey/log"
)
const (
pathRegisterBegin = "/passkey/registerBegin"
pathRegisterFinish = "/passkey/registerFinish"
pathLoginBegin = "/passkey/loginBegin"
pathLoginFinish = "/passkey/loginFinish"
defaultSessionNamePrefix = "pk"
defaultAuthSessionName = "asid"
defaultUserSessionName = "usid"
defaultAuthSessionMaxAge = 5 * time.Minute
defaultUserSessionMaxAge = 60 * time.Minute
)
type Config struct {
WebauthnConfig *webauthn.Config
UserStore
AuthSessionStore SessionStore[webauthn.SessionData]
UserSessionStore SessionStore[UserSessionData]
}
type UserSessionData struct {
UserID []byte
Expires time.Time
}
type CookieSettings struct {
Path string
authSessionName string
userSessionName string
authSessionMaxAge time.Duration
userSessionMaxAge time.Duration
Secure bool
HttpOnly bool //nolint:stylecheck // naming from http.Cookie
SameSite http.SameSite
}
type Passkey struct {
cfg Config
webAuthn deps.WebAuthnInterface
userStore UserStore
authSessionStore SessionStore[webauthn.SessionData]
userSessionStore SessionStore[UserSessionData]
mux *http.ServeMux
staticMux *http.ServeMux
log Logger
cookieSettings CookieSettings
}
// New creates new Passkey instance
func New(cfg Config, opts ...Option) (*Passkey, error) {
p := &Passkey{
cfg: cfg,
userStore: cfg.UserStore,
authSessionStore: cfg.AuthSessionStore,
userSessionStore: cfg.UserSessionStore,
mux: http.NewServeMux(),
staticMux: http.NewServeMux(),
}
p.setupCookieSettings()
p.setupOptions(opts)
p.setupRoutes()
err := p.setupWebAuthn()
if err != nil {
return nil, fmt.Errorf("can't create webauthn: %w", err)
}
if err := p.must(); err != nil {
return nil, fmt.Errorf("invalid cfg: %w", err)
}
p.raiseWarnings()
return p, nil
}
func (p *Passkey) must() error {
return mustNotNil(map[string]any{
"userStore": p.userStore,
"authSessionStore": p.authSessionStore,
"userSessionStore": p.userSessionStore,
})
}
func mustNotNil(nillable map[string]any) error {
for k, v := range nillable {
if v == nil {
return fmt.Errorf("%s can't be nil", k)
}
}
return nil
}
func (p *Passkey) setupOptions(opts []Option) {
setupDefaultOptions(p)
for _, opt := range opts {
opt(p)
}
}
func setupDefaultOptions(p *Passkey) {
defaultOpts := []Option{
WithLogger(logger.NewLogger()),
WithSessionCookieNamePrefix(defaultSessionNamePrefix),
WithUserSessionMaxAge(defaultUserSessionMaxAge),
}
for _, opt := range defaultOpts {
opt(p)
}
}
func (p *Passkey) raiseWarnings() {
if p.cookieSettings.userSessionMaxAge == 0 {
p.log.Warnf("session max age is not set")
}
if !p.cookieSettings.Secure {
p.log.Warnf("cookie is not secure!")
}
}
func (p *Passkey) setupWebAuthn() error {
webAuthn, err := webauthn.New(p.cfg.WebauthnConfig)
if err != nil {
fmt.Printf("[FATA] %s", err.Error())
p.log.Errorf("can't create webauthn: %s", err.Error())
return err
}
p.webAuthn = webAuthn
return nil
}
func (p *Passkey) setupRoutes() {
p.mux.HandleFunc(pathRegisterBegin, p.beginRegistration)
p.mux.HandleFunc(pathRegisterFinish, p.finishRegistration)
p.mux.HandleFunc(pathLoginBegin, p.beginLogin)
p.mux.HandleFunc(pathLoginFinish, p.finishLogin)
p.staticMux.Handle("/", http.FileServer(http.Dir("./static")))
}
// MountRoutes mounts passkey routes to mux
func (p *Passkey) MountRoutes(mux *http.ServeMux, path string) {
mux.Handle(path, http.StripPrefix(path[:len(path)-1], p.mux))
}
func (p *Passkey) setupCookieSettings() {
p.cookieSettings = CookieSettings{
Path: "/",
authSessionName: defaultAuthSessionName,
userSessionName: defaultUserSessionName,
authSessionMaxAge: defaultAuthSessionMaxAge,
userSessionMaxAge: defaultUserSessionMaxAge,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
}
}