This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
basicauth_membership.go
82 lines (65 loc) · 2.02 KB
/
basicauth_membership.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package api
import (
"time"
"golang.org/x/crypto/bcrypt"
)
// BasicAuthMembership stores basic authentication information for the account.
type BasicAuthMembership struct {
ID int
AccountID int
Account *Account
PasswordHash string
Created time.Time
}
// Save the basic membership.
func (entity *BasicAuthMembership) Save(context DatabaseService, account int) (int, error) {
if err := context.Save(entity); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// Delete the basic membership.
func (entity *BasicAuthMembership) Delete(context DatabaseService, account int) (int, error) {
if entity.ID != 0 {
if err := context.Delete(entity); err != nil {
return entity.ID, err
}
}
return entity.ID, nil
}
// Get the basic membership.
func (entity *BasicAuthMembership) Get(context DatabaseService, account int) (int, error) {
if err := entity.Find(context); err != nil {
return entity.ID, err
}
return entity.ID, nil
}
// GetID returns the entity identifier.
func (entity *BasicAuthMembership) GetID() int {
return entity.ID
}
// SetID sets the entity identifier.
func (entity *BasicAuthMembership) SetID(id int) {
entity.ID = id
}
// Find the basic membership.
func (entity *BasicAuthMembership) Find(context DatabaseService) error {
// if entity.ID == 0 {
// return context.Where(entity, "Account.username = ?", entity.Username)
// }
return context.Select(entity)
}
// PasswordMatch determines if a plain text password matches its equivalent password hash.
func (entity *BasicAuthMembership) PasswordMatch(password string) bool {
return bcrypt.CompareHashAndPassword([]byte(entity.PasswordHash), []byte(password)) == nil
}
// HashPassword converts a plaintext password and generates a hash and updates the
// `PasswordHash` value.
func (entity *BasicAuthMembership) HashPassword(password string) error {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
entity.PasswordHash = string(hashedPassword)
return nil
}