This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
/
jwt_encoding.go
246 lines (228 loc) · 7.07 KB
/
jwt_encoding.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
// Copyright 2022 Google LLC
//
// 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/binary"
"fmt"
"strings"
spb "google.golang.org/protobuf/types/known/structpb"
tpb "github.com/google/tink/go/proto/tink_go_proto"
)
// keyID returns the keyID in big endian format base64 encoded if the key output prefix is of type Tink or nil otherwise.
func keyID(keyID uint32, outPrefixType tpb.OutputPrefixType) *string {
if outPrefixType != tpb.OutputPrefixType_TINK {
return nil
}
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, keyID)
s := base64Encode(buf)
return &s
}
// createUnsigned creates an unsigned JWT by created the header/payload, encoding them to a websafe base64 encoded string and concatenating.
func createUnsigned(rawJWT *RawJWT, algo string, tinkKID *string, customKID *string) (string, error) {
if rawJWT == nil {
return "", fmt.Errorf("rawJWT is nil")
}
var typeHeader *string = nil
if rawJWT.HasTypeHeader() {
th, err := rawJWT.TypeHeader()
if err != nil {
return "", err
}
typeHeader = &th
}
if customKID != nil && tinkKID != nil {
return "", fmt.Errorf("TINK Keys are not allowed to have a kid value set")
}
if tinkKID != nil {
customKID = tinkKID
}
encodedHeader, err := createHeader(algo, typeHeader, customKID)
if err != nil {
return "", err
}
payload, err := rawJWT.JSONPayload()
if err != nil {
return "", err
}
return dotConcat(encodedHeader, base64Encode(payload)), nil
}
// combineUnsignedAndSignature combines the token with the raw signature to provide a signed token.
func combineUnsignedAndSignature(unsigned string, signature []byte) string {
return dotConcat(unsigned, base64Encode(signature))
}
// splitSignedCompact extracts the witness and usigned JWT.
func splitSignedCompact(compact string) ([]byte, string, error) {
i := strings.LastIndex(compact, ".")
if i < 0 {
return nil, "", fmt.Errorf("invalid token")
}
witness, err := base64Decode(compact[i+1:])
if err != nil {
return nil, "", fmt.Errorf("%q: %v", compact[i+1:], err)
}
if len(witness) == 0 {
return nil, "", fmt.Errorf("empty signature")
}
unsigned := compact[0:i]
if len(unsigned) == 0 {
return nil, "", fmt.Errorf("empty content")
}
if strings.Count(unsigned, ".") != 1 {
return nil, "", fmt.Errorf("only tokens in JWS compact serialization formats are supported")
}
return witness, unsigned, nil
}
// decodeUnsignedTokenAndValidateHeader verifies the header on an unsigned JWT and decodes the payload into a RawJWT.
// Expects the token to be in compact serialization format. The signature should be verified before calling this function.
func decodeUnsignedTokenAndValidateHeader(unsigned, algorithm string, tinkKID, customKID *string) (*RawJWT, error) {
parts := strings.Split(unsigned, ".")
if len(parts) != 2 {
return nil, fmt.Errorf("only tokens in JWS compact serialization formats are supported")
}
jsonHeader, err := base64Decode(parts[0])
if err != nil {
return nil, err
}
header, err := jsonToStruct(jsonHeader)
if err != nil {
return nil, err
}
if err := validateHeader(header, algorithm, tinkKID, customKID); err != nil {
return nil, err
}
typeHeader, err := extractTypeHeader(header)
if err != nil {
return nil, err
}
jsonPayload, err := base64Decode(parts[1])
if err != nil {
return nil, err
}
return NewRawJWTFromJSON(typeHeader, jsonPayload)
}
// base64Encode encodes a byte array into a base64 URL safe string with no padding.
func base64Encode(content []byte) string {
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(content)
}
// base64Decode decodes a URL safe base64 encoded string into a byte array ignoring padding.
func base64Decode(content string) ([]byte, error) {
for _, c := range content {
if !isValidURLsafeBase64Char(c) {
return nil, fmt.Errorf("invalid encoding")
}
}
return base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(content)
}
func isValidURLsafeBase64Char(c rune) bool {
return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9')) || ((c == '-') || (c == '_')))
}
func dotConcat(a, b string) string {
return fmt.Sprintf("%s.%s", a, b)
}
func jsonToStruct(jsonPayload []byte) (*spb.Struct, error) {
payload := &spb.Struct{}
if err := payload.UnmarshalJSON(jsonPayload); err != nil {
return nil, err
}
return payload, nil
}
func extractTypeHeader(header *spb.Struct) (*string, error) {
fields := header.GetFields()
if fields == nil {
return nil, fmt.Errorf("header contains no fields")
}
val, ok := fields["typ"]
if !ok {
return nil, nil
}
str, ok := val.Kind.(*spb.Value_StringValue)
if !ok {
return nil, fmt.Errorf("type header isn't a string")
}
return &str.StringValue, nil
}
func createHeader(algorithm string, typeHeader, kid *string) (string, error) {
header := &spb.Struct{
Fields: map[string]*spb.Value{
"alg": spb.NewStringValue(algorithm),
},
}
if typeHeader != nil {
header.Fields["typ"] = spb.NewStringValue(*typeHeader)
}
if kid != nil {
header.Fields["kid"] = spb.NewStringValue(*kid)
}
jsonHeader, err := header.MarshalJSON()
if err != nil {
return "", err
}
return base64Encode(jsonHeader), nil
}
func validateHeader(header *spb.Struct, algorithm string, tinkKID, customKID *string) error {
fields := header.GetFields()
if fields == nil {
return fmt.Errorf("header contains no fields")
}
alg, err := headerStringField(fields, "alg")
if err != nil {
return err
}
if alg != algorithm {
return fmt.Errorf("invalid alg")
}
if _, ok := fields["crit"]; ok {
return fmt.Errorf("all tokens with crit headers are rejected")
}
if tinkKID != nil && customKID != nil {
return fmt.Errorf("custom_kid can only be set for RAW keys")
}
_, hasKID := fields["kid"]
if tinkKID != nil && !hasKID {
return fmt.Errorf("missing kid in header")
}
if tinkKID != nil {
return validateKIDInHeader(fields, tinkKID)
}
if hasKID && customKID != nil {
return validateKIDInHeader(fields, customKID)
}
return nil
}
func validateKIDInHeader(fields map[string]*spb.Value, kid *string) error {
headerKID, err := headerStringField(fields, "kid")
if err != nil {
return err
}
if headerKID != *kid {
return fmt.Errorf("invalid kid header")
}
return nil
}
func headerStringField(fields map[string]*spb.Value, name string) (string, error) {
val, ok := fields[name]
if !ok {
return "", fmt.Errorf("header is missing %q", name)
}
str, ok := val.Kind.(*spb.Value_StringValue)
if !ok {
return "", fmt.Errorf("%q header isn't a string", name)
}
return str.StringValue, nil
}