-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers_auth.go
189 lines (161 loc) · 5.04 KB
/
handlers_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
package main
import (
"encoding/json"
"fmt"
"github.com/alvanrahimli/dots-server/dataaccess"
"github.com/alvanrahimli/dots-server/models"
"github.com/alvanrahimli/dots-server/utils"
"net/http"
"time"
)
func registerHandler(w http.ResponseWriter, r *http.Request) {
InfoLogger.Printf("Request received for URL %s", r.URL)
if err := r.ParseForm(); err != nil {
_, err := fmt.Fprintf(w, "ParseForm() err: %v", err)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
ErrLogger.Println(err.Error())
return
}
}
username := r.FormValue("name")
password := r.FormValue("password")
email := r.FormValue("email")
nameValidationResult, nameValidated := utils.ValidateName(username)
passValidationResult, passValidated := utils.ValidatePassword(password)
emailValidationResult, emailValidated := utils.ValidateEmail(email)
response := models.NewHttpResponse()
if !nameValidated || !passValidated || !emailValidated {
w.WriteHeader(http.StatusBadRequest)
response.Code = 1
response.Message = "Validation error occurred"
response.Data = map[string]string{
"name": nameValidationResult,
"password": passValidationResult,
"email": emailValidationResult,
}
} else {
// Register new user
db := getDbInstance()
//goland:noinspection GoUnhandledErrorResult
defer db.Close()
insertUserRaw := `INSERT INTO Users (Username, Email, Password) VALUES (?, ?, ?)`
statement, stmtErr := db.Prepare(insertUserRaw)
if stmtErr != nil {
ErrLogger.Println(stmtErr.Error())
http.Error(w, stmtErr.Error(), http.StatusInternalServerError)
return
}
_, dbErr := statement.Exec(username, email, utils.HashPassword(password))
if dbErr != nil {
ErrLogger.Println(dbErr.Error())
http.Error(w, dbErr.Error(), http.StatusInternalServerError)
return
}
response.Code = 0
response.Message = fmt.Sprintf("User created with name '%s'", username)
response.Data = map[string]string{
"username": username,
"email": email,
}
}
responseJson, jsonErr := json.Marshal(response)
if jsonErr != nil {
ErrLogger.Println(jsonErr.Error())
http.Error(w, jsonErr.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if response.Code == 0 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
}
if _, err := w.Write(responseJson); err != nil {
ErrLogger.Println(err.Error())
return
}
InfoLogger.Println("Request finished")
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
InfoLogger.Printf("Request received for URL %s", r.URL)
// Parse form
if err := r.ParseForm(); err != nil {
_, err := fmt.Fprintf(w, "ParseForm() err: %v", err)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
ErrLogger.Println(err.Error())
return
}
}
email := r.FormValue("email")
password := r.FormValue("password")
db := getDbInstance()
//goland:noinspection GoUnhandledErrorResult
defer db.Close()
// Get user by email
user, getUserErr := dataaccess.FindUserByEmail(db, email)
if getUserErr != nil {
ErrLogger.Println(getUserErr.Error())
http.Error(w, fmt.Sprintf("Could not find user with email '%s'", email), http.StatusNotFound)
return
}
// Check user's password
if user.Password != utils.HashPassword(password) {
WarnLogger.Printf("User '%s' entered invalid password", email)
http.Error(w, fmt.Sprintf("User '%s' entered invalid password", email), http.StatusUnauthorized)
return
}
// Delete existing tokens
deleteOldTokensQuery := `DELETE FROM AuthTokens WHERE UserId = ?`
statement, stmtErr := db.Prepare(deleteOldTokensQuery)
if stmtErr != nil {
ErrLogger.Println(stmtErr.Error())
http.Error(w, stmtErr.Error(), http.StatusInternalServerError)
return
}
_, execErr := statement.Exec(user.Id)
if execErr != nil {
ErrLogger.Println(execErr.Error())
http.Error(w, "Could not delete existing tokens", http.StatusInternalServerError)
return
}
// Insert new token
token := models.AuthToken{
Id: 0,
UserId: user.Id,
Token: utils.GenerateToken(),
ExpirationDate: time.Now().Add(time.Hour * 24 * 31).Format(models.DatetimeLayout), // a month,
}
tokenErr := dataaccess.AddToken(&token, db)
if tokenErr != nil {
// TODO: token != nil
}
userDto := map[string]string{
"username": user.Username,
"email": user.Email,
}
response := models.HttpResponse{
Code: 0,
Message: "User authenticated successfully",
Data: map[string]interface{}{
"token": token.Token,
"expiration_date": token.ExpirationDate,
"user": userDto,
},
}
responseJson, jsonErr := json.Marshal(response)
if jsonErr != nil {
ErrLogger.Println(jsonErr.Error())
http.Error(w, "Could not marshal response dto", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, writeErr := w.Write(responseJson)
if writeErr != nil {
ErrLogger.Println(writeErr.Error())
http.Error(w, "Could not write response", http.StatusInternalServerError)
return
}
}