Skip to content

Commit f456c93

Browse files
author
raulgzm
committed
RSA Get Keys methods returns a rsa.PrivateKey and a rsa.PublicKey structs respectively
1 parent 98367ab commit f456c93

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

core/authentication/jwt_backend.go

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ import (
44
"api.jwt.auth/core/redis"
55
"api.jwt.auth/services/models"
66
"api.jwt.auth/settings"
7+
"bufio"
78
"code.google.com/p/go-uuid/uuid"
9+
"crypto/rsa"
10+
"crypto/x509"
11+
"encoding/pem"
812
jwt "github.com/dgrijalva/jwt-go"
913
"golang.org/x/crypto/bcrypt"
10-
"io/ioutil"
14+
"os"
1115
"time"
1216
)
1317

1418
type JWTAuthenticationBackend struct {
15-
privateKey []byte
16-
PublicKey []byte
19+
privateKey *rsa.PrivateKey
20+
PublicKey *rsa.PublicKey
1721
}
1822

1923
const (
@@ -86,20 +90,60 @@ func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
8690
return true
8791
}
8892

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)
9195
if err != nil {
9296
panic(err)
9397
}
9498

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
96117
}
97118

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+
100138
if err != nil {
101139
panic(err)
102140
}
103141

104-
return publicKey
142+
rsaPub, ok := publicKeyImported.(*rsa.PublicKey)
143+
144+
if !ok {
145+
panic(err)
146+
}
147+
148+
return rsaPub
105149
}

core/authentication/middlewares.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ func RequireTokenAuthentication(rw http.ResponseWriter, req *http.Request, next
1111

1212
token, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
1313
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
14-
fmt.Println("Unexpected signing method")
1514
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
1615
} else {
1716
return authBackend.PublicKey, nil

0 commit comments

Comments
 (0)