-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
80 lines (70 loc) · 3.78 KB
/
server.go
File metadata and controls
80 lines (70 loc) · 3.78 KB
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
package faroe
import (
"time"
"golang.org/x/sync/semaphore"
)
// Use [NewServer].
type ServerStruct struct {
mainStorage MainStorageInterface
cache CacheInterface
userStore UserStoreInterface
errorLogger ActionErrorLoggerInterface
userPasswordHashAlgorithms []PasswordHashAlgorithmInterface
temporaryPasswordHashAlgorithm PasswordHashAlgorithmInterface
passwordHashingSemaphore *semaphore.Weighted
clock ClockInterface
newEmailAddressChecker EmailAddressCheckerInterface
emailSender EmailSenderInterface
sessionConfig SessionConfigStruct
verifyUserPasswordRateLimit *tokenBucketRateLimit
sendEmailRateLimit *tokenBucketRateLimit
verifyEmailAddressVerificationCodeEmailAddressRateLimit *tokenBucketRateLimit
verifyUserPasswordResetTemporaryPasswordUserRateLimit *tokenBucketRateLimit
}
// All interfaces must be defined and cannot be nil.
//
// Storage entry keys are not globally-scoped. Different entries in mainStorage, cache, and rateLimitStorage may share the same key.
//
// maxConcurrentPasswordHashingProcesses defines the maximum number of concurrent processes for user password and temporary password hashing.
//
// emailAddressChecker is used for checking email addresses for signup and new email addresses of user email address updates.
// It is not used in for sign ins or user password resets.
//
// InactivityTimeout and ActivityCheckInterval should be a non-zero value in sessionConfig.
func NewServer(
mainStorage MainStorageInterface,
cache CacheInterface,
rateLimitStorage RateLimitStorageInterface,
userStore UserStoreInterface,
errorLogger ActionErrorLoggerInterface,
userPasswordHashAlgorithms []PasswordHashAlgorithmInterface,
temporaryPasswordHashAlgorithm PasswordHashAlgorithmInterface,
maxConcurrentPasswordHashingProcesses int,
clock ClockInterface,
newEmailAddressChecker EmailAddressCheckerInterface,
emailSender EmailSenderInterface,
sessionConfig SessionConfigStruct,
) *ServerStruct {
verifyUserPasswordRateLimit := newTokenBucketRateLimit(rateLimitStorage, rateLimitStorageKeyPrefixVerifyUserPasswordRateLimit, clock, 5, time.Minute)
sendEmailRateLimit := newTokenBucketRateLimit(rateLimitStorage, rateLimitStorageKeyPrefixSendEmailRateLimit, clock, 5, 30*time.Minute)
verifyEmailAddressVerificationCodeEmailAddressRateLimit := newTokenBucketRateLimit(rateLimitStorage, rateLimitStorageKeyPrefixVerifyEmailAddressVerificationCodeEmailAddressRateLimit, clock, 5, time.Minute)
verifyUserPasswordResetTemporaryPasswordUserRateLimit := newTokenBucketRateLimit(rateLimitStorage, rateLimitStorageKeyPrefixVerifyUserPasswordResetTemporaryPasswordUserRateLimit, clock, 5, time.Minute)
action := &ServerStruct{
mainStorage: mainStorage,
cache: cache,
userStore: userStore,
errorLogger: errorLogger,
userPasswordHashAlgorithms: userPasswordHashAlgorithms,
temporaryPasswordHashAlgorithm: temporaryPasswordHashAlgorithm,
passwordHashingSemaphore: semaphore.NewWeighted(int64(maxConcurrentPasswordHashingProcesses)),
clock: clock,
newEmailAddressChecker: newEmailAddressChecker,
emailSender: emailSender,
sessionConfig: sessionConfig,
verifyUserPasswordRateLimit: verifyUserPasswordRateLimit,
sendEmailRateLimit: sendEmailRateLimit,
verifyEmailAddressVerificationCodeEmailAddressRateLimit: verifyEmailAddressVerificationCodeEmailAddressRateLimit,
verifyUserPasswordResetTemporaryPasswordUserRateLimit: verifyUserPasswordResetTemporaryPasswordUserRateLimit,
}
return action
}