@@ -21,14 +21,17 @@ const (
21
21
expireOffset = 3600
22
22
)
23
23
24
+ var authBackendInstance * JWTAuthenticationBackend = nil
25
+
24
26
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
+ }
30
33
31
- return authBack
34
+ return authBackendInstance
32
35
}
33
36
34
37
func (backend * JWTAuthenticationBackend ) GenerateToken (user * models.User ) string {
@@ -78,3 +81,23 @@ func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
78
81
79
82
return true
80
83
}
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