-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.go
60 lines (51 loc) · 1.61 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
package gw
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/auth0/go-jwt-middleware"
jwt "github.com/dgrijalva/jwt-go"
"github.com/juju/errors"
"io/ioutil"
"net/http"
)
func SetupJWTMiddleware(private, public string) (*jwtmiddleware.JWTMiddleware, *rsa.PrivateKey, string, error) {
signBytes, err := ioutil.ReadFile(private)
if err != nil {
return nil, nil, "", errors.Annotatef(err, "Failed to read RSA private key")
}
signKey, err := jwt.ParseRSAPrivateKeyFromPEM(signBytes)
if err != nil {
return nil, nil, "", errors.Annotatef(err, "Failed to parse RSA private key file")
}
verifyBytes, err := ioutil.ReadFile(public)
if err != nil {
return nil, nil, "", errors.Annotatef(err, "Failed to read RSA public key")
}
verifyKey, err := jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
if err != nil {
return nil, nil, "", errors.Annotatef(err, "Failed to parse RSA public key file")
}
jm := jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return verifyKey, nil
},
SigningMethod: jwt.SigningMethodRS512,
CredentialsOptional: false,
EnableAuthOnOptions: true,
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err string) {
// Don't have middleware do anything with error other than return
// it as it occurs
return
},
})
pubDER, err := x509.MarshalPKIXPublicKey(verifyKey)
if err != nil {
return nil, nil, "", errors.Annotatef(err, "PEMifying public key failed")
}
pubBytes := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: pubDER,
})
return jm, signKey, string(pubBytes), nil
}