-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
device.go
110 lines (92 loc) · 3.64 KB
/
device.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 models
import (
"time"
jwt "github.com/golang-jwt/jwt/v4"
)
type DeviceStatus string
const (
DeviceStatusAccepted DeviceStatus = "accepted"
DeviceStatusPending DeviceStatus = "pending"
DeviceStatusRejected DeviceStatus = "rejected"
DeviceStatusRemoved DeviceStatus = "removed"
DeviceStatusUnused DeviceStatus = "unused"
DeviceStatusEmpty DeviceStatus = ""
)
type Device struct {
// UID is the unique identifier for a device.
UID string `json:"uid"`
Name string `json:"name" bson:"name,omitempty" validate:"required,device_name"`
Identity *DeviceIdentity `json:"identity"`
Info *DeviceInfo `json:"info"`
PublicKey string `json:"public_key" bson:"public_key"`
TenantID string `json:"tenant_id" bson:"tenant_id"`
LastSeen time.Time `json:"last_seen" bson:"last_seen"`
Online bool `json:"online" bson:",omitempty"`
Namespace string `json:"namespace" bson:",omitempty"`
Status DeviceStatus `json:"status" bson:"status,omitempty" validate:"oneof=accepted rejected pending unused"`
StatusUpdatedAt time.Time `json:"status_updated_at" bson:"status_updated_at,omitempty"`
CreatedAt time.Time `json:"created_at" bson:"created_at,omitempty"`
RemoteAddr string `json:"remote_addr" bson:"remote_addr"`
Position *DevicePosition `json:"position" bson:"position"`
Tags []string `json:"tags" bson:"tags,omitempty"`
PublicURL bool `json:"public_url" bson:"public_url,omitempty"`
PublicURLAddress string `json:"public_url_address" bson:"public_url_address,omitempty"`
Acceptable bool `json:"acceptable" bson:"acceptable,omitempty"`
}
type DeviceAuthClaims struct {
UID string `json:"uid"`
Tenant string `json:"tenant"`
AuthClaims `mapstruct:",squash"`
jwt.RegisteredClaims `mapstruct:",squash"`
}
func (d *DeviceAuthClaims) SetRegisteredClaims(claims jwt.RegisteredClaims) {
d.RegisteredClaims = claims
}
type DeviceAuthRequest struct {
Info *DeviceInfo `json:"info"`
Sessions []string `json:"sessions,omitempty"`
*DeviceAuth
}
type DeviceAuth struct {
Hostname string `json:"hostname,omitempty" bson:"hostname,omitempty" validate:"required_without=Identity,omitempty,hostname_rfc1123" hash:"-"`
Identity *DeviceIdentity `json:"identity,omitempty" bson:"identity,omitempty" validate:"required_without=Hostname,omitempty"`
PublicKey string `json:"public_key"`
TenantID string `json:"tenant_id"`
}
type DeviceAuthResponse struct {
UID string `json:"uid"`
Token string `json:"token"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}
type DeviceIdentity struct {
MAC string `json:"mac"`
}
type DeviceInfo struct {
ID string `json:"id"`
PrettyName string `json:"pretty_name"`
Version string `json:"version"`
Arch string `json:"arch"`
Platform string `json:"platform"`
}
type ConnectedDevice struct {
UID string `json:"uid"`
TenantID string `json:"tenant_id" bson:"tenant_id"`
LastSeen time.Time `json:"last_seen" bson:"last_seen"`
}
type DevicePosition struct {
Latitude float64 `json:"latitude" bson:"latitude"`
Longitude float64 `json:"longitude" bson:"longitude"`
}
type DeviceRemoved struct {
Device *Device `json:"device" bson:"device"`
Timestamp time.Time `json:"timestamp" bson:"timestamp"`
}
type DeviceTag struct {
Tag string `validate:"required,min=3,max=255,alphanum,ascii,excludes=/@&:"`
}
func NewDeviceTag(tag string) DeviceTag {
return DeviceTag{
Tag: tag,
}
}