-
Notifications
You must be signed in to change notification settings - Fork 468
/
unmarshal.go
54 lines (47 loc) · 1.42 KB
/
unmarshal.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
package bundleutil
import (
"encoding/json"
"fmt"
"io"
"time"
"github.com/zeebo/errs"
)
func Decode(trustDomainID string, r io.Reader) (*Bundle, error) {
doc := new(bundleDoc)
if err := json.NewDecoder(r).Decode(doc); err != nil {
return nil, fmt.Errorf("failed to decode bundle: %v", err)
}
return unmarshal(trustDomainID, doc)
}
func Unmarshal(trustDomainID string, data []byte) (*Bundle, error) {
doc := new(bundleDoc)
if err := json.Unmarshal(data, doc); err != nil {
return nil, errs.Wrap(err)
}
return unmarshal(trustDomainID, doc)
}
func unmarshal(trustDomainID string, doc *bundleDoc) (*Bundle, error) {
bundle := New(trustDomainID)
bundle.SetRefreshHint(time.Second * time.Duration(doc.RefreshHint))
for i, key := range doc.Keys {
switch key.Use {
case x509SVIDUse:
if len(key.Certificates) != 1 {
return nil, errs.New("expected a single certificate in x509-svid entry %d; got %d", i, len(key.Certificates))
}
bundle.AppendRootCA(key.Certificates[0])
case jwtSVIDUse:
if key.KeyID == "" {
return nil, errs.New("missing key ID in jwt-svid entry %d", i)
}
if err := bundle.AppendJWTSigningKey(key.KeyID, key.Key); err != nil {
return nil, errs.New("failed to add jwt-svid entry %d: %v", i, err)
}
case "":
return nil, errs.New("missing use for key entry %d", i)
default:
return nil, errs.New("unrecognized use %q for key entry %d", key.Use, i)
}
}
return bundle, nil
}