-
Notifications
You must be signed in to change notification settings - Fork 22
/
auth.go
103 lines (83 loc) · 2.2 KB
/
auth.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
package ble
import (
"encoding/json"
"errors"
"github.com/digital-dream-labs/vector-bluetooth/rts"
)
// AuthStatus defines the status of the response message
type AuthStatus int
const (
UnknownError AuthStatus = iota
ConnectionError
WrongAccount
InvalidSessionToken
AuthorizedAsPrimary
AuthorizedAsSecondary
Reauthorized
)
// AuthResponse is the unified response for Auth messages
type AuthResponse struct {
Status AuthStatus `json:"status,omitempty"`
ClientTokenGUID string `json:"client_token_guid,omitempty"`
Success bool `json:"success,omitempty"`
}
// Marshal converts a AuthResponse message to bytes
func (sr *AuthResponse) Marshal() ([]byte, error) {
return json.Marshal(sr)
}
// Unmarshal converts a AuthResponse byte slice to a AuthResponse
func (sr *AuthResponse) Unmarshal(b []byte) error {
return json.Unmarshal(b, sr)
}
// Auth sends a Auth message to the vector robot
func (v *VectorBLE) Auth(key string) (*AuthResponse, error) {
msg, err := rts.BuildAuthMessage(v.ble.Version(), key)
if err != nil {
return nil, err
}
if err := v.ble.Send(msg); err != nil {
return nil, err
}
b, err := v.watch()
resp := AuthResponse{}
if err := resp.Unmarshal(b); err != nil {
return nil, err
}
if !resp.Success {
return nil, errors.New("authorization failed")
}
v.state.setClientGUID(resp.ClientTokenGUID)
return &resp, err
}
func handleRSTCloudSessionResponse(v *VectorBLE, msg interface{}) (data []byte, cont bool, err error) {
var m *rts.RtsCloudSessionResponse
switch v.ble.Version() {
case rtsV3:
t, ok := msg.(*rts.RtsConnection_3)
if !ok {
return handlerUnsupportedTypeError()
}
m = t.GetRtsCloudSessionResponse()
case rtsV4:
t, ok := msg.(*rts.RtsConnection_4)
if !ok {
return handlerUnsupportedTypeError()
}
m = t.GetRtsCloudSessionResponse()
case rtsV5:
t, ok := msg.(*rts.RtsConnection_5)
if !ok {
return handlerUnsupportedTypeError()
}
m = t.GetRtsCloudSessionResponse()
default:
return handlerUnsupportedVersionError()
}
resp := AuthResponse{
Status: AuthStatus(m.StatusCode),
ClientTokenGUID: m.ClientTokenGuid,
Success: m.Success,
}
b, err := resp.Marshal()
return b, false, err
}