This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
275 lines (254 loc) · 7.61 KB
/
init.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
package sdk
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/Masterminds/semver"
)
// Is sent by the server when token-login is used
// Required for setting the username when token authentication is used
type tokenLoginResponse struct {
Username string `json:"username"`
TokenLabel string `json:"tokenLabel"`
}
// Creates a new connection
// First argument specifies the base URL of the target Smarthome-server
// Second argument specifies how to handle authentication
func NewConnection(
smarthomeURL string,
authMethod AuthMethod,
) (*Connection, error) {
u, err := url.Parse(smarthomeURL)
if err != nil {
return nil, ErrInvalidURL
}
// Create and return a client
return &Connection{
SmarthomeURL: u,
authMethod: authMethod,
sessionCookie: &http.Cookie{},
}, nil
}
// Can be used to connect when the authentication method is set to `None`
func (c *Connection) Connect() error {
if c.authMethod != AuthMethodNone {
return ErrInvalidFunctionAuthMethod
}
// Call the helper function
return c.connectHelper()
}
// Can be used to connect when the authentication method is set to `Password-XXX`
func (c *Connection) UserLogin(username string, password string) error {
if c.authMethod != AuthMethodQueryPassword && c.authMethod != AuthMethodCookiePassword {
return ErrInvalidFunctionAuthMethod
}
// Set the internal credentials using the parameters
c.credStore.Username = username
c.credStore.Password = password
// Call the helper function
return c.connectHelper()
}
// Can be used to connect when the authentication method is set to `Token-XXX`
func (c *Connection) TokenLogin(token string) error {
if c.authMethod != AuthMethodQueryToken && c.authMethod != AuthMethodCookieToken {
return ErrInvalidFunctionAuthMethod
}
// Set the internal token to the parameter
c.credStore.Token = token
// Call the helper function
return c.connectHelper()
}
// If the authentication mode is set to `AuthMethodNone`, both arguments can be set to nil
// Otherwise, username and password are required to login
func (c *Connection) connectHelper() error {
// Retrieve the server's version
version, err := c.Version()
if err != nil {
return err
}
// Set the version in the connection
// Is already set here so it can be used in error messages as `c.SmarthomeVersion`
c.SmarthomeVersion = version.Version
c.SmarthomeGoVersion = version.GoVersion
// Check Smarthome version compatibility
supportedV, err := semver.NewConstraint(fmt.Sprintf("^%s", MinSmarthomeVersion))
if err != nil {
// This must not happen (tests)
// If this happens, the best thing is to abort the connection
return ErrInvalidVersion
}
currentV, err := semver.NewVersion(version.Version)
if err != nil {
// This must also not happen
// If this happens, the best thing is to abort the connection
return ErrInvalidVersion
}
// Perform the version comparison
if !supportedV.Check(currentV) {
// Would not be supported
return ErrUnsupportedVersion
}
switch c.authMethod {
// If the connection does not use authentication, it can be marked as ready
case AuthMethodNone:
c.ready = true
return nil
// If the authentication mode is set to `AuthMethodQueryToken`, validate the token and mark the connection as ready
case AuthMethodQueryToken:
_, tokenData, err := c.doLogin()
if err != nil {
return err
}
c.tokenClientName = tokenData.TokenLabel
c.credStore.Username = tokenData.Username
c.ready = true
return nil
// If the authentication mode is set to `AuthMethodQueryPassword`, validate the user's credentials and mark the connection as ready
case AuthMethodQueryPassword:
_, _, err := c.doLogin()
if err != nil {
return err
}
c.ready = true
return nil
// If the authentication mode is set to `AuthMethodCookieToken`, use the token to obtain a session cookie
case AuthMethodCookieToken:
cookie, tokenData, err := c.doLogin()
if err != nil {
return err
}
c.tokenClientName = tokenData.TokenLabel
c.credStore.Username = tokenData.Username
c.sessionCookie = cookie
c.ready = true
return nil
// If the authentication mode is set to `AuthMethodCookiePassword`, use the user's credentials to obtain a session cookie
case AuthMethodCookiePassword:
cookie, _, err := c.doLogin()
if err != nil {
return err
}
c.sessionCookie = cookie
c.ready = true
return nil
default:
panic("unreachable")
}
}
// Used internally to send a login request
// When the authentication mode is set to `AuthMethodCookie-XXX`, the response cookie is saved
// However, for `AuthMethodQuery-XXX`, it serves the purpose of validating the provided credentials beforehand
// If the authentication mode is sey set to `AuthMethodNone`, the function call is omitted
func (c *Connection) doLogin() (
*http.Cookie,
*tokenLoginResponse,
error,
) {
u := c.SmarthomeURL
// The default path is the user login
u.Path = "/api/login"
// If authentication should use a token, change the path
if c.authMethod == AuthMethodQueryToken || c.authMethod == AuthMethodCookieToken {
u.Path = "/api/login/token"
}
var loginBody []byte
var loginBodyErr error
if c.authMethod == AuthMethodQueryPassword || c.authMethod == AuthMethodCookiePassword {
loginBody, loginBodyErr = json.Marshal(struct {
Username string `json:"username"`
Password string `json:"password"`
}{
Username: c.credStore.Username,
Password: c.credStore.Password,
})
if loginBodyErr != nil {
return nil, nil, loginBodyErr
}
} else if c.authMethod == AuthMethodQueryToken || c.authMethod == AuthMethodCookieToken {
loginBody, loginBodyErr = json.Marshal(struct {
Token string `json:"token"`
}{
Token: c.credStore.Token,
})
if loginBodyErr != nil {
return nil, nil, loginBodyErr
}
} else {
panic("unreachable")
}
// Create a login request
r, err := http.NewRequest(
http.MethodPost,
u.String(),
bytes.NewBuffer(loginBody),
)
if err != nil {
return nil, nil, err
}
// Perform the login request
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
return nil, nil, err
}
defer res.Body.Close()
switch res.StatusCode {
case 200:
if c.authMethod == AuthMethodQueryPassword || c.authMethod == AuthMethodCookiePassword {
// should not happen: this is a bug
panic("unreachable")
}
// Attempt to decode the response body
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, nil, ErrReadResponseBody
}
var parsedBody tokenLoginResponse
if err := json.Unmarshal(resBody, &parsedBody); err != nil {
return nil, nil, ErrReadResponseBody
}
var returnCookie *http.Cookie
for _, cookie := range res.Cookies() {
if cookie.Name == "session" {
returnCookie = cookie
break
}
}
if returnCookie == nil {
return nil, nil, ErrNoCookiesSent
}
return returnCookie, &parsedBody, nil
case 204:
for _, cookie := range res.Cookies() {
if cookie.Name == "session" {
return cookie, nil, nil
}
}
return nil, nil, ErrNoCookiesSent
case 401:
return nil, nil, ErrInvalidCredentials
case 500:
return nil, nil, ErrInternalServerError
case 503:
return nil, nil, ErrServiceUnavailable
default:
return nil, nil, fmt.Errorf("unknown response code: %s", res.Status)
}
}
// Works on every authentication method except `None`
func (c *Connection) GetUsername() (string, error) {
if c.authMethod == AuthMethodNone {
return "", ErrInvalidFunctionAuthMethod
}
return c.credStore.Username, nil
}
// Only works on token-based authentication methods
func (c *Connection) GetTokenClientLabel() (string, error) {
if c.authMethod != AuthMethodQueryToken && c.authMethod != AuthMethodCookieToken {
return "", ErrInvalidFunctionAuthMethod
}
return c.tokenClientName, nil
}