/
manifest.go
221 lines (178 loc) · 5.26 KB
/
manifest.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
package accesscontroller
import (
"context"
"strings"
"sync"
"berty.tech/go-ipfs-log/io"
cid "github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/pkg/errors"
"github.com/polydawn/refmt/obj/atlas"
"go.uber.org/zap"
)
// Manifest An access controller manifest
type Manifest struct {
Type string
Params *CreateAccessControllerOptions
}
// CreateAccessControllerOptions Options used to create an Access Controller
type CreateAccessControllerOptions struct {
SkipManifest bool
Address cid.Cid
Type string
Name string
Access map[string][]string
muAccess sync.RWMutex
}
func CloneManifestParams(m ManifestParams) *CreateAccessControllerOptions {
return &CreateAccessControllerOptions{
Type: m.GetType(),
SkipManifest: m.GetSkipManifest(),
Name: m.GetName(),
Access: m.GetAllAccess(),
Address: m.GetAddress(),
}
}
func (m *CreateAccessControllerOptions) GetName() string {
return m.Name
}
func (m *CreateAccessControllerOptions) SetName(name string) {
m.Name = name
}
func (m *CreateAccessControllerOptions) SetAccess(role string, allowed []string) {
m.muAccess.Lock()
defer m.muAccess.Unlock()
if m.Access == nil {
m.Access = make(map[string][]string)
}
m.Access[role] = allowed
}
func (m *CreateAccessControllerOptions) GetAccess(role string) []string {
m.muAccess.RLock()
defer m.muAccess.RUnlock()
return m.Access[role]
}
func (m *CreateAccessControllerOptions) GetAllAccess() map[string][]string {
m.muAccess.RLock()
defer m.muAccess.RUnlock()
if m.Access == nil {
return map[string][]string{}
}
accessCopy := map[string][]string{}
for k, v := range m.Access {
accessCopy[k] = v
}
return accessCopy
}
func (m *CreateAccessControllerOptions) GetType() string {
return m.Type
}
func (m *CreateAccessControllerOptions) SetType(t string) {
m.Type = t
}
// Create a new manifest parameters instance
func NewManifestParams(address cid.Cid, skipManifest bool, manifestType string) ManifestParams {
return &CreateAccessControllerOptions{
Address: address,
SkipManifest: skipManifest,
Type: manifestType,
}
}
func NewEmptyManifestParams() ManifestParams {
return &CreateAccessControllerOptions{}
}
func NewSimpleManifestParams(manifestType string, access map[string][]string) ManifestParams {
return &CreateAccessControllerOptions{
SkipManifest: true,
Access: access,
Type: manifestType,
}
}
func (m *CreateAccessControllerOptions) GetSkipManifest() bool {
return m.SkipManifest
}
func (m *CreateAccessControllerOptions) GetAddress() cid.Cid {
return m.Address
}
func (m *CreateAccessControllerOptions) SetAddress(c cid.Cid) {
m.Address = c
}
// ManifestParams List of getters for a manifest parameters
type ManifestParams interface {
GetSkipManifest() bool
GetAddress() cid.Cid
SetAddress(cid.Cid)
GetType() string
SetType(string)
GetName() string
SetName(string)
SetAccess(string, []string)
GetAccess(string) []string
GetAllAccess() map[string][]string
}
// CreateManifest Creates a new manifest and returns its CID
func CreateManifest(ctx context.Context, ipfs coreapi.CoreAPI, controllerType string, params ManifestParams) (cid.Cid, error) {
if params.GetSkipManifest() {
return params.GetAddress(), nil
}
manifest := &Manifest{
Type: controllerType,
Params: &CreateAccessControllerOptions{
Address: params.GetAddress(),
SkipManifest: params.GetSkipManifest(),
},
}
return io.WriteCBOR(ctx, ipfs, manifest, nil)
}
// ResolveManifest Retrieves a manifest from its address
func ResolveManifest(ctx context.Context, ipfs coreapi.CoreAPI, manifestAddress string, params ManifestParams) (*Manifest, error) {
if params.GetSkipManifest() {
if params.GetType() == "" {
return nil, errors.New("no manifest, access-controller type required")
}
manifest := &Manifest{
Type: params.GetType(),
Params: CloneManifestParams(params),
}
return manifest, nil
}
if strings.HasPrefix(manifestAddress, "/ipfs") {
manifestAddress = strings.Split(manifestAddress, "/")[2]
}
c, err := cid.Decode(manifestAddress)
if err != nil {
return nil, errors.Wrap(err, "unable to parse CID")
}
node, err := io.ReadCBOR(ctx, ipfs, c)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch manifest data")
}
manifest := &Manifest{}
err = cbornode.DecodeInto(node.RawData(), &manifest)
if err != nil {
return nil, errors.Wrap(err, "unable to unmarshal")
}
return manifest, nil
}
var _ ManifestParams = &CreateAccessControllerOptions{}
func WithLogger(logger *zap.Logger) func(ac Interface) {
return func(ac Interface) {
ac.SetLogger(logger)
}
}
func init() {
atlasManifest := atlas.BuildEntry(Manifest{}).
StructMap().
AddField("Type", atlas.StructMapEntry{SerialName: "type"}).
AddField("Params", atlas.StructMapEntry{SerialName: "manifest"}).
Complete()
atlasManifestParams := atlas.BuildEntry(CreateAccessControllerOptions{}).
StructMap().
AddField("SkipManifest", atlas.StructMapEntry{SerialName: "skip_manifest"}).
AddField("Address", atlas.StructMapEntry{SerialName: "address"}).
AddField("Type", atlas.StructMapEntry{SerialName: "type"}).
Complete()
cbornode.RegisterCborType(atlasManifest)
cbornode.RegisterCborType(atlasManifestParams)
}