-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
43 lines (36 loc) · 945 Bytes
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ctx
import (
"crypto/rsa"
"fmt"
"io/ioutil"
"github.com/dgrijalva/jwt-go"
)
var (
verifyKey *rsa.PublicKey
signKey *rsa.PrivateKey
)
func SignToken(token *jwt.Token) (string, error) {
return token.SignedString(signKey)
}
func LoadSecureKeys(privateKeyPath, publicKeyPath string) (err error) {
bytesSignKey, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
return fmt.Errorf("Error reading private key")
}
signKey, err = jwt.ParseRSAPrivateKeyFromPEM(bytesSignKey)
if err != nil {
return fmt.Errorf("error parsing RSA private key: %s", err)
}
bytesVerifyKey, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return fmt.Errorf("Error reading public key")
}
verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(bytesVerifyKey)
if err != nil {
return fmt.Errorf("error parsing RSA private key: %s", err)
}
if err != nil {
return fmt.Errorf("error parsing RSA public key: %s", err)
}
return nil
}