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
|
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
)
type UserCreds struct {
Username string `json:"username"`
Password string `json:"password"`
IsAdmin bool
}
type SessionStore struct {
sync.Mutex
sessions map[string]UserCreds // sessionID -> UserCreds
}
func NewSessionStore() *SessionStore {
return &SessionStore{sessions: make(map[string]UserCreds)}
}
type UserDB struct {
sync.Mutex
users map[string]UserCreds // username -> creds
}
func NewUserDB() *UserDB {
return &UserDB{users: make(map[string]UserCreds)}
}
type Auth struct {
AdminPassword string
Store *SessionStore
UserDB *UserDB
}
func (a *Auth) RegisterHandler(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
bodyStr := string(body)
if strings.Contains(bodyStr, "IsAdmin") {
http.Error(w, "not allowed!", http.StatusForbidden)
return
}
var c UserCreds
json.Unmarshal(body, &c)
if c.Username == "" || c.Password == "" {
http.Error(w, "username and password required", http.StatusBadRequest)
return
}
if c.Username == "admin" {
http.Error(w, "cannot register as admin", http.StatusForbidden)
return
}
a.UserDB.Lock()
defer a.UserDB.Unlock()
if _, exists := a.UserDB.users[c.Username]; exists {
http.Error(w, "username already exists", http.StatusConflict)
return
}
a.UserDB.users[c.Username] = c
w.Write([]byte("register success"))
}
func (a *Auth) LoginHandler(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
bodyStr := string(body)
if strings.Contains(bodyStr, "IsAdmin") {
http.Error(w, "not allowed!", http.StatusForbidden)
return
}
var c UserCreds
json.Unmarshal(body, &c)
a.UserDB.Lock()
user, ok := a.UserDB.users[c.Username]
a.UserDB.Unlock()
if ok && user.Password == c.Password {
if user.Username == "admin" && user.Password == a.AdminPassword {
user.IsAdmin = true
}
sessionID := GenRandomSeq(32)
a.Store.Lock()
a.Store.sessions[sessionID] = user
a.Store.Unlock()
http.SetCookie(w, &http.Cookie{Name: "session_id", Value: sessionID, Path: "/"})
fmt.Fprintf(w, "user %s logged in", user.Username)
return
}
http.Error(w, "invalid credentials", http.StatusUnauthorized)
}
func (a *Auth) LogoutHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session_id")
if err != nil {
http.Error(w, "no session, are you logged in?", http.StatusInternalServerError)
return
}
a.Store.Lock()
delete(a.Store.sessions, cookie.Value)
a.Store.Unlock()
w.Write([]byte("user logged out"))
}
func (a *Auth) RequireAdmin(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session_id")
if err != nil {
http.Error(w, "not logged in", http.StatusUnauthorized)
return
}
a.Store.Lock()
user, ok := a.Store.sessions[cookie.Value]
a.Store.Unlock()
if !ok || !user.IsAdmin {
http.Error(w, "admin only", http.StatusForbidden)
return
}
next(w, r)
}
}
|