-
Notifications
You must be signed in to change notification settings - Fork 2
/
keys.go
243 lines (189 loc) · 7.91 KB
/
keys.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
232
233
234
235
236
237
238
239
240
241
242
243
package dynamo
import (
"encoding/base64"
"errors"
"fmt"
"strings"
)
const (
lpaPrefix = "LPA"
donorPrefix = "DONOR"
subPrefix = "SUB"
attorneyPrefix = "ATTORNEY"
certificateProviderPrefix = "CERTIFICATE_PROVIDER"
documentPrefix = "DOCUMENT"
evidenceReceivedPrefix = "EVIDENCE_RECEIVED"
organisationPrefix = "ORGANISATION"
memberPrefix = "MEMBER"
memberInvitePrefix = "MEMBERINVITE"
memberIDPrefix = "MEMBERID"
metadataPrefix = "METADATA"
donorSharePrefix = "DONORSHARE"
donorInvitePrefix = "DONORINVITE"
certificateProviderSharePrefix = "CERTIFICATEPROVIDERSHARE"
attorneySharePrefix = "ATTORNEYSHARE"
)
func readKey(s string) (any, error) {
prefix, _, ok := strings.Cut(s, "#")
if !ok {
return nil, fmt.Errorf("malformed key '%s'", s)
}
switch prefix {
case lpaPrefix:
return LpaKeyType(s), nil
case donorSharePrefix:
return DonorShareKeyType(s), nil
case certificateProviderSharePrefix:
return CertificateProviderShareKeyType(s), nil
case attorneySharePrefix:
return AttorneyShareKeyType(s), nil
case donorPrefix:
return DonorKeyType(s), nil
case subPrefix:
return SubKeyType(s), nil
case attorneyPrefix:
return AttorneyKeyType(s), nil
case certificateProviderPrefix:
return CertificateProviderKeyType(s), nil
case documentPrefix:
return DocumentKeyType(s), nil
case evidenceReceivedPrefix:
return EvidenceReceivedKeyType(s), nil
case organisationPrefix:
return OrganisationKeyType(s), nil
case memberPrefix:
return MemberKeyType(s), nil
case memberInvitePrefix:
return MemberInviteKeyType(s), nil
case memberIDPrefix:
return MemberIDKeyType(s), nil
case metadataPrefix:
return MetadataKeyType(s), nil
case donorInvitePrefix:
return DonorInviteKeyType(s), nil
default:
return nil, errors.New("unknown key prefix")
}
}
type PK interface{ PK() string }
type SK interface{ SK() string }
type LpaKeyType string
func (t LpaKeyType) PK() string { return string(t) }
func (t LpaKeyType) ID() string { return t.PK()[len(lpaPrefix)+1:] }
// LpaKey is used as the PK for all Lpa related information.
func LpaKey(s string) LpaKeyType {
return LpaKeyType(lpaPrefix + "#" + s)
}
type DonorKeyType string
func (t DonorKeyType) SK() string { return string(t) }
func (t DonorKeyType) lpaOwner() {} // mark as usable with LpaOwnerKey
// DonorKey is used as the SK (with LpaKey as PK) for donor entered
// information. It is set to PAPER when the donor information has been provided
// from paper forms.
func DonorKey(s string) DonorKeyType {
return DonorKeyType(donorPrefix + "#" + s)
}
type SubKeyType string
func (t SubKeyType) SK() string { return string(t) }
// SubKey is used as the SK (with LpaKey as PK) to allow queries on a OneLogin
// sub against all Lpas an actor may have provided information on.
func SubKey(s string) SubKeyType {
return SubKeyType(subPrefix + "#" + s)
}
type AttorneyKeyType string
func (t AttorneyKeyType) SK() string { return string(t) }
// AttorneyKey is used as the SK (with LpaKey as PK) for attorney entered
// information.
func AttorneyKey(s string) AttorneyKeyType {
return AttorneyKeyType(attorneyPrefix + "#" + s)
}
type CertificateProviderKeyType string
func (t CertificateProviderKeyType) SK() string { return string(t) }
// CertificateProviderKey is used as the SK (with LpaKey as PK) for certificate
// provider entered information.
func CertificateProviderKey(s string) CertificateProviderKeyType {
return CertificateProviderKeyType(certificateProviderPrefix + "#" + s)
}
type DocumentKeyType string
func (t DocumentKeyType) SK() string { return string(t) }
// DocumentKey is used as the SK (with LpaKey as PK) for any documents uploaded
// as evidence for reduced fees.
func DocumentKey(s3Key string) DocumentKeyType {
return DocumentKeyType(documentPrefix + "#" + s3Key)
}
type EvidenceReceivedKeyType string
func (t EvidenceReceivedKeyType) SK() string { return string(t) }
// EvidenceReceivedKey is used as the SK (with LpaKey as PK) to show that paper
// evidence has been submitted for an Lpa.
func EvidenceReceivedKey() EvidenceReceivedKeyType {
return EvidenceReceivedKeyType(evidenceReceivedPrefix + "#")
}
type OrganisationKeyType string
func (t OrganisationKeyType) PK() string { return string(t) }
func (t OrganisationKeyType) SK() string { return string(t) }
func (t OrganisationKeyType) ID() string { return t.PK()[len(organisationPrefix)+1:] }
func (t OrganisationKeyType) lpaOwner() {} // mark as usable with LpaOwnerKey
// OrganisationKey is used as the PK to group organisation data; or as the SK
// (with OrganisationKey as PK) for the organisation itself; or as the SK (with
// LpaKey as PK) for the donor information entered by a member of an
// organisation.
func OrganisationKey(organisationID string) OrganisationKeyType {
return OrganisationKeyType(organisationPrefix + "#" + organisationID)
}
type MemberKeyType string
func (t MemberKeyType) SK() string { return string(t) }
// MemberKey is used as the SK (with OrganisationKey as PK) for a member of an
// organisation.
func MemberKey(sessionID string) MemberKeyType {
return MemberKeyType(memberPrefix + "#" + sessionID)
}
type MemberInviteKeyType string
func (t MemberInviteKeyType) SK() string { return string(t) }
// MemberInviteKey is used as the SK (with OrganisationKey as PK) for a member
// invite.
func MemberInviteKey(email string) MemberInviteKeyType {
return MemberInviteKeyType(memberInvitePrefix + "#" + base64.StdEncoding.EncodeToString([]byte(email)))
}
type MemberIDKeyType string
func (t MemberIDKeyType) SK() string { return string(t) }
// MemberIDKey is used as the SK (with OrganisationKey as PK) to allow
// retrieving a member using their ID instead of their OneLogin sub.
func MemberIDKey(memberID string) MemberIDKeyType {
return MemberIDKeyType(memberIDPrefix + "#" + memberID)
}
type MetadataKeyType string
func (t MetadataKeyType) SK() string { return string(t) }
func (t MetadataKeyType) shareSort() {} // mark as usable with ShareSortKey
// MetadataKey is used as the SK when the value of the SK is not used for any purpose.
func MetadataKey(s string) MetadataKeyType {
return MetadataKeyType(metadataPrefix + "#" + s)
}
type DonorShareKeyType string
func (t DonorShareKeyType) PK() string { return string(t) }
func (t DonorShareKeyType) share() {} // mark as usable with ShareKey
// DonorShareKey is used as the PK for sharing an Lpa with a donor.
func DonorShareKey(code string) DonorShareKeyType {
return DonorShareKeyType(donorSharePrefix + "#" + code)
}
type DonorInviteKeyType string
func (t DonorInviteKeyType) SK() string { return string(t) }
func (t DonorInviteKeyType) shareSort() {} // mark as usable with ShareSortKey
// DonorInviteKey is used as the SK (with DonorShareKey as PK) for an invitation
// to a donor to link an Lpa being created by a member of an organisation.
func DonorInviteKey(organisation OrganisationKeyType, lpa LpaKeyType) DonorInviteKeyType {
return DonorInviteKeyType(donorInvitePrefix + "#" + organisation.ID() + "#" + lpa.ID())
}
type CertificateProviderShareKeyType string
func (t CertificateProviderShareKeyType) PK() string { return string(t) }
func (t CertificateProviderShareKeyType) share() {} // mark as usable with ShareKey
// CertificateProviderShareKey is used as the PK for sharing an Lpa with a donor.
func CertificateProviderShareKey(code string) CertificateProviderShareKeyType {
return CertificateProviderShareKeyType(certificateProviderSharePrefix + "#" + code)
}
type AttorneyShareKeyType string
func (t AttorneyShareKeyType) PK() string { return string(t) }
func (t AttorneyShareKeyType) share() {} // mark as usable with ShareKey
// AttorneyShareKey is used as the PK for sharing an Lpa with a donor.
func AttorneyShareKey(code string) AttorneyShareKeyType {
return AttorneyShareKeyType(attorneySharePrefix + "#" + code)
}