-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
56 lines (48 loc) · 1.73 KB
/
config.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
package passwordless
import (
"crypto/rand"
"encoding/hex"
"time"
)
// Config holds all configurable aspects of the passwordless flow.
type Config struct {
// CodeLength is the number of digits (or characters) in the generated code.
// For example, 6 => "123456".
CodeLength int
// TokenExpiry specifies how long a generated token remains valid (e.g., 15 minutes).
TokenExpiry time.Duration
// IDGenerator is a function that returns a unique string identifier
// for each token stored in the TokenStore.
//
// By default, this creates a 16-byte random hex string (32 hex chars),
// but you can plug in your own function for different formats.
IDGenerator func() string
// CodeCharset is an optional set of characters to use when generating the code.
// If empty, the library might default to digits 0-9.
// For example, "0123456789" for numeric codes
// or "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for alphanumeric codes.
CodeCharset string
// MaxFailedAttempts is the maximum number of failed attempts allowed before the token is invalidated.
MaxFailedAttempts int
}
// DefaultConfig provides sensible defaults for a typical passwordless flow.
func DefaultConfig() Config {
return Config{
CodeLength: 6,
TokenExpiry: 15 * time.Minute,
IDGenerator: defaultIDGenerator,
CodeCharset: "0123456789", // numeric-only
MaxFailedAttempts: 3,
}
}
// defaultIDGenerator returns a random 16-byte hex string.
// This is used if the user doesn't supply a custom IDGenerator.
func defaultIDGenerator() string {
b := make([]byte, 16)
_, err := rand.Read(b)
// In production code, you might handle err or panic if it fails
if err != nil {
panic("failed to generate random bytes for ID")
}
return hex.EncodeToString(b)
}