/
jwt.go
167 lines (144 loc) · 4.46 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
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
// Copyright (c) 2022 IndyKite
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jwt
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
"golang.org/x/oauth2"
"github.com/indykite/indykite-sdk-go/grpc/config"
)
type (
jwtAccessTokenSource struct {
template jwt.Token
signer jwk.Key
tokenLifetime time.Duration
}
)
func CreateTokenSourceFromPrivateKey(privateKeyJWK any, clientID string) (oauth2.TokenSource, error) {
privateKeyJWKBytes, err := interfaceToBytes(privateKeyJWK)
if err != nil {
return nil, err
}
return JWTokenSource(privateKeyJWKBytes, false, clientID, 0)
}
func CreateTokenSource(credentials *config.CredentialsConfig) (oauth2.TokenSource, error) {
var clientID string
switch {
case credentials.AppAgentID != "":
clientID = credentials.AppAgentID
case credentials.ServiceAccountID != "":
clientID = credentials.ServiceAccountID
default:
return nil, errors.New("missing client ID, AppAgentID or ServiceAccountID must be specified")
}
var (
tokenLifetime time.Duration
err error
)
if credentials.TokenLifetime != "" {
tokenLifetime, err = time.ParseDuration(credentials.TokenLifetime)
if err != nil {
return nil, fmt.Errorf("unable to parse 'tokenLifetime': %w", err)
}
}
switch {
case credentials.PrivateKeyJWK != nil:
return JWTokenSource(credentials.PrivateKeyJWK, false, clientID, tokenLifetime)
case credentials.PrivateKeyPKCS8Base64 != "":
var raw []byte
raw, err = base64.StdEncoding.DecodeString(credentials.PrivateKeyPKCS8Base64)
if err != nil {
return nil, err
}
return JWTokenSource(raw, true, clientID, tokenLifetime)
case credentials.PrivateKeyPKCS8 != "":
return JWTokenSource([]byte(credentials.PrivateKeyPKCS8), true, clientID, tokenLifetime)
default:
return nil, errors.New("unable to find secret credential")
}
}
func interfaceToBytes(privateKeyJWK any) ([]byte, error) {
if stringValue, ok := privateKeyJWK.(string); ok {
return []byte(stringValue), nil
}
privateKeyJWKBytes, err := json.Marshal(privateKeyJWK)
if err != nil {
return nil, err
}
return privateKeyJWKBytes, nil
}
func parseKey(secretKey []byte, pem bool) (jwk.Key, error) {
if pem {
return jwk.ParseKey(secretKey, jwk.WithPEM(pem))
}
if secretKey[0] != '"' {
return jwk.ParseKey(secretKey)
}
var raw string
err := json.Unmarshal(secretKey, &raw)
if err != nil {
return nil, err
}
return jwk.ParseKey([]byte(raw))
}
func JWTokenSource(secretKey []byte, pem bool, clientID string,
tokenLifetime time.Duration) (oauth2.TokenSource, error) {
key, err := parseKey(secretKey, pem)
if err != nil {
return nil, err
}
// Remove user defined kid and generate new one same way as we do on BE
// This is micro-optimization on client-side.
_ = key.Remove(jwk.KeyIDKey)
err = jwk.AssignKeyID(key)
if err != nil {
return nil, errors.New("failed to assign kid for public key")
}
t := jwt.New()
_ = t.Set(jwt.IssuerKey, clientID)
_ = t.Set(jwt.SubjectKey, clientID)
// Don't let it be smaller than 2 min because token will be issued in every 1 minute by default.
if tokenLifetime < 2*time.Minute || tokenLifetime > 24*time.Hour {
tokenLifetime = time.Hour
}
ts := &jwtAccessTokenSource{
template: t,
signer: key,
tokenLifetime: tokenLifetime,
}
return oauth2.ReuseTokenSource(nil, ts), nil
}
func (ts *jwtAccessTokenSource) Token() (*oauth2.Token, error) {
iat := time.Now()
exp := iat.Add(ts.tokenLifetime)
token, err := ts.template.Clone()
if err != nil {
return nil, err
}
_ = token.Set(jwt.IssuedAtKey, iat)
_ = token.Set(jwt.ExpirationKey, exp)
_ = token.Set(jwt.JwtIDKey, uuid.New().String())
signed, err := jwt.Sign(token, jwt.WithKey(ts.signer.Algorithm(), ts.signer))
if err != nil {
return nil, err
}
// Exp shell not be less than 2 min!
return &oauth2.Token{TokenType: "Bearer", Expiry: exp.Add(-time.Minute), AccessToken: string(signed)}, nil
}