-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathjwtmanager.go
265 lines (230 loc) · 6.92 KB
/
jwtmanager.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Copyright 2020 The Vouch Proxy Authors.
Use of this source code is governed by The MIT License (MIT) that
can be found in the LICENSE file. Software distributed under The
MIT License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied.
*/
package jwtmanager
import (
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
jwt "github.com/golang-jwt/jwt"
"go.uber.org/zap"
"github.com/vouch/vouch-proxy/pkg/cfg"
"github.com/vouch/vouch-proxy/pkg/cookie"
"github.com/vouch/vouch-proxy/pkg/structs"
)
const comma = ","
// VouchClaims jwt Claims specific to vouch
type VouchClaims struct {
Username string `json:"username"`
CustomClaims map[string]interface{}
PAccessToken string
PIdToken string
jwt.StandardClaims
}
// StandardClaims jwt.StandardClaims implementation
var StandardClaims jwt.StandardClaims
var logger *zap.Logger
var log *zap.SugaredLogger
var aud string
// Configure see main.go configure()
func Configure() {
log = cfg.Logging.Logger
logger = cfg.Logging.FastLogger
cacheConfigure()
aud = audience()
StandardClaims = jwt.StandardClaims{
Issuer: cfg.Cfg.JWT.Issuer,
Audience: aud,
}
}
// `aud` of the issued JWT https://tools.ietf.org/html/rfc7519#section-4.1.3
func audience() string {
aud := make([]string, 0)
// TODO: the Sites that end up in the JWT come from here
// if we add fine grain ability (ACL?) to the equation
// then we're going to have to add something fancier here
for i := 0; i < len(cfg.Cfg.Domains); i++ {
aud = append(aud, cfg.Cfg.Domains[i])
}
if cfg.Cfg.Cookie.Domain != "" {
aud = append(aud, cfg.Cfg.Cookie.Domain)
}
return strings.Join(aud, comma)
}
// NewVPJWT issue a signed Vouch Proxy JWT for a user
func NewVPJWT(u structs.User, customClaims structs.CustomClaims, ptokens structs.PTokens) (string, error) {
// User`token`
// u.PrepareUserData()
claims := VouchClaims{
u.Username,
customClaims.Claims,
ptokens.PAccessToken,
ptokens.PIdToken,
StandardClaims,
}
claims.Audience = aud
claims.ExpiresAt = time.Now().Add(time.Minute * time.Duration(cfg.Cfg.JWT.MaxAge)).Unix()
// https://github.com/vouch/vouch-proxy/issues/287
if cfg.Cfg.Headers.AccessToken == "" {
claims.PAccessToken = ""
}
if cfg.Cfg.Headers.IDToken == "" {
claims.PIdToken = ""
}
// https://godoc.org/github.com/golang-jwt/jwt#NewWithClaims
token := jwt.NewWithClaims(jwt.GetSigningMethod(cfg.Cfg.JWT.SigningMethod), claims)
// log.Debugf("token: %v", token)
log.Debugf("token created, expires: %d diff from now: %d", claims.StandardClaims.ExpiresAt, claims.StandardClaims.ExpiresAt-time.Now().Unix())
key, err := cfg.SigningKey()
if err != nil {
log.Errorf("%s", err)
}
ss, err := token.SignedString(key)
if ss == "" || err != nil {
return "", fmt.Errorf("New JWT: signed token error: %s", err)
}
if cfg.Cfg.JWT.Compress {
ss, err = compressAndEncodeTokenString(ss)
if ss == "" || err != nil {
return "", fmt.Errorf("New JWT: compressed token error: %w", err)
}
}
return ss, nil
}
// SiteInToken searches does the token contain the site?
func SiteInToken(site string, token *jwt.Token) bool {
if claims, ok := token.Claims.(*VouchClaims); ok {
log.Debugf("site %s claim %v", site, claims)
if claims.SiteInAudience(site) {
return true
}
}
log.Errorf("site %s not found in token audience", site)
return false
}
// ParseTokenString converts signed token to jwt struct
func ParseTokenString(tokenString string) (*jwt.Token, error) {
log.Debugf("tokenString length: %d", len(tokenString))
if cfg.Cfg.JWT.Compress {
tokenString = decodeAndDecompressTokenString(tokenString)
log.Debugf("decompressed tokenString length %d", len(tokenString))
}
key, err := cfg.DecryptionKey()
if err != nil {
log.Errorf("%s", err)
}
return jwt.ParseWithClaims(tokenString, &VouchClaims{}, func(token *jwt.Token) (interface{}, error) {
// return jwt.ParseWithClaims(tokenString, &VouchClaims{}, func(token *jwt.Token) (interface{}, error) {
if token.Method != jwt.GetSigningMethod(cfg.Cfg.JWT.SigningMethod) {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return key, nil
})
}
// SiteInAudience does the claim contain the value?
func (claims *VouchClaims) SiteInAudience(site string) bool {
for _, s := range strings.Split(aud, comma) {
if strings.Contains(site, s) {
log.Debugf("site %s is found for claims.Audience %s", site, s)
return true
}
}
return false
}
// PTokenClaims get all the claims
func PTokenClaims(ptoken *jwt.Token) (*VouchClaims, error) {
ptokenClaims, ok := ptoken.Claims.(*VouchClaims)
if !ok {
log.Debugf("failed claims: %v %v", ptokenClaims, ptoken.Claims)
return ptokenClaims, errors.New("cannot parse claims")
}
log.Debugf("*ptokenCLaims: %v", *ptokenClaims)
return ptokenClaims, nil
}
func decodeAndDecompressTokenString(encgzipss string) string {
var gzipss []byte
// gzipss, err := url.QueryUnescape(encgzipss)
gzipss, err := base64.URLEncoding.DecodeString(encgzipss)
if err != nil {
log.Debugf("Error in Base64decode: %v", err)
}
breader := bytes.NewReader(gzipss)
zr, err := gzip.NewReader(breader)
if err != nil {
log.Debugf("Error reading gzip data: %v", err)
return ""
}
if err := zr.Close(); err != nil {
log.Debugf("Error decoding token: %v", err)
}
ss, _ := ioutil.ReadAll(zr)
return string(ss)
}
func compressAndEncodeTokenString(ss string) (string, error) {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
if _, err := zw.Write([]byte(ss)); err != nil {
return "", err
}
if err := zw.Close(); err != nil {
return "", err
}
ret := base64.URLEncoding.EncodeToString(buf.Bytes())
// ret := url.QueryEscape(buf.String())
log.Debugf("token compressed: was %d bytes, now %d", len(ss), len(ret))
return ret, nil
}
// FindJWT look for JWT in Cookie, JWT Header, Authorization Header (OAuth2 Bearer Token)
// and Query String in that order
func FindJWT(r *http.Request) string {
jwt, err := cookie.Cookie(r)
if err == nil {
logger.Debug("jwt found in cookie")
return jwt
}
jwt = r.Header.Get(cfg.Cfg.Headers.JWT)
if jwt != "" {
log.Debugf("jwt from header %s: %s", cfg.Cfg.Headers.JWT, jwt)
return jwt
}
auth := r.Header.Get("Authorization")
if auth != "" {
s := strings.SplitN(auth, " ", 2)
if len(s) == 2 {
jwt = s[1]
log.Debugf("jwt from authorization header: %s", jwt)
return jwt
}
}
jwt = r.URL.Query().Get(cfg.Cfg.Headers.QueryString)
if jwt != "" {
log.Debugf("jwt from querystring %s: %s", cfg.Cfg.Headers.QueryString, jwt)
return jwt
}
return ""
}
// ClaimsFromJWT parse the jwt and return the claims
func ClaimsFromJWT(jwt string) (*VouchClaims, error) {
var claims *VouchClaims
jwtParsed, err := ParseTokenString(jwt)
if err != nil {
return nil, err
}
claims, err = PTokenClaims(jwtParsed)
if err != nil {
// claims = jwtmanager.PTokenClaims(jwtParsed)
// if claims == &jwtmanager.VouchClaims{} {
return nil, err
}
return claims, nil
}