-
Notifications
You must be signed in to change notification settings - Fork 3
/
endpoints_auth.go
289 lines (277 loc) · 9.12 KB
/
endpoints_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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"log"
"net/http"
"os"
"time"
"github.com/retrixe/octyne/auth"
)
// GET /login
type loginEndpointResponse struct {
Token string `json:"token"`
}
func loginEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request) {
if r.RemoteAddr == "@" {
httpError(w, "Auth endpoints cannot be called over Unix socket!", http.StatusBadRequest)
return
}
// In case the username and password headers don't exist.
username := r.Header.Get("Username")
password := r.Header.Get("Password")
if username == "" || password == "" {
httpError(w, "Username or password not provided!", http.StatusBadRequest)
return
}
// Authorize the user.
token, err := connector.Login(username, password)
if err != nil {
log.Println("An error occurred when logging user in!", err)
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
} else if token == "" {
httpError(w, "Invalid username or password!", http.StatusUnauthorized)
return
}
connector.Info("auth.login", "ip", GetIP(r), "user", username)
// Set the authentication cookie, if requested.
if r.URL.Query().Get("cookie") == "true" {
http.SetCookie(w, &http.Cookie{
Name: "X-Authentication",
Value: token,
MaxAge: 60 * 60 * 24 * 31 * 3, // 3 months
// Allows HTTP usage. Strict SameSite will block sending cookie over HTTP when using HTTPS:
// https://web.dev/same-site-same-origin/
Secure: false,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
})
writeJsonStringRes(w, "{\"success\":true}")
return
}
// Send the response.
writeJsonStructRes(w, loginEndpointResponse{Token: token}) // skipcq GSC-G104
}
// GET /logout
func logoutEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request) {
if r.RemoteAddr == "@" {
httpError(w, "Auth endpoints cannot be called over Unix socket!", http.StatusBadRequest)
return
}
// Check with authenticator.
user := connector.Validate(w, r)
if user == "" {
return
}
token := auth.GetTokenFromRequest(r)
// Authorize the user.
success, err := connector.Logout(token)
if err != nil {
log.Println("An error occurred when logging out user!", err)
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
} else if !success {
httpError(w, "Invalid token, failed to logout!", http.StatusUnauthorized)
return
}
// Unset the authentication cookie.
http.SetCookie(w, &http.Cookie{
Name: "X-Authentication",
Value: "",
MaxAge: -1,
Secure: false,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
})
// Send the response.
connector.Info("auth.logout", "ip", GetIP(r), "user", user)
writeJsonStringRes(w, "{\"success\":true}")
}
// GET /ott
func ottEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request) {
if r.RemoteAddr == "@" {
httpError(w, "Auth endpoints cannot be called over Unix socket!", http.StatusBadRequest)
return
}
// Check with authenticator.
user := connector.Validate(w, r)
if user == "" {
return
}
token := auth.GetTokenFromRequest(r)
// Add a ticket.
ticket := make([]byte, 4)
rand.Read(ticket) // Tolerate errors here, an error here is incredibly unlikely: skipcq GSC-G104
ticketString := base64.StdEncoding.EncodeToString(ticket)
connector.Tickets.Store(ticketString, Ticket{
Time: time.Now().Unix(),
User: user,
Token: token,
IPAddr: GetIP(r),
})
// Schedule deletion (cancellable).
go (func() {
<-time.After(2 * time.Minute)
connector.Tickets.Delete(ticketString)
})()
// Send the response.
writeJsonStringRes(w, "{\"ticket\": \""+ticketString+"\"}")
}
// GET /accounts
// POST /accounts
// PATCH /accounts?username=username
// DELETE /accounts?username=username
type accountsRequestBody struct {
Username string `json:"username"`
Password string `json:"password"`
}
func accountsEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request) {
user := connector.Validate(w, r)
if user == "" {
return
} else if r.Method != "GET" && r.Method != "POST" && r.Method != "PATCH" && r.Method != "DELETE" {
httpError(w, "Only GET, POST, PATCH and DELETE are allowed!", http.StatusMethodNotAllowed)
return
}
var users map[string]string
contents, err := os.ReadFile(UsersJsonPath)
if err != nil {
log.Println("Error reading "+UsersJsonPath+" when modifying accounts!", err)
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
}
err = json.Unmarshal(contents, &users)
if err != nil {
log.Println("Error parsing "+UsersJsonPath+" when modifying accounts!", err)
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
}
if r.Method == "GET" {
accountsEndpointGet(w, users)
return
} else if r.Method == "POST" && !accountsEndpointPost(connector, w, r, users, user) {
return
} else if r.Method == "PATCH" && !accountsEndpointPatch(connector, w, r, users, user) {
return
} else if r.Method == "DELETE" && !accountsEndpointDelete(connector, w, r, users, user) {
return
}
usersJson, err := json.MarshalIndent(users, "", " ")
if err != nil {
log.Println("Error serialising " + UsersJsonPath + " when modifying accounts!")
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
}
err = os.WriteFile(UsersJsonPath+"~", []byte(string(usersJson)+"\n"), 0666)
if err != nil {
log.Println("Error writing to " + UsersJsonPath + " when modifying accounts!")
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
}
err = os.Rename(UsersJsonPath+"~", UsersJsonPath)
if err != nil {
log.Println("Error writing to " + UsersJsonPath + " when modifying accounts!")
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
}
writeJsonStringRes(w, "{\"success\":true}")
}
func accountsEndpointGet(w http.ResponseWriter, users map[string]string) {
var usernames []string
for username := range users {
usernames = append(usernames, username)
}
usernamesJson, err := json.Marshal(usernames)
if err != nil {
log.Println("Error serialising usernames when listing accounts!")
httpError(w, "Internal Server Error!", http.StatusInternalServerError)
return
}
writeJsonStringRes(w, string(usernamesJson))
}
func accountsEndpointPost(connector *Connector, w http.ResponseWriter, r *http.Request,
users map[string]string, user string) bool {
var buffer bytes.Buffer
_, err := buffer.ReadFrom(r.Body)
if err != nil {
httpError(w, "Failed to read body!", http.StatusBadRequest)
return false
}
var body accountsRequestBody
err = json.Unmarshal(buffer.Bytes(), &body)
if err != nil {
httpError(w, "Invalid JSON body!", http.StatusBadRequest)
return false
} else if body.Username == "" || body.Password == "" {
httpError(w, "Username or password not provided!", http.StatusBadRequest)
return false
} else if users[body.Username] != "" {
httpError(w, "User already exists!", http.StatusConflict)
return false
}
hash := auth.HashPassword(body.Password)
connector.Info("accounts.create", "ip", GetIP(r), "user", user, "newUser", body.Username)
users[body.Username] = hash
return true
}
func accountsEndpointPatch(connector *Connector, w http.ResponseWriter, r *http.Request,
users map[string]string, user string) bool {
username := r.URL.Query().Get("username")
var buffer bytes.Buffer
_, err := buffer.ReadFrom(r.Body)
if err != nil {
httpError(w, "Failed to read body!", http.StatusBadRequest)
return false
}
var body accountsRequestBody
err = json.Unmarshal(buffer.Bytes(), &body)
if username == "" { // Legacy compat with older API, assume body.Username, fix in 2.0
username = body.Username
}
toUpdateUsername := body.Username != username && body.Username != ""
if err != nil {
httpError(w, "Invalid JSON body!", http.StatusBadRequest)
return false
} else if username == "" || (body.Password == "" && !toUpdateUsername) {
httpError(w, "Username or password not provided!", http.StatusBadRequest)
return false
} else if users[username] == "" {
httpError(w, "User does not exist!", http.StatusNotFound)
return false
} else if toUpdateUsername && users[body.Username] != "" {
httpError(w, "User already exists!", http.StatusConflict)
return false
}
hash := users[username]
if body.Password != "" {
hash = auth.HashPassword(body.Password)
}
if toUpdateUsername {
connector.Info("accounts.update", "ip", GetIP(r), "user", user,
"updatedUser", body.Username, "oldUsername", username, "changedPassword", body.Password != "")
delete(users, username)
users[body.Username] = hash
} else {
connector.Info("accounts.update", "ip", GetIP(r), "user", user,
"updatedUser", username, "changedPassword", true)
users[username] = hash
}
return true
}
func accountsEndpointDelete(connector *Connector, w http.ResponseWriter, r *http.Request,
users map[string]string, user string) bool {
username := r.URL.Query().Get("username")
if username == "" {
httpError(w, "Username not provided!", http.StatusBadRequest)
return false
} else if users[username] == "" {
httpError(w, "User does not exist!", http.StatusNotFound)
return false
}
connector.Info("accounts.delete", "ip", GetIP(r), "user", user, "deletedUser", username)
delete(users, username)
return true
}