@@ -68,119 +68,82 @@ type AuthService interface {
68
68
}
69
69
70
70
func (s * service ) AuthDevice (ctx context.Context , req requests.DeviceAuth , remoteAddr string ) (* models.DeviceAuthResponse , error ) {
71
- var identity * models.DeviceIdentity
72
- if req .Identity != nil {
73
- identity = & models.DeviceIdentity {
74
- MAC : req .Identity .MAC ,
75
- }
71
+ if _ , err := s .store .NamespaceGet (ctx , store .NamespaceIdentTenantID , req .TenantID ); err != nil {
72
+ return nil , NewErrNamespaceNotFound (req .TenantID , err )
76
73
}
77
74
78
- var hostname string
79
- if req .Hostname ! = "" {
80
- hostname = req . Hostname
81
- }
75
+ if req . Hostname == "" {
76
+ if req .Identity == nil || req . Identity . MAC = = "" {
77
+ return nil , NewErrAuthDeviceNoIdentityAndHostname ()
78
+ }
82
79
83
- if hostname == "" && (identity == nil || identity .MAC == "" ) {
84
- return nil , NewErrAuthDeviceNoIdentityAndHostname ()
80
+ req .Hostname = strings .ReplaceAll (req .Identity .MAC , ":" , "-" )
85
81
}
86
82
87
83
auth := models.DeviceAuth {
88
- Hostname : hostname ,
89
- Identity : identity ,
84
+ Hostname : req . Hostname ,
85
+ Identity : & models. DeviceIdentity { MAC : req . Identity . MAC } ,
90
86
PublicKey : req .PublicKey ,
91
87
TenantID : req .TenantID ,
92
88
}
93
89
94
- uid := sha256 .Sum256 (structhash .Dump (auth , 1 ))
95
- key := hex .EncodeToString (uid [:])
96
-
97
- claims := authorizer.DeviceClaims {
98
- UID : key ,
99
- TenantID : req .TenantID ,
100
- }
101
-
102
- token , err := jwttoken .EncodeDeviceClaims (claims , s .privKey )
90
+ uidSHA := sha256 .Sum256 (structhash .Dump (auth , 1 ))
91
+ device , err := s .store .DeviceGet (ctx , store .DeviceIdentUID , hex .EncodeToString (uidSHA [:]))
103
92
if err != nil {
104
- return nil , NewErrTokenSigned (err )
105
- }
106
-
107
- type Device struct {
108
- Name string
109
- Namespace string
110
- }
111
-
112
- var value * Device
113
-
114
- if err := s .cache .Get (ctx , strings .Join ([]string {"auth_device" , key }, "/" ), & value ); err == nil && value != nil {
115
- return & models.DeviceAuthResponse {
116
- UID : key ,
117
- Token : token ,
118
- Name : value .Name ,
119
- Namespace : value .Namespace ,
120
- }, nil
121
- }
122
- var info * models.DeviceInfo
123
- if req .Info != nil {
124
- info = & models.DeviceInfo {
125
- ID : req .Info .ID ,
126
- PrettyName : req .Info .PrettyName ,
127
- Version : req .Info .Version ,
128
- Arch : req .Info .Arch ,
129
- Platform : req .Info .Platform ,
93
+ if err != store .ErrNoDocuments {
94
+ return nil , err
130
95
}
131
- }
132
-
133
- position , err := s .locator .GetPosition (net .ParseIP (remoteAddr ))
134
- if err != nil {
135
- return nil , err
136
- }
137
96
138
- device := models.Device {
139
- UID : key ,
140
- Identity : identity ,
141
- Info : info ,
142
- PublicKey : req .PublicKey ,
143
- TenantID : req .TenantID ,
144
- LastSeen : clock .Now (),
145
- RemoteAddr : remoteAddr ,
146
- Position : & models.DevicePosition {
147
- Longitude : position .Longitude ,
148
- Latitude : position .Latitude ,
149
- },
150
- }
97
+ position , err := s .locator .GetPosition (net .ParseIP (remoteAddr ))
98
+ if err != nil {
99
+ return nil , err
100
+ }
151
101
152
- // The order here is critical as we don't want to register devices if the tenant id is invalid
153
- namespace , err := s .store .NamespaceGet (ctx , store .NamespaceIdentTenantID , device .TenantID )
154
- if err != nil {
155
- return nil , NewErrNamespaceNotFound (device .TenantID , err )
156
- }
102
+ device = & models.Device {
103
+ UID : hex .EncodeToString (uidSHA [:]),
104
+ TenantID : req .TenantID ,
105
+ LastSeen : clock .Now (),
106
+ DisconnectedAt : & time.Time {},
107
+ Status : models .DeviceStatusPending ,
108
+ Name : req .Hostname ,
109
+ Identity : & models.DeviceIdentity {MAC : req .Identity .MAC },
110
+ PublicKey : req .PublicKey ,
111
+ Position : & models.DevicePosition {
112
+ Longitude : position .Longitude ,
113
+ Latitude : position .Latitude ,
114
+ },
115
+ Info : nil ,
116
+ }
157
117
158
- hostname = strings .ToLower (hostname )
118
+ if req .Info != nil {
119
+ device .Info = & models.DeviceInfo {
120
+ ID : req .Info .ID ,
121
+ PrettyName : req .Info .PrettyName ,
122
+ Version : req .Info .Version ,
123
+ Arch : req .Info .Arch ,
124
+ Platform : req .Info .Platform ,
125
+ }
126
+ }
159
127
160
- if err := s .store .DeviceCreate (ctx , device , hostname ); err != nil {
161
- return nil , NewErrDeviceCreate (device , err )
162
- }
128
+ if _ , err := s .store .DeviceCreate (ctx , device ); err != nil {
129
+ return nil , NewErrDeviceCreate (* device , err )
130
+ }
131
+ } else {
132
+ device .DisconnectedAt = nil
133
+ device .LastSeen = clock .Now ()
163
134
164
- for _ , uid := range req .Sessions {
165
- if err := s .store .SessionSetLastSeen (ctx , models .UID (uid )); err != nil {
166
- continue
135
+ if err := s .store .DeviceSave (ctx , device ); err != nil {
136
+ log .WithError (err ).Error ("failed to updated device to online" )
167
137
}
168
138
}
169
139
170
- dev , err := s .store .DeviceGetByUID (ctx , models .UID (device .UID ), device .TenantID )
140
+ claims := authorizer.DeviceClaims {UID : device .UID , TenantID : device .TenantID }
141
+ token , err := jwttoken .EncodeDeviceClaims (claims , s .privKey )
171
142
if err != nil {
172
- return nil , NewErrDeviceNotFound (models .UID (device .UID ), err )
173
- }
174
- if err := s .cache .Set (ctx , strings .Join ([]string {"auth_device" , key }, "/" ), & Device {Name : dev .Name , Namespace : namespace .Name }, time .Second * 30 ); err != nil {
175
- return nil , err
143
+ return nil , NewErrTokenSigned (err )
176
144
}
177
145
178
- return & models.DeviceAuthResponse {
179
- UID : key ,
180
- Token : token ,
181
- Name : dev .Name ,
182
- Namespace : namespace .Name ,
183
- }, nil
146
+ return & models.DeviceAuthResponse {UID : device .UID , Token : token , Name : device .Name , Namespace : "dev" }, nil
184
147
}
185
148
186
149
func (s * service ) AuthLocalUser (ctx context.Context , req * requests.AuthLocalUser , sourceIP string ) (* models.UserAuthResponse , int64 , string , error ) {
0 commit comments