-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (107 loc) · 2.88 KB
/
main.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
package main
import (
"fmt"
"net/http"
"time"
)
type Login struct {
HashedPassword string
SessionToken string
CSRFToken string
}
var users = map[string]Login{}
func main() {
http.HandleFunc("/register", register)
http.HandleFunc("/login", login)
http.HandleFunc("/logout", logout)
http.HandleFunc("/protected", protected)
http.ListenAndServe(":80", nil)
}
func register(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invaild method", http.StatusMethodNotAllowed)
return
}
username := r.FormValue("username")
password := r.FormValue("password")
if len(username) < 8 || len(password) < 8 {
http.Error(w, "Invalid username/password", http.StatusNotAcceptable)
return
}
if _, ok := users[username]; ok {
http.Error(w, "User already exists", http.StatusConflict)
return
}
hashedPassword, _ := hashPassword(password)
users[username] = Login{
HashedPassword: hashedPassword,
}
fmt.Fprintf(w, "User registered successfully!")
}
func login(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid Method", http.StatusMethodNotAllowed)
return
}
username := r.FormValue("username")
password := r.FormValue("password")
user, ok := users[username]
if !ok || !checkPasswordHach(password, user.HashedPassword) {
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
sessionToken := generateToken(32)
csrftToken := generateToken(32)
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Value: sessionToken,
Expires: time.Now().Add(24 * time.Hour),
HttpOnly: true,
})
http.SetCookie(w, &http.Cookie{
Name: "csrf_token",
Value: csrftToken,
Expires: time.Now().Add(24 * time.Hour),
HttpOnly: false,
})
user.SessionToken = sessionToken
user.CSRFToken = csrftToken
users[username] = user
fmt.Fprintf(w, "Login successful!")
}
func protected(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
if err := Authorize(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
username := r.FormValue("username")
fmt.Fprintf(w, "CSRF validation successful!, Welcome, %s", username)
}
func logout(w http.ResponseWriter, r *http.Request) {
if err := Authorize(r); err != nil {
http.Error(w, "Unauthorize", http.StatusUnauthorized)
return
}
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Value: "",
Expires: time.Now().Add(-time.Hour),
HttpOnly: true,
})
http.SetCookie(w, &http.Cookie{
Name: "csrf_token",
Value: "",
Expires: time.Now().Add(-time.Hour),
HttpOnly: false,
})
username := r.FormValue("username")
user := users[username]
user.SessionToken = ""
user.CSRFToken = ""
users[username] = user
fmt.Fprintf(w, "Logged out successfully!")
}