forked from koding/kite
/
kitetest.go
178 lines (145 loc) · 3.48 KB
/
kitetest.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package kitetest
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"os/user"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/koding/kite/protocol"
uuid "github.com/satori/go.uuid"
)
// KeyPair represents PEM encoded RSA key pair.
type KeyPair struct {
Public []byte
Private []byte
}
// KiteKey represents JWT token used in kite authentication,
// also known as kite.key.
type KiteKey struct {
ID string `json:"keyID,omitempty"`
Issuer string `json:"issuer,omitempty"`
Username string `json:"username,omitempty"`
IssuedAt int64 `json:"issuedAt,omitempty"`
KontrolURL string `json:"kontrolURL,omitempty"`
URL string `json:"url,omitempty"`
Kite protocol.Kite `json:"kite,omitempty"`
}
func (k *KiteKey) id() string {
if k.ID != "" {
return k.ID
}
return uuid.Must(uuid.NewV4()).String()
}
func (k *KiteKey) issuer() string {
if k.Issuer != "" {
return k.Issuer
}
return "koding"
}
func (k *KiteKey) username() string {
if k.Username != "" {
return k.Username
}
u, err := user.Current()
if err != nil {
panic(err)
}
return u.Username
}
func (k *KiteKey) issuedAt() int64 {
if k.IssuedAt != 0 {
return k.IssuedAt
}
return time.Now().UTC().Unix()
}
func (k *KiteKey) kontrolURL() string {
if k.KontrolURL != "" {
return k.KontrolURL
}
return "https://koding.com/kontrol/kite"
}
// GenerateKeyPair
func GenerateKeyPair() (*KeyPair, error) {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
pub, err := x509.MarshalPKIXPublicKey(&priv.PublicKey)
if err != nil {
return nil, err
}
return &KeyPair{
Private: pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
}),
Public: pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: pub,
}),
}, nil
}
// GenerateKiteKey
func GenerateKiteKey(k *KiteKey, keys *KeyPair) (*jwt.Token, error) {
var err error
if keys == nil {
keys, err = GenerateKeyPair()
if err != nil {
return nil, err
}
}
kiteKey := jwt.New(jwt.GetSigningMethod("RS256"))
kiteKey.Claims = jwt.MapClaims{
"iss": k.issuer(),
"sub": k.username(),
"iat": k.issuedAt(),
"jti": k.id(),
"kontrolURL": k.kontrolURL(),
"kontrolKey": string(keys.Public),
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(keys.Private)
if err != nil {
return nil, err
}
kiteKey.Raw, err = kiteKey.SignedString(privateKey)
if err != nil {
return nil, err
}
kiteKey.Valid = true
return kiteKey, nil
}
// TokenExtractor is used to extract kite ID from the given JWT token.
type TokenExtractor struct {
Token *jwt.Token
KiteID string
}
var errTokenExtractor = errors.New("extraction ok")
// Extract is a jwt.Keyfunc that extracts kite ID.
func (te *TokenExtractor) Extract(tok *jwt.Token) (interface{}, error) {
var id string
var ok bool
switch claims := tok.Claims.(type) {
case *jwt.StandardClaims:
id, ok = claims.Id, true
case jwt.MapClaims:
id, ok = claims["jti"].(string)
}
if !ok || id == "" {
return nil, errors.New("no kite ID")
}
te.KiteID = id
te.Token = tok
return nil, errTokenExtractor
}
// ExtractKiteID extracts kite ID from the raw JWT token.
func ExtractKiteID(token string) (string, error) {
te := &TokenExtractor{}
_, err := jwt.Parse(token, te.Extract)
if e, ok := err.(*jwt.ValidationError); ok && e.Error() == errTokenExtractor.Error() {
return te.KiteID, nil
}
return "", err
}