This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappointments.go
627 lines (528 loc) · 18.2 KB
/
appointments.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Kiebitz - Privacy-Friendly Appointment Scheduling
// Copyright (C) 2021-2021 The Kiebitz Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version. Additional terms
// as defined in section 7 of the license (e.g. regarding attribution)
// are specified at https://kiebitz.eu/en/docs/open-source/additional-terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package services
import (
"encoding/json"
"github.com/impfen/services-inoeg/crypto"
"time"
)
// Generic signed params for checking
type SignedParams struct {
JSON string
Signature []byte
PublicKey []byte
Timestamp time.Time
ExtraData interface{}
}
// ConfirmProvider
type ConfirmProviderSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *ConfirmProviderParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
// this data is accessible to the provider, nothing "secret" should be
// stored here...
type ConfirmProviderParams struct {
Timestamp time.Time `json:"timestamp"`
PublicProviderData *SignedProviderData `json:"publicProviderData"`
ConfirmedProviderData *ConfirmedProviderData `json:"confirmedProviderData"`
SignedKeyData *SignedProviderKeyData `json:"signedKeyData"`
}
type ConfirmedProviderData struct {
JSON string `json:"data" coerce:"name:json"`
Data *crypto.ECDHEncryptedData `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type SignedProviderKeyData struct {
JSON string `json:"data" coerce:"name:json"`
Data *ProviderKeyData `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
func (k *ProviderKeyData) Sign(key *crypto.Key) (*SignedProviderKeyData, error) {
if data, err := json.Marshal(k); err != nil {
return nil, err
} else if signedData, err := key.Sign(data); err != nil {
return nil, err
} else {
return &SignedProviderKeyData{
JSON: string(data),
Signature: signedData.Signature,
PublicKey: signedData.PublicKey,
Data: k,
}, nil
}
}
type ProviderKeyData struct {
Signing []byte `json:"signing"`
Encryption []byte `json:"encryption"`
QueueData *ProviderQueueData `json:"queueData"`
}
type ProviderQueueData struct {
ZipCode string `json:"zipCode"`
Accessible bool `json:"accessible"`
}
// ResetDB
type ResetDBSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *ResetDBParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type ResetDBParams struct {
Timestamp time.Time `json:"timestamp"`
}
// AddMediatorPublicKeys
type AddMediatorPublicKeysSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *AddMediatorPublicKeysParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type AddMediatorPublicKeysParams struct {
Timestamp time.Time `json:"timestamp"`
SignedKeyData *SignedMediatorKeyData `json:"signedKeyData"`
}
type SignedMediatorKeyData struct {
JSON string `json:"data" coerce:"name:json"`
Data *MediatorKeyData `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
func (k *MediatorKeyData) Sign(key *crypto.Key) (*SignedMediatorKeyData, error) {
if data, err := json.Marshal(k); err != nil {
return nil, err
} else if signedData, err := key.Sign(data); err != nil {
return nil, err
} else {
return &SignedMediatorKeyData{
JSON: string(data),
Signature: signedData.Signature,
PublicKey: signedData.PublicKey,
Data: k,
}, nil
}
}
type MediatorKeyData struct {
Signing []byte `json:"signing"`
Encryption []byte `json:"encryption"`
}
// AddCodes
type AddCodesParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *CodesData `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type CodesData struct {
Actor string `json:"actor"`
Timestamp time.Time `json:"timestamp"`
Codes [][]byte `json:"codes"`
}
// UploadDistances
type UploadDistancesSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *UploadDistancesParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type UploadDistancesParams struct {
Timestamp time.Time `json:"timestamp"`
Type string `json:"type"`
Distances []Distance `json:"distances"`
}
type Distance struct {
From string `json:"from"`
To string `json:"to"`
Distance float64 `json:"distance"`
}
// getKeys
type GetKeysParams struct {
}
// getConfigurables
type GetConfigurablesParams struct {
}
type Keys struct {
ProviderData []byte `json:"providerData"`
RootKey []byte `json:"rootKey"`
TokenKey []byte `json:"tokenKey"`
}
type KeyLists struct {
Providers []*ActorKey `json:"providers"`
Mediators []*ActorKey `json:"mediators"`
}
type ActorKey struct {
ID []byte `json:"id"`
Data string `json:"data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
data *ActorKeyData `json:"-"`
}
func (a *ActorKey) KeyData() (*ActorKeyData, error) {
var akd *ActorKeyData
if a.data != nil {
return a.data, nil
}
if err := json.Unmarshal([]byte(a.Data), &akd); err != nil {
return nil, err
}
a.data = akd
return akd, nil
}
func (a *ActorKey) ProviderKeyData() (*ProviderKeyData, error) {
var pkd *ProviderKeyData
if err := json.Unmarshal([]byte(a.Data), &pkd); err != nil {
return nil, err
}
if pkd.QueueData == nil {
pkd.QueueData = &ProviderQueueData{}
}
return pkd, nil
}
type ActorKeyData struct {
Encryption []byte `json:"encryption"`
Signing []byte `json:"signing"`
}
// GetToken
type GetTokenParams struct {
Hash []byte `json:"hash"`
Code []byte `json:"code"`
PublicKey []byte `json:"publicKey"`
}
type SignedTokenData struct {
JSON string `json:"data" coerce:"name:json"`
Data *TokenData `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
func (k *TokenData) Sign(key *crypto.Key) (*SignedTokenData, error) {
if data, err := json.Marshal(k); err != nil {
return nil, err
} else if signedData, err := key.Sign(data); err != nil {
return nil, err
} else {
return &SignedTokenData{
JSON: string(data),
Signature: signedData.Signature,
PublicKey: signedData.PublicKey,
Data: k,
}, nil
}
}
type PriorityToken struct {
N int64 `json:"n"`
}
func (p *PriorityToken) Marshal() ([]byte, error) {
if data, err := json.Marshal(p); err != nil {
return nil, err
} else {
return data, nil
}
}
type TokenData struct {
JSON string `json:"data" coerce:"name:json"`
Data *PriorityToken `json:"-" coerce:"name:data"`
PublicKey []byte `json:"publicKey"`
Token []byte `json:"token"`
Hash []byte `json:"hash"`
}
// GetAppointmentsByZipCode
type GetAppointmentsByZipCodeParams struct {
Radius int64 `json:"radius"`
ZipCode string `json:"zipCode"`
From time.Time `json:"from"`
To time.Time `json:"to"`
}
// GetProvidersAggregated
type GetAppointmentsAggregatedParams struct {
Date time.Time `json:"date"`
ZipFrom string `json:"zipFrom"`
ZipTo string `json:"zipTo"`
}
// GetProvidersByZipCode
type GetProvidersByZipCodeParams struct {
ZipFrom string `json:"zipFrom"`
ZipTo string `json:"zipTo"`
}
type KeyChain struct {
Provider *ActorKey `json:"provider"`
Mediator *ActorKey `json:"mediator"`
}
type ProviderAppointments struct {
Provider *SignedProviderData `json:"provider"`
Appointments []*SignedAppointment `json:"appointments"`
KeyChain *KeyChain `json:"keyChain"`
}
type AggregatedProviderAppointments struct {
Provider *ProviderData `json:"provider"`
Appointments []*AppointmentAggregated `json:"appointments"`
}
type SignedProviderData struct {
JSON string `json:"data" coerce:"name:json"`
Data *ProviderData `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
ID []byte `json:"id"`
}
func (k *ProviderData) Sign(key *crypto.Key) (*SignedProviderData, error) {
if data, err := json.Marshal(k); err != nil {
return nil, err
} else if signedData, err := key.Sign(data); err != nil {
return nil, err
} else {
return &SignedProviderData{
JSON: string(data),
Signature: signedData.Signature,
PublicKey: signedData.PublicKey,
Data: k,
}, nil
}
}
type ProviderData struct {
ID []byte `json:"id,omitempty"`
Name string `json:"name"`
Street string `json:"street"`
City string `json:"city"`
ZipCode string `json:"zipCode"`
Description string `json:"description"`
Accessible bool `json:"accessible"`
}
// GetProviderAppointments
type GetProviderAppointmentsSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *GetProviderAppointmentsParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type GetProviderAppointmentsParams struct {
Timestamp time.Time `json:"timestamp"`
From time.Time `json:"from"`
To time.Time `json:"to"`
UpdatedSince *time.Time `json:"updatedSince"`
}
// GetProviderAppointmentsByProperty
type GetProviderAppointmentsByPropertySignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *GetProviderAppointmentsByPropertyParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type GetProviderAppointmentsByPropertyParams struct {
Key string `json:"key"`
Value string `json:"value"`
Timestamp time.Time `json:"timestamp"`
}
// PublishAppointments
type PublishAppointmentsSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *PublishAppointmentsParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type PublishAppointmentsParams struct {
Timestamp time.Time `json:"timestamp"`
Appointments []*SignedAppointment `json:"appointments"`
}
type SignedAppointment struct {
UpdatedAt time.Time `json:"updatedAt"`
Bookings []*Booking `json:"bookings"` // only for providers
BookedSlots []*Slot `json:"bookedSlots"` // for users
JSON string `json:"data" coerce:"name:json"`
Data *Appointment `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
func MakeAppointment(timestamp time.Time, slots, duration int64) (*Appointment, error) {
if id, err := crypto.RandomBytes(32); err != nil {
return nil, err
} else {
slotData := make([]*Slot, slots)
for i, _ := range slotData {
sd := &Slot{}
if id, err := crypto.RandomBytes(32); err != nil {
return nil, err
} else {
sd.ID = id
}
slotData[i] = sd
}
return &Appointment{
Timestamp: timestamp,
Duration: duration,
ID: id,
SlotData: slotData,
}, nil
}
}
type Appointment struct {
ID []byte `json:"id"`
Duration int64 `json:"duration"`
Properties map[string]string `json:"properties"`
SlotData []*Slot `json:"slotData"`
Timestamp time.Time `json:"timestamp"`
Vaccine string `json:"vaccine"`
PublicKey []byte `json:"publicKey"`
}
func (k *Appointment) Sign(key *crypto.Key) (*SignedAppointment, error) {
if data, err := json.Marshal(k); err != nil {
return nil, err
} else if signedData, err := key.Sign(data); err != nil {
return nil, err
} else {
return &SignedAppointment{
JSON: string(data),
Signature: signedData.Signature,
PublicKey: signedData.PublicKey,
Data: k,
}, nil
}
}
type Slot struct {
ID []byte `json:"id"`
}
type AppointmentAggregated struct {
ID []byte `json:"id"`
Duration int64 `json:"duration"`
Properties map[string]string `json:"properties"`
SlotN int `json:"slotN"`
Timestamp time.Time `json:"timestamp"`
Vaccine string `json:"vaccine"`
}
// ValidateUser
type ValidateUserSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *ValidateUserParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type ValidateUserParams struct {
SignedTokenData *SignedTokenData `json:"signedTokenData"`
Timestamp time.Time `json:"timestamp"`
}
// BookAppointment
type BookAppointmentSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *BookAppointmentParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type BookAppointmentParams struct {
ProviderID []byte `json:"providerID"`
ID []byte `json:"id"`
EncryptedData *crypto.ECDHEncryptedData `json:"encryptedData"`
SignedTokenData *SignedTokenData `json:"signedTokenData"`
Timestamp time.Time `json:"timestamp"`
}
type Booking struct {
ID []byte `json:"id"`
PublicKey []byte `json:"publicKey"`
Token []byte `json:"token"`
EncryptedData *crypto.ECDHEncryptedData `json:"encryptedData"`
}
// GetAppointment
type GetAppointmentParams struct {
ProviderID []byte `json:"providerID"`
ID []byte `json:"id"`
}
// CancelAppointment
type CancelAppointmentSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *CancelAppointmentParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type CancelAppointmentParams struct {
Timestamp time.Time `json:"timestamp"`
ProviderID []byte `json:"providerID"`
SignedTokenData *SignedTokenData `json:"signedTokenData"`
ID []byte `json:"id"`
}
// CheckProviderData
type CheckProviderDataSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *CheckProviderDataParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type CheckProviderDataParams struct {
Timestamp time.Time `json:"timestamp"`
}
// StoreProviderData
type StoreProviderDataSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *StoreProviderDataParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type StoreProviderDataParams struct {
Timestamp time.Time `json:"timestamp"`
EncryptedData *crypto.ECDHEncryptedData `json:"encryptedData"`
Code []byte `json:"code"`
}
type RawProviderData struct {
ID []byte `json:"id,omitempty"`
Verified bool `json:"verified,omitempty"`
Status string `json:"status,omitempty"`
EncryptedData *crypto.ECDHEncryptedData `json:"encryptedData"`
}
// GetProviderData
type GetProviderDataSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *GetProviderDataParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type GetProviderDataParams struct {
Timestamp time.Time `json:"timestamp"`
ProviderID []byte `json:"providerID"`
}
type GetProviderResult struct {
UnverifiedData *RawProviderData `json:"unverifiedData"`
VerifiedData *RawProviderData `json:"verifiedData"`
}
// GetProvidersData
type GetProvidersDataSignedParams struct {
JSON string `json:"data" coerce:"name:json"`
Data *GetProvidersDataParams `json:"-" coerce:"name:data"`
Signature []byte `json:"signature"`
PublicKey []byte `json:"publicKey"`
}
type GetProvidersDataParams struct {
Timestamp time.Time `json:"timestamp"`
Limit int64 `json:"limit"`
}
// GetStats
type GetStatsParams struct {
ID string `json:"id"`
Type string `json:"type"`
Filter map[string]interface{} `json:"filter"`
Metric string `json:"metric"`
Name string `json:"name"`
From *time.Time `json:"from"` // optional
To *time.Time `json:"to"` // optional
N *int64 `json:"n"` // optional
}
type StatsValue struct {
Name string `json:"name"`
From time.Time `json:"from"`
To time.Time `json:"to"`
Data map[string]string `json:"data"`
Value int64 `json:"value"`
}