-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
277 lines (234 loc) · 6.27 KB
/
auth.go
File metadata and controls
277 lines (234 loc) · 6.27 KB
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
// summit backend/auth.go - handles authentication and sessions
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/user"
"slices"
"strconv"
"sync"
"time"
)
// the maximum time a session can last
const A_SessionExpireTime = 4 * time.Hour
// a user session
type A_Session struct {
user string // username
gid uint32 // unix gid
uid uint32 // unix uid
suppgids []uint32 // supplementary unix gids
configFile string // configuration file path
config map[string]any // cached config data
homedir string // home directory
ua string // user agent
ip string // ip
expires time.Time // time when this user's login expires
}
var (
// mutex for A_Sessions
A_SessionsMutex sync.RWMutex
// stores all valid sessions
A_Sessions = make(map[string]A_Session)
)
// handles /api/login
func REST_Login(w http.ResponseWriter, r *http.Request) {
// only allow post
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
// get login data
if err := r.ParseForm(); err != nil {
H_ISE(w, "REST_Login: Couldn't parse login", err)
return
}
// disallow root
if r.FormValue("username") == "root" {
http.Redirect(w, r, "/?err=root", http.StatusFound)
return
}
// log in with PAM
if err := H_PamAuth("passwd", r.FormValue("username"), r.FormValue("password")); err != nil {
http.Redirect(w, r, "/?err=auth", http.StatusFound)
return
}
if !A_Authenticated(w, r) {
// generate id & expire time
id, err := H_RandomBase64(32)
if err != nil {
H_ISE(w, "REST_Login: Couldn't generate login id", err)
return
}
expires := time.Now().Add(A_SessionExpireTime)
// lookup user in the system
u, err := user.Lookup(r.FormValue("username"))
if err != nil {
H_ISE(w, "REST_Login: Couldn't lookup username", err)
return
}
uid, gid, groups, err := A_GetUnixInfo(u)
if err != nil {
H_ISE(w, "A_GetUnixInfo", err)
return
}
// config logic
var configData map[string]any
configFile := u.HomeDir + "/.config/summit.json"
if _, err := os.Stat(configFile); os.IsNotExist(err) {
if err = C_Create(configFile, uid, gid); err != nil {
H_ISE(w, "REST_Login: Couldn't create configuration file", err)
return
}
}
config, err := os.ReadFile(configFile)
if err != nil {
H_ISE(w, "REST_Login: Couldn't read config file", err)
return
}
if err := json.Unmarshal(config, &configData); err != nil {
H_ISE(w, "REST_Login: Couldn't parse config file", err)
return
}
if err := A_ChangeAvailablePages(configData); err != nil {
H_ISE(w, "REST_Login: Couldn't update available pages", err)
return
}
// create auth, server-side
A_SessionsMutex.Lock()
A_Sessions[id] = A_Session{
ua: r.UserAgent(),
user: u.Username,
uid: uint32(uid),
gid: uint32(gid),
suppgids: groups,
configFile: configFile,
config: configData,
homedir: u.HomeDir,
ip: H_ClientIP(r),
expires: expires,
}
A_SessionsMutex.Unlock()
// create auth, browser-side
http.SetCookie(w, &http.Cookie{
Name: "s",
Value: id,
Path: "/",
SameSite: http.SameSiteStrictMode,
Expires: expires,
Secure: true,
HttpOnly: true,
})
log.Println("REST_Login: Authenticated a client from", H_ClientIP(r))
}
http.Redirect(w, r, "/terminal", http.StatusFound)
}
// handles /api/logout
func REST_Logout(w http.ResponseWriter, r *http.Request) {
// only allow get
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
if A_Authenticated(w, r) {
s, err := r.Cookie("s")
if err != nil {
H_ISE(w, "REST_Logout: Couldn't get session cookie", err)
return
}
A_Remove(s.Value, w)
}
http.Redirect(w, r, "/", http.StatusFound)
}
// gets uid, gid, and associated groups from a user
func A_GetUnixInfo(u *user.User) (uint32, uint32, []uint32, error) {
// get uid
uid, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return 0, 0, nil, err
}
// get gid
gid, err := strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
return 0, 0, nil, err
}
// get group ids
stringGroups, err := u.GroupIds()
if err != nil {
return 0, 0, nil, err
}
var groups []uint32 = make([]uint32, len(stringGroups))
// convert group ids to uint32, because somebody thought a fucking STRING is the best way.
for i, gidStr := range stringGroups {
gidInt, err := strconv.Atoi(gidStr)
if err != nil {
return 0, 0, nil, err
}
groups[i] = uint32(gidInt)
}
return uint32(uid), uint32(gid), groups, nil
}
// deletes authentication, both server and browser side.
func A_Remove(id string, w http.ResponseWriter) {
// server-side
A_SessionsMutex.Lock()
defer A_SessionsMutex.Unlock()
delete(A_Sessions, id)
// browser-side
http.SetCookie(w, &http.Cookie{
Name: "s",
Value: "",
MaxAge: -1,
Expires: time.Unix(0, 0),
SameSite: http.SameSiteStrictMode,
})
}
// deletes expired sessions every 10 minutes
func A_RemoveExpiredSessions() {
log.Println("A_RemoveExpiredSessions: Start session auto-remove task.")
for {
time.Sleep(10 * time.Minute)
removed := 0
A_SessionsMutex.Lock()
for id, v := range A_Sessions {
if v.expires.Before(time.Now()) {
delete(A_Sessions, id)
removed++
}
}
A_SessionsMutex.Unlock()
if removed > 0 {
log.Printf("A_RemoveExpiredSessions: Removed %d sessions.\n", removed)
}
}
}
// checks if user is authenticated
func A_Authenticated(w http.ResponseWriter, r *http.Request) bool {
s, err := r.Cookie("s")
if err != nil {
return false
}
A_SessionsMutex.RLock()
v, ok := A_Sessions[s.Value]
A_SessionsMutex.RUnlock()
if !ok || v.ua != r.UserAgent() || v.ip != H_ClientIP(r) || v.expires.Before(time.Now()) || len(s.Value) != 32 {
A_Remove(s.Value, w)
return false
}
return true
}
func A_ChangeAvailablePages(configData map[string]any) error {
raw, err := IT_RawValue(configData, "ui.pages")
if err != nil {
return err
}
val, ok := raw.([]any)
if !ok {
return fmt.Errorf("ui.pages isn't a list")
}
return IT_Set(configData, "ui.pages", slices.DeleteFunc(val, func(e any) bool {
return (e == "services" && S_SrvMgr.IsStub()) || (e == "updates" && U_PkgMgr.IsStub())
}))
}