1
|
|
|
package config |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"os" |
5
|
|
|
"sync" |
6
|
|
|
"time" |
7
|
|
|
|
8
|
|
|
"github.com/gofiber/fiber/v2/log" |
9
|
|
|
"github.com/memnix/memnix-rest/pkg/crypto" |
10
|
|
|
myJwt "github.com/memnix/memnix-rest/pkg/jwt" |
11
|
|
|
"golang.org/x/crypto/ed25519" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
const ( |
15
|
|
|
ExpirationTimeInHours = 24 // ExpirationTimeInHours is the expiration time for the JWT token |
16
|
|
|
SQLMaxOpenConns = 10 // SQLMaxOpenConns is the max number of connections in the open connection pool |
17
|
|
|
SQLMaxIdleConns = 1 // SQLMaxIdleConns is the max number of connections in the idle connection pool |
18
|
|
|
|
19
|
|
|
OauthStateLength = 16 // OauthStateLength is the length of the state for oauth |
20
|
|
|
|
21
|
|
|
RedisDefaultExpireTime = 6 * time.Hour // RedisDefaultExpireTime is the default expiration time for keys |
22
|
|
|
|
23
|
|
|
CacheExpireTime = 10 * time.Second // CacheExpireTime is the expiration time for the cache |
24
|
|
|
|
25
|
|
|
GCThresholdPercent = 0.7 // GCThresholdPercent is the threshold for garbage collection |
26
|
|
|
|
27
|
|
|
GCLimit = 1024 * 1024 * 1024 // GCLimit is the limit for garbage collection |
28
|
|
|
|
29
|
|
|
RistrettoMaxCost = 3 * MB // RistrettoMaxCost is the maximum cost |
30
|
|
|
RistrettoBufferItems = 32 // RistrettoBufferItems is the number of items per get buffer |
31
|
|
|
RistrettoNumCounters = 1e4 // RistrettoNumCounters is the number of counters |
32
|
|
|
|
33
|
|
|
MB = 1024 * 1024 // MB is the number of bytes in a megabyte |
34
|
|
|
|
35
|
|
|
MaxPasswordLength = 72 // MaxPasswordLength is the max password length |
36
|
|
|
MinPasswordLength = 8 // MinPasswordLength is the min password length |
37
|
|
|
|
38
|
|
|
SentryFlushTimeout = 2 * time.Second // SentryFlushTimeout is the timeout for flushing sentry |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
type JwtInstanceSingleton struct { |
42
|
|
|
jwtInstance myJwt.Instance |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
var ( |
46
|
|
|
jwtInstance *JwtInstanceSingleton //nolint:gochecknoglobals //Singleton |
47
|
|
|
jwtOnce sync.Once //nolint:gochecknoglobals //Singleton |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
func GetJwtInstance() *JwtInstanceSingleton { |
51
|
|
|
jwtOnce.Do(func() { |
52
|
|
|
jwtInstance = &JwtInstanceSingleton{} |
53
|
|
|
}) |
54
|
|
|
return jwtInstance |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func (j *JwtInstanceSingleton) GetJwt() myJwt.Instance { |
58
|
|
|
return j.jwtInstance |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
func (j *JwtInstanceSingleton) SetJwt(instance myJwt.Instance) { |
62
|
|
|
j.jwtInstance = instance |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// PasswordConfigStruct is the struct for the password config. |
66
|
|
|
type PasswordConfigStruct struct { |
67
|
|
|
Iterations uint32 // Iterations to use for Argon2ID |
68
|
|
|
Memory uint32 // Memory to use for Argon2ID |
69
|
|
|
Threads uint8 // Threads to use for Argon2ID |
70
|
|
|
KeyLen uint32 // KeyLen to use for Argon2ID |
71
|
|
|
SaltLen uint32 // SaltLen to use for Argon2ID |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
type KeyManager struct { |
75
|
|
|
privateKey ed25519.PrivateKey |
76
|
|
|
publicKey ed25519.PublicKey |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
var ( |
80
|
|
|
keyManagerInstance *KeyManager //nolint:gochecknoglobals //Singleton |
81
|
|
|
keyManagerOnce sync.Once //nolint:gochecknoglobals //Singleton |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
func GetKeyManagerInstance() *KeyManager { |
85
|
|
|
keyManagerOnce.Do(func() { |
86
|
|
|
keyManagerInstance = &KeyManager{} |
87
|
|
|
}) |
88
|
|
|
return keyManagerInstance |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func (k *KeyManager) GetPrivateKey() ed25519.PrivateKey { |
92
|
|
|
return k.privateKey |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
func (k *KeyManager) GetPublicKey() ed25519.PublicKey { |
96
|
|
|
return k.publicKey |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
func (k *KeyManager) ParseEd25519Key() error { |
100
|
|
|
publicKey, privateKey, err := crypto.GenerateKeyPair() |
101
|
|
|
if err != nil { |
102
|
|
|
return err |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
k.privateKey = privateKey |
106
|
|
|
k.publicKey = publicKey |
107
|
|
|
|
108
|
|
|
log.Info("✅ Created ed25519 keys") |
109
|
|
|
|
110
|
|
|
return nil |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
func GetConfigPath(isDevelopment bool) string { |
114
|
|
|
if isDevelopment { |
115
|
|
|
return "./config/config-local" |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return "./config/config-prod" |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
func IsProduction() bool { |
122
|
|
|
return os.Getenv("APP_ENV") != "dev" |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
func IsDevelopment() bool { |
126
|
|
|
return os.Getenv("APP_ENV") == "dev" |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
func GetCallbackURL() string { |
130
|
|
|
return os.Getenv("CALLBACK_URL") |
131
|
|
|
} |
132
|
|
|
|