@@ -4,16 +4,20 @@ import (
4
4
"api.jwt.auth/core/redis"
5
5
"api.jwt.auth/services/models"
6
6
"api.jwt.auth/settings"
7
+ "bufio"
7
8
"code.google.com/p/go-uuid/uuid"
9
+ "crypto/rsa"
10
+ "crypto/x509"
11
+ "encoding/pem"
8
12
jwt "github.com/dgrijalva/jwt-go"
9
13
"golang.org/x/crypto/bcrypt"
10
- "io/ioutil "
14
+ "os "
11
15
"time"
12
16
)
13
17
14
18
type JWTAuthenticationBackend struct {
15
- privateKey [] byte
16
- PublicKey [] byte
19
+ privateKey * rsa. PrivateKey
20
+ PublicKey * rsa. PublicKey
17
21
}
18
22
19
23
const (
@@ -86,20 +90,60 @@ func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
86
90
return true
87
91
}
88
92
89
- func getPrivateKey () [] byte {
90
- privateKey , err := ioutil . ReadFile (settings .Get ().PrivateKeyPath )
93
+ func getPrivateKey () * rsa. PrivateKey {
94
+ privateKeyFile , err := os . Open (settings .Get ().PrivateKeyPath )
91
95
if err != nil {
92
96
panic (err )
93
97
}
94
98
95
- return privateKey
99
+ pemfileinfo , _ := privateKeyFile .Stat ()
100
+ var size int64 = pemfileinfo .Size ()
101
+ pembytes := make ([]byte , size )
102
+
103
+ buffer := bufio .NewReader (privateKeyFile )
104
+ _ , err = buffer .Read (pembytes )
105
+
106
+ data , _ := pem .Decode ([]byte (pembytes ))
107
+
108
+ privateKeyFile .Close ()
109
+
110
+ privateKeyImported , err := x509 .ParsePKCS1PrivateKey (data .Bytes )
111
+
112
+ if err != nil {
113
+ panic (err )
114
+ }
115
+
116
+ return privateKeyImported
96
117
}
97
118
98
- func getPublicKey () []byte {
99
- publicKey , err := ioutil .ReadFile (settings .Get ().PublicKeyPath )
119
+ func getPublicKey () * rsa.PublicKey {
120
+ publicKeyFile , err := os .Open (settings .Get ().PublicKeyPath )
121
+ if err != nil {
122
+ panic (err )
123
+ }
124
+
125
+ pemfileinfo , _ := publicKeyFile .Stat ()
126
+ var size int64 = pemfileinfo .Size ()
127
+ pembytes := make ([]byte , size )
128
+
129
+ buffer := bufio .NewReader (publicKeyFile )
130
+ _ , err = buffer .Read (pembytes )
131
+
132
+ data , _ := pem .Decode ([]byte (pembytes ))
133
+
134
+ publicKeyFile .Close ()
135
+
136
+ publicKeyImported , err := x509 .ParsePKIXPublicKey (data .Bytes )
137
+
100
138
if err != nil {
101
139
panic (err )
102
140
}
103
141
104
- return publicKey
142
+ rsaPub , ok := publicKeyImported .(* rsa.PublicKey )
143
+
144
+ if ! ok {
145
+ panic (err )
146
+ }
147
+
148
+ return rsaPub
105
149
}
0 commit comments