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