This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
builder.go
161 lines (133 loc) · 4.19 KB
/
builder.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
package jwtee
import (
"encoding"
"encoding/base64"
"github.com/pkg/errors"
)
// Builder used to build encoded and signed token.
type Builder interface {
Build(claims encoding.BinaryMarshaler, signer Signer, key Key) (*DecodedParts, error)
}
// TokenBuilder implements Builder.
type TokenBuilder struct {
h Header
}
// NewTokenBuilder returns new instance of TokenBuilder.
func NewTokenBuilder() *TokenBuilder {
return &TokenBuilder{
h: Header{
Typ: "JWT",
},
}
}
// WithKID used to setup the kid (key ID) Header Parameter.
func (b *TokenBuilder) WithKID(kid string) *TokenBuilder {
b.h.Kid = kid
return b
}
// Build used to construct and encode JWT.
func (b *TokenBuilder) Build(claims encoding.BinaryMarshaler, signer Signer, key Key) (*DecodedParts, error) {
// TODO: Possible to reduce allocation if encode parts in same buffer
encodedHeader := b.encodeHeader(signer)
rawClaims, encodedClaims, err := b.encodeClaims(claims)
if err != nil {
return nil, errors.Wrap(err, "failed to encode claims")
}
payload := b.buildPayload(encodedHeader, encodedClaims)
signed, signature, err := b.signPayload(payload, signer, key)
if err != nil {
return nil, errors.Wrap(err, "failed to sign payload")
}
parts := &DecodedParts{
raw: signed,
header: Header{
Typ: "JWT",
Alg: signer.GetAlgorithmID(),
},
claims: rawClaims,
payload: payload,
signature: signature,
}
return parts, nil
}
func (b *TokenBuilder) concatParts(p1, p2, sep []byte) []byte {
buf := make([]byte, len(p1)+len(p2)+1)
copy(buf[:len(p1)], p1)
copy(buf[len(p1):len(p1)+1], sep)
copy(buf[len(p1)+1:], p2)
return buf
}
func (b *TokenBuilder) buildPayload(headers, claims []byte) []byte {
return b.concatParts(headers, claims, []byte{sep})
}
func (b *TokenBuilder) signPayload(payload []byte, signer Signer, key Key) (signed, signature []byte, err error) {
signature, err = signer.Sign(payload, key)
if err != nil {
return nil, nil, err
}
encodedSignature := make([]byte, base64.RawURLEncoding.EncodedLen(len(signature)))
base64.RawURLEncoding.Encode(encodedSignature, signature)
signed = b.concatParts(payload, encodedSignature, []byte{sep})
return signed, signature, nil
}
func (b *TokenBuilder) encodeClaims(claims encoding.BinaryMarshaler) (raw, encoded []byte, err error) {
raw, err = claims.MarshalBinary()
if err != nil {
return nil, nil, err
}
encoded = make([]byte, base64.RawURLEncoding.EncodedLen(len(raw)))
base64.RawURLEncoding.Encode(encoded, raw)
return raw, encoded, nil
}
// nolint: gocyclo
func (b *TokenBuilder) encodeHeader(signer Signer) []byte {
if b.h.Kid != "" && b.h.Typ == "JWT" {
algID := signer.GetAlgorithmID()
algIDLen := len(algID)
kid := b.h.Kid
kidLen := len(kid)
buf := make([]byte, 20+algIDLen+9+kidLen+2)
copy(buf[:20], `{"typ":"JWT","alg":"`)
copy(buf[20:20+algIDLen], algID)
copy(buf[20+algIDLen:20+algIDLen+9], `","kid":"`)
copy(buf[20+algIDLen+9:], kid)
copy(buf[20+algIDLen+9+kidLen:], `"}`)
encoded := make([]byte, base64.RawURLEncoding.EncodedLen(len(buf)))
base64.RawURLEncoding.Encode(encoded, buf)
return encoded
}
switch signer.GetAlgorithmID() {
case HS256:
return []byte("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9")
case HS384:
return []byte("eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9")
case HS512:
return []byte("eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9")
case RS256:
return []byte("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9")
case RS384:
return []byte("eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9")
case RS512:
return []byte("eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9")
case ES256:
return []byte("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9")
case ES384:
return []byte("eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9")
case ES512:
return []byte("eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9")
case PS256:
return []byte("eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9")
case PS384:
return []byte("eyJhbGciOiJQUzM4NCIsInR5cCI6IkpXVCJ9")
default:
algID := signer.GetAlgorithmID()
algIDLen := len(algID)
buf := make([]byte, 20+algIDLen+2)
copy(buf[:20], `{"typ":"JWT","alg":"`)
copy(buf[20:20+algIDLen], algID)
copy(buf[20+algIDLen:], `"}`)
encoded := make([]byte, base64.RawURLEncoding.EncodedLen(len(buf)))
base64.RawURLEncoding.Encode(encoded, buf)
return encoded
}
}