-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
85 lines (71 loc) ยท 1.7 KB
/
options.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
package passkey
import (
"fmt"
"strings"
"time"
)
type Option func(*Passkey)
// WithLogger sets the logger for the passkey instance.
func WithLogger(l Logger) Option {
return func(p *Passkey) {
if l != nil {
p.log = l
}
}
}
// WithInsecureCookie sets Cookie.Secure to false. This is useful for development. Do not use in production.
func WithInsecureCookie() Option {
return func(p *Passkey) {
p.cookieSettings.Secure = false
}
}
// WithSessionCookieNamePrefix sets custom prefix for names of the session cookies instead of default.
func WithSessionCookieNamePrefix(prefix string) Option {
return func(p *Passkey) {
if prefix != "" {
p.cookieSettings.authSessionName = camelCaseConcat(
prefix,
strings.TrimPrefix(p.cookieSettings.authSessionName, defaultSessionNamePrefix),
)
p.cookieSettings.userSessionName = camelCaseConcat(
prefix,
strings.TrimPrefix(p.cookieSettings.userSessionName, defaultSessionNamePrefix),
)
}
}
}
// WithUserSessionMaxAge sets the max age of the user session cookie.
func WithUserSessionMaxAge(maxAge time.Duration) Option {
return func(p *Passkey) {
if maxAge > 0 {
p.cookieSettings.userSessionMaxAge = maxAge
}
}
}
func camelCaseConcat(ws ...string) string {
if len(ws) == 0 {
return ""
}
if len(ws) == 1 {
return ws[0]
}
invalidChars := []string{" ", "\t", "\n", "\r"}
sb := strings.Builder{}
sb.WriteString(strings.ToLower(ws[0]))
for i := 1; i < len(ws); i++ {
w := ws[i]
for _, ch := range invalidChars {
w = strings.ReplaceAll(w, ch, "")
}
if w == "" {
continue
}
sb.WriteString(
fmt.Sprintf(
"%s%s",
string(strings.ToUpper(w)[0]),
strings.ToLower(w)[1:],
))
}
return sb.String()
}