This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 904
/
auth.go
50 lines (41 loc) · 1.37 KB
/
auth.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
package auth
import (
"errors"
"time"
"code.google.com/p/go.crypto/bcrypt"
)
var (
ErrUnauthorized = errors.New("unauthorized")
)
type (
Account struct {
ID string `json:"id,omitempty" gorethink:"id,omitempty"`
FirstName string `json:"first_name,omitempty" gorethink:"first_name,omitempty"`
LastName string `json:"last_name,omitempty" gorethink:"last_name,omitempty"`
Username string `json:"username,omitempty" gorethink:"username"`
Password string `json:"password,omitempty" gorethink:"password"`
Tokens []*AuthToken `json:"-" gorethink:"tokens"`
Roles []string `json:"roles,omitempty" gorethink:"roles"`
}
AuthToken struct {
Token string `json:"auth_token,omitempty" gorethink:"auth_token"`
UserAgent string `json:"user_agent,omitempty" gorethink:"user_agent"`
}
ServiceKey struct {
Key string `json:"key,omitempty" gorethink:"key"`
Description string `json:"description,omitempty" gorethink:"description"`
}
Authenticator interface {
Authenticate(username, password, hash string) (bool, error)
GenerateToken() (string, error)
IsUpdateSupported() bool
Name() string
}
)
func Hash(data string) (string, error) {
h, err := bcrypt.GenerateFromPassword([]byte(data), bcrypt.DefaultCost)
return string(h[:]), err
}
func GenerateToken() (string, error) {
return Hash(time.Now().String())
}