Skip to content

Commit 5761fcd

Browse files
author
raul-brainattica
committed
Singleton
1 parent 95744b4 commit 5761fcd

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

core/authentication/jwt_backend.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ const (
2121
expireOffset = 3600
2222
)
2323

24+
var authBackendInstance *JWTAuthenticationBackend = nil
25+
2426
func InitJWTAuthenticationBackend() *JWTAuthenticationBackend {
25-
authBack := new(JWTAuthenticationBackend)
26-
privateKeyPath, _ := filepath.Abs("./core/authentication/keys/private_key")
27-
publicKeyPath, _ := filepath.Abs("./core/authentication/keys/public_key.pub")
28-
authBack.privateKey, _ = ioutil.ReadFile(privateKeyPath)
29-
authBack.PublicKey, _ = ioutil.ReadFile(publicKeyPath)
27+
if authBackendInstance == nil {
28+
authBackendInstance = &JWTAuthenticationBackend{
29+
privateKey: getPrivateKey(),
30+
PublicKey: getPublicKey(),
31+
}
32+
}
3033

31-
return authBack
34+
return authBackendInstance
3235
}
3336

3437
func (backend *JWTAuthenticationBackend) GenerateToken(user *models.User) string {
@@ -78,3 +81,23 @@ func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
7881

7982
return true
8083
}
84+
85+
func getPrivateKey() []byte {
86+
privateKeyPath, _ := filepath.Abs("./core/authentication/keys/private_key")
87+
privateKey, err := ioutil.ReadFile(privateKeyPath)
88+
if err != nil {
89+
panic(err)
90+
}
91+
92+
return privateKey
93+
}
94+
95+
func getPublicKey() []byte {
96+
publicKeyPath, _ := filepath.Abs("./core/authentication/keys/public_key.pub")
97+
publicKey, err := ioutil.ReadFile(publicKeyPath)
98+
if err != nil {
99+
panic(err)
100+
}
101+
102+
return publicKey
103+
}

0 commit comments

Comments
 (0)