-
Notifications
You must be signed in to change notification settings - Fork 2
/
creator.go
167 lines (153 loc) · 4.51 KB
/
creator.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
/* ****************************************************************************
* Copyright 2020 51 Degrees Mobile Experts Limited (51degrees.com)
*
* 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 owid
import (
"encoding/json"
"fmt"
"time"
)
// Creator of Open Web Ids and immutable data.
type Creator struct {
domain string // The registered domain name and key fields
privateKey string
publicKey string
name string // The name of the entity associated with the domain
contractURL string // URL with the T&Cs associated with the creation of data
sign *Crypto
verify *Crypto
}
// CreateOWID returns a new unsigned OWID from the creator containing the
// payload provided.
func (c *Creator) CreateOWID(payload []byte) (*OWID, error) {
return NewOwid(c.domain, time.Now(), payload)
}
// Sign the OWID by updating the signature field.
func (c *Creator) Sign(o *OWID, others ...*OWID) error {
if c.domain != o.Domain {
return fmt.Errorf(
"can't use creator '%s' to sign OWID for domain '%s'",
c.domain,
o.Domain)
}
x, err := c.NewCryptoSignOnly()
if err != nil {
return err
}
return o.Sign(x, others)
}
// CreateOWIDandSign the OWID with the payload and signs the result.
func (c *Creator) CreateOWIDandSign(
payload []byte,
others ...*OWID) (*OWID, error) {
o, err := c.CreateOWID(payload)
if err != nil {
return nil, err
}
err = c.Sign(o, others...)
if err != nil {
return nil, err
}
return o, nil
}
// Verify the OWID and any other OWIDs are valid for this creator.
func (c *Creator) Verify(o *OWID, others ...*OWID) (bool, error) {
if c.domain != o.Domain {
return false, fmt.Errorf(
"Can't use creator '%s' to verify OWID for domain '%s'",
c.domain,
o.Domain)
}
x, err := c.NewCryptoVerifyOnly()
if err != nil {
return false, err
}
return o.VerifyWithCrypto(x, others)
}
// NewCryptoSignOnly creates a new instance of the Crypto structure
// for signing OWIDs only.
func (c *Creator) NewCryptoSignOnly() (*Crypto, error) {
if c.sign == nil {
var err error
c.sign, err = NewCryptoSignOnly(c.privateKey)
if err != nil {
return nil, err
}
}
return c.sign, nil
}
// NewCryptoVerifyOnly creates a new instance of the Crypto structure
// for Verifying OWIDs only.
func (c *Creator) NewCryptoVerifyOnly() (*Crypto, error) {
if c.verify == nil {
var err error
c.verify, err = NewCryptoVerifyOnly(c.publicKey)
if err != nil {
return nil, err
}
}
return c.verify, nil
}
// SubjectPublicKeyInfo returns the public key in SPKI form.
func (c *Creator) SubjectPublicKeyInfo() (string, error) {
cry, err := NewCryptoVerifyOnly(c.publicKey)
if err != nil {
return "", err
}
return cry.getSubjectPublicKeyInfo()
}
// Domain associated with the creator.
func (c *Creator) Domain() string { return c.domain }
// MarshalJSON marshals a node to JSON without having to expose the fields in
// the node struct. This is achieved by converting a node to a map.
func (c *Creator) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"domain": c.domain,
"privateKey": c.privateKey,
"publicKey": c.publicKey,
"name": c.name,
"contractURL:": c.contractURL})
}
// UnmarshalJSON called by json.Unmarshall unmarshals a node from JSON and turns
// it into a new node. As the node is marshalled to JSON by converting it to a
// map, the unmarshalling from JSON needs to handle the type of each field
// correctly.
func (c *Creator) UnmarshalJSON(b []byte) error {
var d map[string]string
err := json.Unmarshal(b, &d)
if err != nil {
return err
}
c.domain = d["domain"]
c.privateKey = d["privateKey"]
c.publicKey = d["publicKey"]
c.name = d["name"]
c.contractURL = d["contractURL"]
return nil
}
func newCreator(
domain string,
privateKey string,
publicKey string,
name string,
contractURL string) *Creator {
var c Creator
c.domain = domain
c.privateKey = privateKey
c.publicKey = publicKey
c.name = name
c.contractURL = contractURL
return &c
}