-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
173 lines (140 loc) · 4.17 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package user
import (
"errors"
"fmt"
"github.com/asaskevich/govalidator"
"github.com/dgrijalva/jwt-go"
_ "github.com/go-sql-driver/mysql" // MySQL Go driver
"github.com/jmoiron/sqlx"
"golang.org/x/crypto/bcrypt"
)
// Database queries
const (
queryInsertNewUser = `INSERT INTO users (email, password) VALUES (:email, :password)`
queryFindUserByEmail = `SELECT * FROM users WHERE email=?`
queryFindUserByID = `SELECT * FROM users WHERE id=?`
)
// Error responses
const (
errorInvalidEmail = "Invalid email address"
errorEmailNotFound = "Could not find a user with the given email address"
errorShortPassword = "Password must be greater than %d characters"
errorExistingUser = "A user with this email address has already been registered"
errorIDNotFound = "Could not find a user with the given ID"
errorJWT = "An error occured while trying to product a JSON Web Token"
errorInvalidCredentials = "Invalid email/password combination"
)
// A User represents the database record of the user model.
type User struct {
db *sqlx.DB
ID int64 `db:"id"`
Email string `db:"email"`
Password string `db:"password"`
}
// A SimpleUser represents a simpler version of User without any of
// the supperfluous database fields. Used for API responses.
type SimpleUser struct {
ID int64 `json:"id"`
Email string `json:"email"`
}
// A Token is a <a href="http://jwt.io">JSON Web Token</a> representation of a
// User authenticated session.
type Token struct {
Token string `json:"token"`
}
// CreateUser creates a new User record with an email and password,
// and saves it to the database.
func (u *User) CreateUser(email, password string) error {
u.Email = email
u.Password = password
// Validation
if govalidator.IsEmail(u.Email) != true {
return errors.New(errorInvalidEmail)
}
if govalidator.IsByteLength(u.Password, 8, 255) != true {
return fmt.Errorf(errorShortPassword, 8)
}
// Check if there's an existing user with this email
_, err := u.FindUserByEmail(u.Email)
if err == nil {
return errors.New(errorExistingUser)
}
// Encode the user's password
encryptedPass, err := bcrypt.GenerateFromPassword([]byte(password), 5)
if err != nil {
return err
}
u.Password = string(encryptedPass)
// Insert into the DB
_, err = u.db.NamedExec(queryInsertNewUser, *u)
if err != nil {
return err
}
return err
}
// FindUserByID retrieves a User record from the database by its
// index.
func (u *User) FindUserByID(id int64) (*User, error) {
// Find user row
err := u.db.QueryRowx(queryFindUserByID, id).StructScan(u)
if err != nil {
return nil, errors.New(errorIDNotFound)
}
return u, err
}
// FetchUserTokenByID retrieves a user by it's primary key, and then
// encodes it into a <a href="http://jwt.io">JSON Web Token</a>.
func (u *User) FetchUserTokenByID(id int64) (*Token, error) {
t := &Token{}
u, err := u.FindUserByID(id)
if err != nil {
return nil, err
}
jt := jwt.New(jwt.SigningMethodHS256)
jt.Claims["id"] = u.ID
jt.Claims["email"] = u.Email
t.Token, err = jt.SignedString([]byte(config.EncodingJWT))
if err != nil {
return nil, errors.New(errorJWT)
}
return t, err
}
// FindUserByEmail queries the database for a User record by its
// email/username.
func (u *User) FindUserByEmail(email string) (*User, error) {
// Validation
if govalidator.IsEmail(email) != true {
return nil, errors.New(errorInvalidEmail)
}
// Find user row
err := u.db.QueryRowx(queryFindUserByEmail, email).StructScan(u)
if err != nil {
return nil, errors.New(errorEmailNotFound)
}
return u, err
}
// FindUserByEmailAndPassword queries the database for a User
// record by its username/password pair.
func (u *User) FindUserByEmailAndPassword(email, password string) (*User, error) {
u, err := u.FindUserByEmail(email)
if err != nil {
return nil, err
}
err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
if err != nil {
return nil, errors.New(errorInvalidCredentials)
}
return u, err
}
func LoadUser() (*User, error) {
var err error
u := &User{}
u.db, err = sqlx.Connect(config.DBDriver, config.dbString)
if err != nil {
return nil, err
}
return u, err
}
func init() {
config.LoadJSON()
}