-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
163 lines (135 loc) · 2.94 KB
/
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package mock
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"io/ioutil"
"math/big"
"strings"
"time"
)
type Token struct {
KeyID string
TeamID string
IssuedAt int64
}
func (t *Token) Expired() bool {
return time.Now().Unix() > t.IssuedAt+3600
}
type header struct {
Algorithm string `json:"alg"`
Type string `json:"typ"`
KeyID string `json:"kid"`
}
type payload struct {
Issuer string `json:"iss"`
IssuedAt int64 `json:"iat"`
}
func DecodeToken(bearer string) (*Token, error) {
parts := strings.Split(bearer, ".")
if len(parts) != 3 {
return nil, errors.New("encoded jwt token must have three parts")
}
hs, err := base64.RawURLEncoding.DecodeString(parts[0])
if err != nil {
return nil, err
}
var h header
err = json.Unmarshal(hs, &h)
if err != nil {
return nil, err
}
if h.Algorithm != "ES256" {
return nil, errors.New("jwt alg not ES256")
}
if h.KeyID == "" {
return nil, errors.New("jwt kid empty")
}
ps, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
var p payload
err = json.Unmarshal(ps, &p)
if err != nil {
return nil, err
}
if p.Issuer == "" {
return nil, errors.New("jwt iss empty")
}
return &Token{
KeyID: h.KeyID,
TeamID: p.Issuer,
IssuedAt: p.IssuedAt,
}, nil
}
func GenerateAuthKeyPEM() ([]byte, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
der, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return nil, err
}
block := &pem.Block{
Type: "PRIVATE KEY",
Bytes: der,
}
bytes := pem.EncodeToMemory(block)
if bytes == nil {
return nil, errors.New("pem encoding failure")
}
return bytes, nil
}
func AuthKeyFromFile(filename string) (*ecdsa.PrivateKey, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return AuthKeyFromBytes(bytes)
}
func AuthKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error) {
b, _ := pem.Decode(bytes)
if b == nil {
return nil, errors.New("invalid .p8 PEM")
}
p8, err := x509.ParsePKCS8PrivateKey(b.Bytes)
if err != nil {
return nil, err
}
key, ok := p8.(*ecdsa.PrivateKey)
if !ok {
return nil, errors.New("not ECDSA private key")
}
return key, nil
}
func VerifyJWT(token string, pub *ecdsa.PublicKey) (bool, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return false, errors.New("encoded jwt token must have three parts")
}
hash := crypto.SHA256.New()
_, err := hash.Write([]byte(parts[0] + "." + parts[1]))
if err != nil {
return false, err
}
sig, err := base64.RawURLEncoding.DecodeString(parts[2])
if err != nil {
return false, err
}
if len(sig) != 64 {
return false, errors.New("invalid jwt signature size")
}
r := new(big.Int)
r.SetBytes(sig[:32])
s := new(big.Int)
s.SetBytes(sig[32:])
return ecdsa.Verify(pub, hash.Sum(nil), r, s), nil
}