Passed
Push — main ( 0795ea...57a23e )
by Yume
02:19 queued 01:06
created

config.ParseEd25519PublicKey   A

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nop 0
dl 0
loc 13
rs 9.95
c 0
b 0
f 0
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/env"
9
	"github.com/memnix/memnix-rest/pkg/json"
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
// EnvHelper is the helper for environment variables
17
var EnvHelper = env.NewMyEnv(&env.OsEnv{})
18
19
const (
20
	ExpirationTimeInHours = 24 // ExpirationTimeInHours is the expiration time for the JWT token
21
	SQLMaxOpenConns       = 20 // SQLMaxOpenConns is the max number of connections in the open connection pool
22
	SQLMaxIdleConns       = 5  // SQLMaxIdleConns is the max number of connections in the idle connection pool
23
24
	BCryptCost = 11 // BCryptCost is the cost for bcrypt
25
26
	OauthStateLength   = 16               // OauthStateLength is the length of the state for oauth
27
	OauthStateDuration = 10 * time.Minute // OauthStateDuration is the duration for the state for oauth
28
29
	RedisMinIdleConns      = 200               // RedisMinIdleConns is the minimum number of idle connections in the pool
30
	RedisPoolSize          = 12000             // RedisPoolSize is the maximum number of connections allocated by the pool at a given time
31
	RedisPoolTimeout       = 240 * time.Second // RedisPoolTimeout is the amount of time a connection can be used before being closed
32
	RedisDefaultExpireTime = 6 * time.Hour     // RedisDefaultExpireTime is the default expiration time for keys
33
	RedisOwnedExpireTime   = 2 * time.Hour     // RedisOwnedExpireTime is the expiration time for owned keys
34
35
	CacheExpireTime = 10 * time.Second // CacheExpireTime is the expiration time for the cache
36
	InfluxDBFreq    = 10 * time.Second // InfluxDBFreq is the frequency for writing to InfluxDB
37
38
	DeckSecretCodeLength = 10  // DeckSecretCodeLength is the length of the secret code for decks
39
	GCThresholdPercent   = 0.7 // GCThresholdPercent is the threshold for garbage collection
40
41
	GCLimit = 1024 * 1024 * 1024 // GCLimit is the limit for garbage collection
42
43
	GormPrometheusRefreshInterval = 15 // GormPrometheusRefreshInterval is the refresh interval for gorm prometheus
44
45
	RistrettoMaxCost     = 5 * MB // RistrettoMaxCost is the maximum cost
46
	RistrettoBufferItems = 32     // RistrettoBufferItems is the number of items per get buffer
47
	RistrettoNumCounters = 1e4    // RistrettoNumCounters is the number of counters
48
)
49
50
// JwtSigningMethod is the signing method for JWT
51
var JwtSigningMethod = jwt.SigningMethodEdDSA
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
// IsProduction returns true if the app is in production
63
func IsProduction() bool {
64
	return EnvHelper.GetEnv("APP_ENV") != "dev"
65
}
66
67
// IsDevelopment returns true if the app is in development
68
func IsDevelopment() bool {
69
	return EnvHelper.GetEnv("APP_ENV") == "dev"
70
}
71
72
var (
73
	ed25519PrivateKey = ed25519.PrivateKey{}
74
	ed25519PublicKey  = ed25519.PublicKey{}
75
)
76
77
// GetEd25519PrivateKey returns the ed25519 private key
78
func GetEd25519PrivateKey() ed25519.PrivateKey {
79
	return ed25519PrivateKey
80
}
81
82
// GetEd25519PublicKey returns the ed25519 public key
83
func GetEd25519PublicKey() ed25519.PublicKey {
84
	return ed25519PublicKey
85
}
86
87
// ParseEd25519PrivateKey parses the ed25519 private key
88
func ParseEd25519PrivateKey() error {
89
	key, err := os.ReadFile("./config/keys/ed25519_private.pem")
90
	if err != nil {
91
		return err
92
	}
93
94
	privateKey, err := jwt.ParseEdPrivateKeyFromPEM(key)
95
	if err != nil {
96
		return err
97
	}
98
99
	ed25519PrivateKey = privateKey.(ed25519.PrivateKey)
100
	return nil
101
}
102
103
// ParseEd25519PublicKey parses the ed25519 public key
104
func ParseEd25519PublicKey() error {
105
	key, err := os.ReadFile("./config/keys/ed25519_public.pem")
106
	if err != nil {
107
		return err
108
	}
109
110
	publicKey, err := jwt.ParseEdPublicKeyFromPEM(key)
111
	if err != nil {
112
		return err
113
	}
114
115
	ed25519PublicKey = publicKey.(ed25519.PublicKey)
116
	return nil
117
}
118