-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
48 lines (39 loc) · 888 Bytes
/
jwt.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
44
45
46
47
48
package jwt
import (
"crypto/rsa"
"io/ioutil"
jwtGo "github.com/dgrijalva/jwt-go"
"github.com/naufalfmm/project-iot/resource/config"
)
type (
JWT interface {
Parse(tokenString string, claims jwtGo.Claims) (jwtGo.Claims, error)
Create(claims jwtGo.Claims) (string, error)
}
jwtHelper struct {
verifyKey *rsa.PublicKey
signKey *rsa.PrivateKey
}
)
func New(config *config.EnvConfig) (JWT, error) {
signBytes, err := ioutil.ReadFile(config.JwtPrivateKey)
if err != nil {
return nil, err
}
signKey, err := jwtGo.ParseRSAPrivateKeyFromPEM(signBytes)
if err != nil {
return nil, err
}
verifyBytes, err := ioutil.ReadFile(config.JwtPublicKey)
if err != nil {
return nil, err
}
verifyKey, err := jwtGo.ParseRSAPublicKeyFromPEM(verifyBytes)
if err != nil {
return nil, err
}
return &jwtHelper{
verifyKey: verifyKey,
signKey: signKey,
}, nil
}