-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
72 lines (57 loc) · 1.39 KB
/
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
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
package goku
import (
"encoding/json"
"errors"
"fmt"
)
// User is a simple structure to represent a user that can interact with repositories
type User struct {
Username string `json:"username"`
Email string `json:"email"`
PasswordHash string `json:"passwordHash"`
PasswordSalt string `json:"passwordSalt"`
}
// UserFromJson creates a new User from a json string
func UserFromJson(userJson []byte) User {
u := User{}
json.Unmarshal(userJson, &u)
return u
}
func NewUserStore(backend Backend) userStore {
return userStore{
backend,
}
}
type userStore struct{ backend Backend }
func (u userStore) HandleAuth(username, password string) error {
user, err := u.Get(username)
if err != nil {
return err
}
if password == user.PasswordHash {
return nil
}
return errors.New("Unauthorized")
}
func (u userStore) Get(username string) (User, error) {
userJson, err := u.backend.Get(createUserKey(username))
if err != nil {
return User{}, err
}
return UserFromJson(userJson), nil
}
func (u userStore) New(username, password string) (User, error) {
return User{}, nil
}
func (u userStore) Update(user User) error {
return nil
}
func (u userStore) Delete(username string) error {
return u.backend.Delete(createUserKey(username))
}
func (u userStore) List() ([]User, error) {
return nil, nil
}
func createUserKey(username string) string {
return fmt.Sprintf("/users/%v", username)
}