This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotvid.go
231 lines (213 loc) · 5.45 KB
/
otvid.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
package otgo
import (
"errors"
"fmt"
"time"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jws"
"github.com/lestrrat-go/jwx/jwt"
)
const otvidMaxSize = 2048
// OTVID represents a Open Trust Verifiable Identity Document.
type OTVID struct {
// ID is the Open Trust ID of the OTVID as present in the 'sub' claim
ID OTID
// Issuer is the principal that issued OTVID as present in 'iss' claim
Issuer OTID
// Audience is the intended recipient of OTVID as present in the 'aud' claim
Audience OTID
// Expiry is the expiration time of OTVID as present in 'exp' claim
Expiry time.Time
// IssuedAt is the the time at which the OTVID was issued as present in 'iat' claim
IssuedAt time.Time
// Release ID
ReleaseID string
// Claims is the parsed claims from token
Claims map[string]interface{}
// token is the serialized JWT token
token string
}
// ToJWT returns a JWT from OTVID.
func (o *OTVID) ToJWT() (Token, error) {
var err error
t := jwt.New()
for key, val := range o.Claims {
if err = t.Set(key, val); err != nil {
return t, err
}
}
if err = t.Set("sub", o.ID.String()); err != nil {
return t, err
}
if err = t.Set("iss", o.Issuer.String()); err != nil {
return t, err
}
if err = t.Set("aud", []string{o.Audience.String()}); err != nil {
return t, err
}
if err = t.Set("iat", o.IssuedAt); err != nil {
return t, err
}
if err = t.Set("exp", o.Expiry); err != nil {
return t, err
}
if o.ReleaseID != "" {
if err = t.Set("rid", o.ReleaseID); err != nil {
return t, err
}
}
return t, nil
}
// Validate ...
func (o *OTVID) Validate() error {
if err := o.ID.Validate(); err != nil {
return fmt.Errorf("sub OTID invalid: %s", err.Error())
}
if err := o.Issuer.Validate(); err != nil {
return fmt.Errorf("iss OTID invalid: %s", err.Error())
}
if err := o.Audience.Validate(); err != nil {
return fmt.Errorf("aud OTID invalid: %s", err.Error())
}
return nil
}
// Verify ...
func (o *OTVID) Verify(ks *JWKSet, issuer, audience OTID) error {
err := o.Validate()
if err != nil {
return err
}
if err = o.verifyClaims(issuer, audience); err != nil {
return err
}
if ks == nil {
return fmt.Errorf("otgo.OTVID.Verify: public keys required")
}
_, err = jwt.ParseString(o.token, jwt.WithKeySet(ks))
return err
}
func (o *OTVID) verifyClaims(issuer, audience OTID) error {
if !o.Issuer.Equal(issuer) {
return errors.New(`otgo.OTVID.Verify: issuer not satisfied`)
}
if !o.Audience.Equal(audience) {
return errors.New(`otgo.OTVID.Verify: audience not satisfied`)
}
if !time.Now().Truncate(time.Second).Before(o.Expiry) {
return errors.New(`otgo.OTVID.Validate: expiration time not satisfied`)
}
return nil
}
// Token ...
func (o *OTVID) Token() string {
return o.token
}
// MaybeRevoked ...
func (o *OTVID) MaybeRevoked() bool {
return o.ReleaseID != ""
}
// ShouldRenew ...
func (o *OTVID) ShouldRenew() bool {
return time.Now().Add(time.Second * 10).After(o.Expiry)
}
// Sign ...
func (o *OTVID) Sign(key Key) (string, error) {
var err error
var t Token
if err = validateKeys(key); err != nil {
return "", err
}
hdrs := jws.NewHeaders()
alg := key.Algorithm()
if err = hdrs.Set("alg", alg); err != nil {
return "", err
}
if err = hdrs.Set("kid", key.KeyID()); err != nil {
return "", err
}
o.IssuedAt = time.Now().UTC().Truncate(time.Second)
if o.Expiry.Unix() <= 0 {
o.Expiry = o.IssuedAt.Add(time.Minute * 10)
}
if t, err = o.ToJWT(); err != nil {
return "", err
}
s, err := jwt.Sign(t, jwa.SignatureAlgorithm(alg), key, jwt.WithHeaders(hdrs))
if err != nil {
return "", err
}
o.token = string(s)
if l := len(s); l > otvidMaxSize {
return "", fmt.Errorf("invalid OTVID, it' length %d is too large", l)
}
return o.token, nil
}
// FromJWT returns a OTVID from a JWT token
func FromJWT(token string, t Token) (*OTVID, error) {
var err error
vid := &OTVID{token: token}
vid.ID, err = ParseOTID(t.Subject())
if err == nil {
vid.Issuer, err = ParseOTID(t.Issuer())
}
if err == nil {
if as := t.Audience(); len(as) > 0 {
vid.Audience, err = ParseOTID(as[0])
}
}
if err == nil {
if rid, ok := t.Get("rid"); ok {
if vid.ReleaseID, ok = rid.(string); !ok {
return nil, fmt.Errorf("invalid 'rid' field, must be a string")
}
}
}
if err == nil {
vid.Expiry = t.Expiration()
vid.IssuedAt = t.IssuedAt()
vid.Claims = t.PrivateClaims()
err = vid.Validate()
}
if err != nil {
return nil, err
}
return vid, nil
}
// ParseOTVID parses a OTVID from a serialized JWT token.
// The OTVID signature is verified using the JWK set.
func ParseOTVID(token string, ks *JWKSet, issuer, audience OTID) (*OTVID, error) {
if l := len(token); l < 64 || l > 2048 {
return nil, fmt.Errorf("invalid OTVID token with length %d", l)
}
if ks == nil {
return nil, fmt.Errorf("otgo.ParseOTVID: public keys required")
}
t, err := jwt.ParseString(token, jwt.WithKeySet(ks))
if err != nil {
return nil, err
}
vid, err := FromJWT(token, t)
if err != nil {
return nil, err
}
if err = vid.verifyClaims(issuer, audience); err != nil {
return nil, err
}
return vid, nil
}
// ParseOTVIDInsecure parses a OTVID from a serialized JWT token.
// The OTVID signature is not verified.
func ParseOTVIDInsecure(token string) (*OTVID, error) {
if l := len(token); l < 64 || l > 2048 {
return nil, fmt.Errorf("invalid OTVID token with length %d", l)
}
t, err := jwt.ParseString(token)
if err != nil {
return nil, err
}
vid, err := FromJWT(token, t)
if err != nil {
return nil, err
}
return vid, nil
}