-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttpFuncs.go
193 lines (157 loc) · 4.34 KB
/
httpFuncs.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
package main
import (
"fmt"
"net/http"
"time"
"web-chat-go/hub"
"web-chat-go/user"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
// serveWs will instantiate a new "hub.Client" pointer and pass it as an
// argument in "hub.ServeClientWs".
func serveWs(w http.ResponseWriter, r *http.Request) {
u := getUser(w, r)
clientWS := &hub.ClientWS{
User: &u,
Hub: clientsHub,
Send: make(chan hub.ClientMessage),
}
hub.ServeClientWs(clientsHub, clientWS, w, r)
}
func index(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed.", http.StatusMethodNotAllowed)
return
}
tpl.ExecuteTemplate(w, "index.gohtml", getUser(w, r))
}
// signUp will execute the given template and save the data that the user
// posted through the form displayed, encrypting the password with an added
// salt and the "bcrypt" package. signUp will redirect to "/" if
// "alreadyLoggedIn" returns true.
func signUp(w http.ResponseWriter, r *http.Request) {
if alreadyLoggedIn(w, r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
if r.Method == http.MethodPost {
un := r.FormValue("username")
pw := r.FormValue("password")
fn := r.FormValue("firstname")
ln := r.FormValue("lastname")
// More validations would be appropriate.
if un == "" || pw == "" || fn == "" || ln == "" {
http.Error(w,
"Please fill all fields before proceeding through the SignUp.",
http.StatusBadRequest)
return
}
isSigned := user.IsSigned(user.SearchUser(dbConn, un))
if isSigned {
http.Error(w,
"The submitted username is already in use.",
http.StatusForbidden)
return
}
saltPass := uuid.NewString()
// Encrypting password with bcrypt.
sb, err := bcrypt.GenerateFromPassword(
[]byte(saltPass+pw),
bcrypt.DefaultCost,
)
if err != nil {
http.Error(w,
"Internal Server Error",
http.StatusInternalServerError)
// Put panic instead of return since this error might not be very
// clear so panic will help more in debugging.
panic(err)
}
u := user.User{
UserName: un,
SaltPass: saltPass,
Password: sb,
FirstName: fn,
LastName: ln,
}
err = user.AddUser(dbConn, u)
if err != nil {
http.Error(w,
"Internal Server Error",
http.StatusInternalServerError)
panic(err)
}
c := setCookie(w)
dbSessions[c.Value] = session{
un: un,
lastActivity: time.Now(),
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(w, "signup.gohtml", nil)
}
// login will execute the given template and receive the information passed
// from the form displayed and check if it is in our database of users.
// If it is, the function will create a session and assign it to that user.
// If it is not, then the user will be redirected to "/".
func login(w http.ResponseWriter, r *http.Request) {
if alreadyLoggedIn(w, r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
if r.Method == http.MethodPost {
un := r.FormValue("username")
pw := r.FormValue("password")
// More validations would be appropriate.
if un == "" || pw == "" {
http.Error(w,
"Please fill all fields before proceeding through the LogIn.",
http.StatusBadRequest)
return
}
u, err := user.SearchUser(dbConn, un)
if err != nil {
fmt.Println(err.Error())
http.Error(w,
"Incorrect Username or Password.",
http.StatusForbidden)
return
}
err = bcrypt.CompareHashAndPassword(
u.Password,
[]byte(u.SaltPass+pw),
)
if err != nil {
http.Error(w,
"Incorrect Username or Password.",
http.StatusForbidden)
return
}
c := setCookie(w)
dbSessions[c.Value] = session{
un: un,
lastActivity: time.Now(),
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(w, "login.gohtml", nil)
}
// logout will check that the user is logged in and remove their session and
// record from "dbSessions". If the user isn't logged in or the cookie isn't
// found it will redirect to "/".
func logout(w http.ResponseWriter, r *http.Request) {
if !alreadyLoggedIn(w, r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
// err is thrown because it is already checked in "alreadyLoggedIn".
c, _ := r.Cookie("session")
c.MaxAge = -1
c.Path = "/"
http.SetCookie(w, c)
delete(dbSessions, c.Value)
http.Redirect(w, r, "/", http.StatusSeeOther)
}