-
Notifications
You must be signed in to change notification settings - Fork 0
/
Put.go
58 lines (55 loc) · 1.66 KB
/
Put.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
package password
import (
"crypto/rand"
"encoding/hex"
"fmt"
"strings"
intermoni "github.com/intern-monitoring/backend-intermoni"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/crypto/argon2"
)
func UpdatePasswordUser(iduser primitive.ObjectID, db *mongo.Database, insertedDoc intermoni.Password) error {
dataUser, err := intermoni.GetUserFromID(iduser, db)
if err != nil {
return err
}
salt, err := hex.DecodeString(dataUser.Salt)
if err != nil {
return fmt.Errorf("kesalahan server : salt")
}
hash := argon2.IDKey([]byte(insertedDoc.Password), salt, 1, 64*1024, 4, 32)
if hex.EncodeToString(hash) != dataUser.Password {
return fmt.Errorf("password lama salah")
}
if insertedDoc.Newpassword == "" || insertedDoc.Confirmpassword == "" {
return fmt.Errorf("mohon untuk melengkapi data")
}
if insertedDoc.Confirmpassword != insertedDoc.Newpassword {
return fmt.Errorf("konfirmasi password salah")
}
if strings.Contains(insertedDoc.Newpassword, " ") {
return fmt.Errorf("password tidak boleh mengandung spasi")
}
if len(insertedDoc.Newpassword) < 8 {
return fmt.Errorf("password terlalu pendek")
}
salt = make([]byte, 16)
_, err = rand.Read(salt)
if err != nil {
return fmt.Errorf("kesalahan server : salt")
}
hashedPassword := argon2.IDKey([]byte(insertedDoc.Newpassword), salt, 1, 64*1024, 4, 32)
user := bson.M{
"email": dataUser.Email,
"password": hex.EncodeToString(hashedPassword),
"salt": hex.EncodeToString(salt),
"role": dataUser.Role,
}
err = intermoni.UpdateOneDoc(iduser, db, "user", user)
if err != nil {
return err
}
return nil
}