-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
39 lines (34 loc) · 937 Bytes
/
user.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
package auth
import (
"github.com/dbeliakov/revisor/api/store"
"golang.org/x/crypto/bcrypt"
)
const (
hashCost = 8
)
// NewUser creates new user object
func newUser(firstName, lastName, login, password string) (store.User, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), hashCost)
if err != nil {
return store.User{}, err
}
return store.User{
FirstName: firstName,
LastName: lastName,
Login: login,
PasswordHash: string(hash),
}, nil
}
// CheckPassword compares password with PasswordHash
func checkPassword(user store.User, password string) bool {
return bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)) == nil
}
// SetPassword new password
func setPassword(user *store.User, password string) error {
hash, err := bcrypt.GenerateFromPassword([]byte(password), hashCost)
if err != nil {
return err
}
user.PasswordHash = string(hash)
return nil
}