-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
61 lines (48 loc) · 1.22 KB
/
create.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
// Copyright 2020 Self Group Ltd. All Rights Reserved.
package fact
import (
"encoding/json"
"time"
"github.com/google/uuid"
"github.com/joinself/self-go-sdk/pkg/ntp"
"gopkg.in/square/go-jose.v2"
)
// Attest creates an attested fact about a self identity
func (s Service) Attest(selfID string, facts []Fact) ([]json.RawMessage, error) {
attestations := make([]json.RawMessage, len(facts))
opts := &jose.SignerOptions{
ExtraHeaders: map[jose.HeaderKey]interface{}{
"kid": s.keyID,
},
}
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.EdDSA, Key: s.sk}, opts)
if err != nil {
return nil, err
}
for i, f := range facts {
if len(f.Sources) != 0 {
return nil, ErrFactBadSource
}
err = f.validate()
if err != nil {
return nil, err
}
payload, err := json.Marshal(map[string]string{
"jti": uuid.New().String(),
"iss": s.selfID,
"sub": selfID,
"iat": ntp.TimeFunc().Format(time.RFC3339),
"source": f.Sources[0],
f.Fact: f.AttestedValue,
})
if err != nil {
return nil, err
}
attestation, err := signer.Sign(payload)
if err != nil {
return nil, err
}
attestations[i] = json.RawMessage(attestation.FullSerialize())
}
return attestations, nil
}