fix bugs from todo except first critical
This commit is contained in:
+1
-1
@@ -84,7 +84,7 @@ func DbGetIdByClientName(ctx context.Context, name string) (uint32, error) {
|
|||||||
func DbSetClientByName(ctx context.Context, client *Client) error {
|
func DbSetClientByName(ctx context.Context, client *Client) error {
|
||||||
err := dbConn.QueryRow(ctx, `
|
err := dbConn.QueryRow(ctx, `
|
||||||
SELECT name, pass_hash, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1
|
SELECT name, pass_hash, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1
|
||||||
`, client.Name).Scan(&client.Name, &client.PasswordHash, client.Pronouns, client.Color[0], client.Color[1], client.Color[2], client.CreatedAt)
|
`, client.Name).Scan(&client.Name, &client.PasswordHash, &client.Pronouns, &client.Color[0], &client.Color[1], &client.Color[2], &client.CreatedAt)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isMethodAllowed(response *http.ResponseWriter, request *http.Request) bool {
|
func isMethodAllowed(response *http.ResponseWriter, request *http.Request) bool {
|
||||||
@@ -53,6 +55,7 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
|
|||||||
color, err := parseRgb(request.FormValue("color"))
|
color, err := parseRgb(request.FormValue("color"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "bad color", http.StatusBadRequest)
|
http.Error(response, "bad color", http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedPassword, err := PasswordHash(password)
|
hashedPassword, err := PasswordHash(password)
|
||||||
@@ -95,7 +98,9 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(username) < 8 {
|
password := request.FormValue("password")
|
||||||
|
|
||||||
|
if len(password) < 8 {
|
||||||
http.Error(response, "no or short password", http.StatusBadRequest)
|
http.Error(response, "no or short password", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -110,12 +115,18 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
err := DbSetClientByName(ctx, client)
|
err := DbSetClientByName(ctx, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "bad login", http.StatusBadRequest)
|
http.Error(response, "bad login", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
CacheSetClient(client)
|
CacheSetClient(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = bcrypt.CompareHashAndPassword([]byte(client.PasswordHash), []byte(password))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(response, "bad login", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
token, err := TokenCreate(client.Id)
|
token, err := TokenCreate(client.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||||
|
|||||||
+9
-5
@@ -20,13 +20,14 @@ func ServeWsConnection(responseWriter http.ResponseWriter, request *http.Request
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var client = Client{WsConn: connection}
|
var client = Client{WsConn: connection}
|
||||||
var isAuthenticated bool
|
var isAuthenticated bool
|
||||||
|
var ignoreCache bool
|
||||||
|
|
||||||
defer closeConnection(&client)
|
defer closeConnection(&client, ignoreCache)
|
||||||
for {
|
for {
|
||||||
var clientMessage map[string]any
|
var clientMessage map[string]any
|
||||||
err := wsjson.Read(ctx, connection, &clientMessage)
|
err := wsjson.Read(ctx, connection, &clientMessage)
|
||||||
@@ -42,6 +43,7 @@ func ServeWsConnection(responseWriter http.ResponseWriter, request *http.Request
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !handleUnauthenticatedMessage(&client, &clientMessage) {
|
if !handleUnauthenticatedMessage(&client, &clientMessage) {
|
||||||
|
ignoreCache = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
isAuthenticated = true
|
isAuthenticated = true
|
||||||
@@ -57,7 +59,7 @@ func sendMessageCloseIfTimeout(client *Client, message *map[string]any) {
|
|||||||
err := wsjson.Write(ctx, client.WsConn, message)
|
err := wsjson.Write(ctx, client.WsConn, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, context.DeadlineExceeded) {
|
if errors.Is(err, context.DeadlineExceeded) {
|
||||||
closeConnection(client)
|
closeConnection(client, false)
|
||||||
}
|
}
|
||||||
log.Printf("write error: %v", err)
|
log.Printf("write error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -111,7 +113,9 @@ func handleUnauthenticatedMessage(client *Client, clientMessage *map[string]any)
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeConnection(client *Client) {
|
func closeConnection(client *Client, ignoreCache bool) {
|
||||||
CacheDeleteClient(client.Id)
|
if !ignoreCache {
|
||||||
|
CacheDeleteClient(client.Id)
|
||||||
|
}
|
||||||
client.WsConn.CloseNow()
|
client.WsConn.CloseNow()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user