-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
131 lines (119 loc) · 3.24 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package api
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/dgrijalva/jwt-go"
"os"
"strings"
)
const (
tokenHeaderPrefix = "Bearer "
)
var (
hmacKeyPath = "conf/hmac_key"
rsaPublicKeyPath = "conf/public_key.pub"
es256PublicKeyPath = "conf/ec256-public.pem"
es384PublicKeyPath = "conf/ec384-public.pem"
es512PublicKeyPath = "conf/ec512-public.pem"
)
type algError struct {
alg string
}
func (algError *algError) Error() string {
return algError.alg + " is not support."
}
func RsaApi(c context.Context, ctx *app.RequestContext) {
token := ctx.Request.Header.Get(authHeader)
if token == "" || !strings.HasPrefix(token, tokenHeaderPrefix) {
unauthorized(ctx, "authorization not found.")
return
}
token = strings.TrimPrefix(token, tokenHeaderPrefix)
claims, err := jwtParse(c, ctx, token, func(token *jwt.Token) (interface{}, error) {
alg := token.Header["alg"].(string)
if alg != "RS256" && alg != "RS384" && alg != "RS512" {
return nil, &algError{alg: alg}
}
pubFile, _ := os.ReadFile(rsaPublicKeyPath)
pubKey, _ := jwt.ParseRSAPublicKeyFromPEM(pubFile)
return pubKey, nil
})
if err != nil {
unauthorized(ctx, err.Error())
return
}
key := claims["key"]
if key != "api-mock-rsa" {
unauthorized(ctx, "validate authorization failed.")
return
}
success(ctx)
}
func HmacApi(c context.Context, ctx *app.RequestContext) {
token := ctx.Request.Header.Get(authHeader)
if token == "" || !strings.HasPrefix(token, tokenHeaderPrefix) {
unauthorized(ctx, "authorization not found.")
return
}
token = strings.TrimPrefix(token, tokenHeaderPrefix)
claims, err := jwtParse(c, ctx, token, func(token *jwt.Token) (interface{}, error) {
alg := token.Header["alg"].(string)
if alg != "HS256" && alg != "HS384" && alg != "HS512" {
return nil, &algError{alg: alg}
}
hmacKey, _ := os.ReadFile(hmacKeyPath)
return hmacKey, nil
})
if err != nil {
return
}
key := claims["key"]
if key != "api-mock-hmac" {
unauthorized(ctx, "validate authorization failed.")
return
}
success(ctx)
}
func EcdsaApi(c context.Context, ctx *app.RequestContext) {
token := ctx.Request.Header.Get(authHeader)
if token == "" || !strings.HasPrefix(token, tokenHeaderPrefix) {
unauthorized(ctx, "authorization not found.")
return
}
token = strings.TrimPrefix(token, tokenHeaderPrefix)
claims, err := jwtParse(c, ctx, token, func(token *jwt.Token) (interface{}, error) {
alg := token.Header["alg"].(string)
var key []byte
if alg == "ES256" {
key, _ = os.ReadFile(es256PublicKeyPath)
} else if alg == "ES384" {
key, _ = os.ReadFile(es384PublicKeyPath)
} else if alg == "ES512" {
key, _ = os.ReadFile(es512PublicKeyPath)
} else {
return nil, &algError{alg: alg}
}
pub, err := jwt.ParseECPublicKeyFromPEM(key)
if err != nil {
return nil, err
}
return pub, nil
})
if err != nil {
return
}
key := claims["key"]
if key != "api-mock-ecdsa" {
unauthorized(ctx, "validate authorization failed.")
return
}
success(ctx)
}
func jwtParse(c context.Context, ctx *app.RequestContext, token string, keyfunc jwt.Keyfunc) (jwt.MapClaims, error) {
parse, err := jwt.Parse(token, keyfunc)
if err != nil {
unauthorized(ctx, err.Error())
return nil, err
}
return parse.Claims.(jwt.MapClaims), nil
}