mirror of
https://github.com/originalmk/archat-server.git
synced 2025-01-18 16:29:17 +00:00
Passwords are now stored as bcrypt hashes
This commit is contained in:
parent
90b837fe5c
commit
fffd687641
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
|||||||
module krzyzanowski.dev/p2pchat
|
module krzyzanowski.dev/p2pchat
|
||||||
|
|
||||||
go 1.21.7
|
go 1.21.7
|
||||||
|
|
||||||
|
require golang.org/x/crypto v0.21.0
|
||||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||||
|
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
30
main.go
30
main.go
@ -9,11 +9,13 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
nickname string
|
nickname string
|
||||||
password string
|
passHash []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerContext struct {
|
type ServerContext struct {
|
||||||
@ -169,7 +171,6 @@ func handleEcho(_ *HandlerContext, reqBytes []byte) (resBytes []byte, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleListPeers(handlerCtx *HandlerContext, reqBytes []byte) (resBytes []byte, err error) {
|
func handleListPeers(handlerCtx *HandlerContext, reqBytes []byte) (resBytes []byte, err error) {
|
||||||
// For the sake of conciseness -> currently unmarshalling empty slice to empty struct
|
|
||||||
var listPeersReq ListPeersRequest
|
var listPeersReq ListPeersRequest
|
||||||
err = json.Unmarshal(reqBytes, &listPeersReq)
|
err = json.Unmarshal(reqBytes, &listPeersReq)
|
||||||
|
|
||||||
@ -215,7 +216,7 @@ func handleAuth(handlerCtx *HandlerContext, reqBytes []byte) (resBytes []byte, e
|
|||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
// Check if password matches
|
// Check if password matches
|
||||||
if authReq.Password == account.password {
|
if bcrypt.CompareHashAndPassword(account.passHash, []byte(authReq.Password)) == nil {
|
||||||
authRes = AuthResponse{true}
|
authRes = AuthResponse{true}
|
||||||
handlerCtx.srvCtx.peersListLock.Lock()
|
handlerCtx.srvCtx.peersListLock.Lock()
|
||||||
handlerCtx.peer.hasAccount = true
|
handlerCtx.peer.hasAccount = true
|
||||||
@ -226,14 +227,20 @@ func handleAuth(handlerCtx *HandlerContext, reqBytes []byte) (resBytes []byte, e
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
authRes = AuthResponse{true}
|
authRes = AuthResponse{true}
|
||||||
newAcc := Account{authReq.Nickname, authReq.Password}
|
passHash, err := bcrypt.GenerateFromPassword([]byte(authReq.Password), bcrypt.DefaultCost)
|
||||||
handlerCtx.srvCtx.accountsLock.Lock()
|
|
||||||
handlerCtx.srvCtx.accounts[newAcc.nickname] = &newAcc
|
if err != nil {
|
||||||
handlerCtx.srvCtx.accountsLock.Unlock()
|
authRes = AuthResponse{false}
|
||||||
handlerCtx.srvCtx.peersListLock.Lock()
|
} else {
|
||||||
handlerCtx.peer.hasAccount = true
|
newAcc := Account{authReq.Nickname, passHash}
|
||||||
handlerCtx.peer.account = &newAcc
|
handlerCtx.srvCtx.accountsLock.Lock()
|
||||||
handlerCtx.srvCtx.peersListLock.Unlock()
|
handlerCtx.srvCtx.accounts[newAcc.nickname] = &newAcc
|
||||||
|
handlerCtx.srvCtx.accountsLock.Unlock()
|
||||||
|
handlerCtx.srvCtx.peersListLock.Lock()
|
||||||
|
handlerCtx.peer.hasAccount = true
|
||||||
|
handlerCtx.peer.account = &newAcc
|
||||||
|
handlerCtx.srvCtx.peersListLock.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resBytes, err = json.Marshal(authRes)
|
resBytes, err = json.Marshal(authRes)
|
||||||
@ -265,7 +272,6 @@ func printConnectedPeers(srvCtx *ServerContext) {
|
|||||||
func runServer() {
|
func runServer() {
|
||||||
idCounter := 0
|
idCounter := 0
|
||||||
srvCtx := &ServerContext{peersList: make([]*Peer, 0), accounts: make(map[string]*Account)}
|
srvCtx := &ServerContext{peersList: make([]*Peer, 0), accounts: make(map[string]*Account)}
|
||||||
srvCtx.accounts["xd"] = &Account{"xd", "XD"}
|
|
||||||
ln, err := net.Listen("tcp", ":8080")
|
ln, err := net.Listen("tcp", ":8080")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user