1
|
|
|
package config |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"crypto/rand" |
5
|
|
|
"os" |
6
|
|
|
"time" |
7
|
|
|
|
8
|
|
|
"github.com/memnix/memnix-rest/pkg/json" |
9
|
|
|
myJwt "github.com/memnix/memnix-rest/pkg/jwt" |
10
|
|
|
"github.com/pkg/errors" |
11
|
|
|
"github.com/uptrace/opentelemetry-go-extra/otelzap" |
12
|
|
|
"golang.org/x/crypto/ed25519" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
// JSONHelper is the helper for JSON operations |
16
|
|
|
var JSONHelper = json.NewJSON(&json.SonicJSON{}) |
17
|
|
|
|
18
|
|
|
const ( |
19
|
|
|
ExpirationTimeInHours = 24 // ExpirationTimeInHours is the expiration time for the JWT token |
20
|
|
|
SQLMaxOpenConns = 10 // SQLMaxOpenConns is the max number of connections in the open connection pool |
21
|
|
|
SQLMaxIdleConns = 1 // SQLMaxIdleConns is the max number of connections in the idle connection pool |
22
|
|
|
|
23
|
|
|
BCryptCost = 11 // BCryptCost is the cost for bcrypt |
24
|
|
|
|
25
|
|
|
OauthStateLength = 16 // OauthStateLength is the length of the state for oauth |
26
|
|
|
|
27
|
|
|
RedisDefaultExpireTime = 6 * time.Hour // RedisDefaultExpireTime is the default expiration time for keys |
28
|
|
|
|
29
|
|
|
CacheExpireTime = 10 * time.Second // CacheExpireTime is the expiration time for the cache |
30
|
|
|
|
31
|
|
|
GCThresholdPercent = 0.7 // GCThresholdPercent is the threshold for garbage collection |
32
|
|
|
|
33
|
|
|
GCLimit = 1024 * 1024 * 1024 // GCLimit is the limit for garbage collection |
34
|
|
|
|
35
|
|
|
RistrettoMaxCost = 5 * MB // RistrettoMaxCost is the maximum cost |
36
|
|
|
RistrettoBufferItems = 32 // RistrettoBufferItems is the number of items per get buffer |
37
|
|
|
RistrettoNumCounters = 1e4 // RistrettoNumCounters is the number of counters |
38
|
|
|
|
39
|
|
|
MB = 1024 * 1024 // MB is the number of bytes in a megabyte |
40
|
|
|
|
41
|
|
|
MaxPasswordLength = 72 // MaxPasswordLength is the max password length |
42
|
|
|
MinPasswordLength = 8 // MinPasswordLength is the min password length |
43
|
|
|
|
44
|
|
|
SentryFlushTimeout = 2 * time.Second // SentryFlushTimeout is the timeout for flushing sentry |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
var JwtInstance myJwt.Instance |
48
|
|
|
|
49
|
|
|
func GetJwtInstance() myJwt.Instance { |
50
|
|
|
return JwtInstance |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// PasswordConfigStruct is the struct for the password config |
54
|
|
|
type PasswordConfigStruct struct { |
55
|
|
|
Iterations uint32 // Iterations to use for Argon2ID |
56
|
|
|
Memory uint32 // Memory to use for Argon2ID |
57
|
|
|
Threads uint8 // Threads to use for Argon2ID |
58
|
|
|
KeyLen uint32 // KeyLen to use for Argon2ID |
59
|
|
|
SaltLen uint32 // SaltLen to use for Argon2ID |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
var ( |
63
|
|
|
ed25519PrivateKey = ed25519.PrivateKey{} |
64
|
|
|
ed25519PublicKey = ed25519.PublicKey{} |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
func ParseEd25519Key() error { |
68
|
|
|
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader) |
69
|
|
|
if err != nil { |
70
|
|
|
return errors.Wrap(err, "Error generating keys") |
71
|
|
|
} |
72
|
|
|
ed25519PrivateKey = privateKey |
73
|
|
|
|
74
|
|
|
ed25519PublicKey = publicKey |
75
|
|
|
|
76
|
|
|
otelzap.L().Info("✅ Created ed25519 keys") |
77
|
|
|
|
78
|
|
|
return nil |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// GetEd25519PrivateKey returns the ed25519 private key |
82
|
|
|
func GetEd25519PrivateKey() ed25519.PrivateKey { |
83
|
|
|
return ed25519PrivateKey |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// GetEd25519PublicKey returns the ed25519 public key |
87
|
|
|
func GetEd25519PublicKey() ed25519.PublicKey { |
88
|
|
|
return ed25519PublicKey |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func GetConfigPath() string { |
92
|
|
|
if IsDevelopment() { |
93
|
|
|
return "./config/config-local" |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return "./config/config-prod" |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
func IsProduction() bool { |
100
|
|
|
return os.Getenv("APP_ENV") != "dev" |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
func IsDevelopment() bool { |
104
|
|
|
return os.Getenv("APP_ENV") == "dev" |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
func GetCallbackURL() string { |
108
|
|
|
return os.Getenv("CALLBACK_URL") |
109
|
|
|
} |
110
|
|
|
|