]>
code.octet-stream.net Git - broadcaster/blob - server/user.go
5 "golang.org/x/crypto/bcrypt"
12 func (u
*Users
) GetUserForSession(token
string) (User
, error
) {
13 username
, err
:= db
.GetUserNameForSession(token
)
17 user
, err
:= db
.GetUser(username
)
24 func (u
*Users
) Authenticate(username
string, clearPassword
string) (User
, error
) {
25 user
, err
:= db
.GetUser(username
)
29 err
= bcrypt
.CompareHashAndPassword([]byte(user
.PasswordHash
), []byte(clearPassword
))
36 func (u
*Users
) CreateUser(username
string, clearPassword
string, isAdmin
bool) error
{
37 if clearPassword
== "" {
38 return errors
.New("password cannot be empty")
40 hashed
, err
:= bcrypt
.GenerateFromPassword([]byte(clearPassword
), bcrypt
.DefaultCost
)
44 return db
.CreateUser(User
{
47 PasswordHash
: string(hashed
),
52 func (u
*Users
) DeleteUser(username
string) {
53 db
.DeleteUser(username
)
56 func (u
*Users
) UpdatePassword(username
string, oldClearPassword
string, newClearPassword
string) error
{
57 user
, err
:= db
.GetUser(username
)
61 err
= bcrypt
.CompareHashAndPassword([]byte(user
.PasswordHash
), []byte(oldClearPassword
))
63 return errors
.New("old password is incorrect")
65 if newClearPassword
== "" {
66 return errors
.New("password cannot be empty")
68 hashed
, err
:= bcrypt
.GenerateFromPassword([]byte(newClearPassword
), bcrypt
.DefaultCost
)
72 db
.SetUserPassword(username
, string(hashed
))
76 func (u
*Users
) UpdateIsAdmin(username
string, isAdmin
bool) {
77 db
.SetUserIsAdmin(username
, isAdmin
)
80 func (u
*Users
) Users() []User
{