-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.go
executable file
·110 lines (94 loc) · 3.07 KB
/
data.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
package hypertrace
import (
"context"
"encoding/json"
"fmt"
"time"
)
const (
UploadTokenLength = 16
)
var (
ErrRegisterUIDError = fmt.Errorf("uid registration error")
ErrUIDNotFound = fmt.Errorf("uid not found")
ErrTokenNotFound = fmt.Errorf("token not found")
ErrSecretNotValid = fmt.Errorf("secret not valid")
ErrInvalidParameter = fmt.Errorf("invalid parameter")
)
type ITracing interface {
RegisterNewUser(ctx context.Context, UID, PIN string) (err error)
GetHandshakePIN(ctx context.Context, UID string) (PIN string, err error)
SaveTraceData(ctx context.Context, UID, OID string, data []*TraceData) (err error)
PurgeOldTraceData(ctx context.Context, oldestTimeStamp int64) (err error)
GetTraceData(ctx context.Context, UID string) (traces []*TraceData, err error)
RegisterNewOfficer(ctx context.Context, OID, secret string) (err error)
GetOfficerID(ctx context.Context, secret string) (OID string, err error)
DeleteOfficer(ctx context.Context, OID string) (err error)
}
type User struct {
UID string `json:"uid" bson:"uid"`
PIN string `json:"pin" bson:"pin"`
}
type Officer struct {
OID string `json:"oid" bson:"oid"`
Secret string `json:"secret" bson:"secret"`
}
type TraceData struct {
OID string `json:"oid,omitempty" bson:"oid"`
UID string `json:"uid,omitempty" bson:"uid"`
CUID string `json:"cuid" bson:"cuid"`
Timestamp int64 `json:"timestamp" bson:"timestamp"`
ModelC string `json:"modelC" bson:"modelC"`
ModelP string `json:"modelP" bson:"modelP"`
RSSI int `json:"rssi" bson:"rssi"`
TxPower int `json:"txPower" bson:"txPower"`
Org string `json:"org" bson:"org"`
}
func NewUploadToken(uid, oid string, validHour int) *UploadToken {
return &UploadToken{
OID: oid,
UID: uid,
ValidFrom: time.Now().Unix(),
ValidUntil: time.Now().Add(time.Duration(validHour) * time.Hour).Unix(),
}
}
func NewUploadTokenFromString(token string, key []byte) (*UploadToken, error) {
dataJson, err := decodeAndDecrypt(token, key)
if err != nil {
return nil, err
}
ut := &UploadToken{}
err = json.Unmarshal(dataJson, ut)
return ut, err
}
type UploadToken struct {
OID string `json:"oid" bson:"oid"`
UID string `json:"uid" bson:"uid"`
ValidFrom int64 `json:"nbf" bson:"nbf"`
ValidUntil int64 `json:"exp" bson:"exp"`
}
func (ut *UploadToken) IsValid() bool {
n := time.Now().Unix()
return n > ut.ValidFrom && n < ut.ValidUntil
}
func (ut *UploadToken) ToToken(key []byte) (token string, err error) {
utBytes, err := json.Marshal(ut)
if err != nil {
return "", err
}
return encryptAndEncode(utBytes, key)
}
type DataUpload struct {
UID string `json:"uid" bson:"uid"`
UploadToken string `json:"uploadToken" bson:"uploadToken"`
Traces []*UploadTraceRecord `json:"traces" bson:"traces"`
}
type UploadTraceRecord struct {
Timestamp int64 `json:"timestamp"`
Message string `json:"msg"`
ModelC string `json:"modelC"`
ModelP string `json:"modelP"`
RSSI int `json:"rssi"`
TxPower int `json:"txPower"`
Org string `json:"org"`
}