-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
104 lines (93 loc) · 2.91 KB
/
models.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
package traefik_magic_jwt
import (
"crypto"
"crypto/hmac"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"net/http"
)
type JwtHeader struct {
Alg string `json:"alg"`
Kid string `json:"kid"`
Typ string `json:"typ"`
Cty string `json:"cty"`
Crit []string `json:"crit"`
}
type tokenPayLoad struct {
Iat json.Number `json:"iat"`
Exp json.Number `json:"exp"`
}
type JWT struct {
Plaintext []byte
Signature []byte
Header JwtHeader
Payload tokenPayLoad
RawPayload []byte
}
var supportedHeaderNames = map[string]struct{}{"alg": {}, "kid": {}, "typ": {}, "cty": {}, "crit": {}}
type tokenVerifyFunction func(key interface{}, hash crypto.Hash, payload []byte, signature []byte) error
type tokenVerifyAsymmetricFunction func(key interface{}, hash crypto.Hash, digest []byte, signature []byte) error
// jwtAlgorithm describes a JWS 'alg' value
type tokenAlgorithm struct {
hash crypto.Hash
verify tokenVerifyFunction
}
// tokenAlgorithms is the known JWT algorithms
var (
tokenAlgorithms = map[string]tokenAlgorithm{
"RS256": {crypto.SHA256, verifyAsymmetric(verifyRSAPKCS)},
"HS256": {crypto.SHA256, verifyHMAC},
}
noTokenError = &RequestError{StatusCode: http.StatusUnauthorized, Message: "No Token Detect"}
badTokenError = &RequestError{StatusCode: http.StatusBadRequest, Message: "Invalid Token"}
verifyTokenError = &RequestError{StatusCode: http.StatusBadRequest, Message: "Verify Error"}
expiredTokenError = &RequestError{StatusCode: http.StatusUnavailableForLegalReasons, Message: "Expired Token"}
)
func verifyHMAC(key interface{}, hash crypto.Hash, payload []byte, signature []byte) error {
macKey, ok := key.([]byte)
if !ok {
return fmt.Errorf("incorrect symmetric key type")
}
mac := hmac.New(hash.New, macKey)
if _, err := mac.Write([]byte(payload)); err != nil {
return err
}
if !hmac.Equal(signature, mac.Sum([]byte{})) {
return errors.New("signature not verified")
}
return nil
}
func verifyAsymmetric(verify tokenVerifyAsymmetricFunction) tokenVerifyFunction {
return func(key interface{}, hash crypto.Hash, payload []byte, signature []byte) error {
h := hash.New()
_, err := h.Write(payload)
if err != nil {
return err
}
return verify(key, hash, h.Sum([]byte{}), signature)
}
}
func verifyRSAPKCS(key interface{}, hash crypto.Hash, digest []byte, signature []byte) error {
publicKeyRsa := key.(*rsa.PublicKey)
if err := rsa.VerifyPKCS1v15(publicKeyRsa, hash, digest, signature); err != nil {
return fmt.Errorf("token verification failed (RSAPKCS)")
}
return nil
}
type WhiteUrl struct {
URL string `json:"url"`
Method string `json:"method"`
Type string `json:"type,omitempty"`
}
type RequestError struct {
StatusCode int
Message string
}
func httpError(w http.ResponseWriter, e string, code int) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
fmt.Fprint(w, e)
}