summaryrefslogtreecommitdiff
path: root/server/user/user.go
diff options
context:
space:
mode:
Diffstat (limited to 'server/user/user.go')
-rw-r--r--server/user/user.go51
1 files changed, 49 insertions, 2 deletions
diff --git a/server/user/user.go b/server/user/user.go
index 311a1b8..d2d5724 100644
--- a/server/user/user.go
+++ b/server/user/user.go
@@ -4,6 +4,7 @@ import (
"citrons.xyz/talk/server/object"
"citrons.xyz/talk/server/session"
"citrons.xyz/talk/server/validate"
+ "citrons.xyz/talk/server/passwords"
"citrons.xyz/talk/proto"
bolt "go.etcd.io/bbolt"
"log"
@@ -30,9 +31,16 @@ func Kind(world *object.World) *UserKind {
}
func (us *UserKind) CreateUser(name string) (*User, *proto.Fail) {
- if us.ByName(name) != nil {
+ existing := us.ByName(name)
+ if existing != nil {
+ anon := "no"
+ if existing.anonymous {
+ anon = "yes"
+ }
return nil, &proto.Fail {
- "name-taken", "", map[string]string {"": name},
+ "name-taken", "", map[string]string {
+ "": name, "anonymous": anon, "id": existing.Id(),
+ },
}
}
if !validate.Name(name) {
@@ -157,6 +165,8 @@ func (u *User) Delete() {
anons.Delete([]byte(u.id))
channels, _ := tx.CreateBucketIfNotExists([]byte("user channels"))
channels.DeleteBucket([]byte(u.id))
+ auth, _ := tx.CreateBucketIfNotExists([]byte("auth"))
+ auth.DeleteBucket([]byte(u.id))
return nil
})
if err != nil {
@@ -180,3 +190,40 @@ func (u *User) InfoFor(uid string) proto.Object {
func (u *User) IsAnonymous() bool {
return u.anonymous
}
+
+func (u *User) CheckPassword(password string) bool {
+ var hash []byte
+ err := u.kind.db.View(func(tx *bolt.Tx) error {
+ auth := tx.Bucket([]byte("auth"))
+ if auth == nil {
+ return nil
+ }
+ userData := auth.Bucket([]byte(u.id))
+ if userData == nil {
+ return nil
+ }
+ hash = userData.Get([]byte("password hash"))
+ return nil
+ })
+ if err != nil {
+ log.Fatal("error reading database: ", err)
+ }
+ if hash == nil {
+ return false
+ }
+ return passwords.Check(password, hash)
+}
+
+func (u *User) SetPassword(password string) {
+ err := u.kind.db.Update(func(tx *bolt.Tx) error {
+ auth, _ := tx.CreateBucketIfNotExists([]byte("auth"))
+ userData, _ := auth.CreateBucketIfNotExists([]byte(u.id))
+ userData.Put([]byte("password hash"), passwords.Hash(password))
+ return nil
+ })
+ if err != nil {
+ log.Fatal("error updating database: ", err)
+ }
+ u.anonymous = false
+ u.Save()
+}