-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackendMongo.go
219 lines (187 loc) · 7.66 KB
/
backendMongo.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package auth
import (
"strings"
"time"
"github.com/EndFirstCorp/onedb/mgo"
"github.com/pkg/errors"
"gopkg.in/mgo.v2/bson"
)
type backendMongo struct {
m mgo.Sessioner
c Crypter
}
type mongoUser struct {
ID bson.ObjectId `bson:"_id" json:"id"`
PrimaryEmail string `bson:"primaryEmail" json:"primaryEmail"`
SecondaryEmails []email `bson:"secondaryEmails" json:"secondaryEmails"`
PasswordHash string `bson:"passwordHash" json:"passwordHash"`
IsEmailVerified bool `bson:"isEmailVerified" json:"isEmailVerified"`
Info map[string]interface{} `bson:"info" json:"info"`
LockoutEndTimeUTC *time.Time `bson:"lockoutEndTimeUTC" json:"lockoutEndTimeUTC"`
AccessFailedCount int `bson:"accessFailedCount" json:"accessFailedCount"`
}
type email struct {
Address string `bson:"address" json:"address"`
VerifyHash string `bson:"verifyHash" json:"verifyHash"`
IsVerified bool `bson:"isVerified" json:"isVerified"`
}
// NewBackendMongo creates a MongoDB-based Backender
func NewBackendMongo(m mgo.Sessioner, c Crypter) Backender {
return &backendMongo{m, c}
}
func (b *backendMongo) Clone() Backender {
return &backendMongo{b.m.Clone(), b.c}
}
func (b *backendMongo) AddVerifiedUser(email string, info map[string]interface{}) (string, error) {
u, err := b.getUser(email)
if err == nil {
return u.ID.Hex(), errors.New("user already exists")
}
id := bson.NewObjectId()
return id.Hex(), b.users().Insert(mongoUser{ID: id, PrimaryEmail: email, IsEmailVerified: true, Info: info})
}
func (b *backendMongo) AddUserFull(email, password string, info map[string]interface{}) (*User, error) {
passwordHash, err := b.c.Hash(password)
if err != nil {
return nil, err
}
_, err = b.getUser(email)
if err == nil {
return nil, errors.New("user already exists")
}
id := bson.NewObjectId()
return &User{id.Hex(), strings.ToLower(email), false, info}, b.users().Insert(mongoUser{ID: id, PrimaryEmail: strings.ToLower(email), PasswordHash: passwordHash, Info: info})
}
func (b *backendMongo) getUser(email string) (*mongoUser, error) {
u := &mongoUser{}
return u, b.users().Find(bson.M{"primaryEmail": strings.ToLower(email)}).One(u)
}
func (b *backendMongo) GetUser(email string) (*User, error) {
u, err := b.getUser(email)
if err != nil {
return nil, err
}
return &User{u.ID.Hex(), u.PrimaryEmail, u.IsEmailVerified, u.Info}, nil
}
func (b *backendMongo) UpdateUser(userID, password string, info map[string]interface{}) error {
passwordHash, err := b.c.Hash(password)
if err != nil {
return err
}
set := bson.M{"passwordHash": passwordHash, "info": info}
return b.users().UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": set})
}
func (b *backendMongo) UpdatePassword(userID, password string) error {
passwordHash, err := b.c.Hash(password)
if err != nil {
return err
}
return b.users().UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": bson.M{"passwordHash": passwordHash}})
}
func (b *backendMongo) VerifyEmail(email string) error {
return b.users().Update(bson.M{"primaryEmail": email}, bson.M{"$set": bson.M{"isEmailVerified": true}})
}
func (b *backendMongo) UpdateInfo(userID string, info map[string]interface{}) error {
set := make(bson.M)
for k, v := range info {
set["info."+k] = v
}
_, err := b.loginSessions().UpdateAll(bson.M{"userID": userID}, bson.M{"$set": set})
if err != nil {
return err
}
return b.users().UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": set})
}
func (b *backendMongo) Close() error {
b.m.Close()
return nil
}
func (b *backendMongo) LoginAndGetUser(email, password string) (*User, error) {
u, err := b.getUser(email)
if err != nil {
return nil, err
}
if err := b.c.HashEquals(password, u.PasswordHash); err != nil {
return nil, err
}
return &User{u.ID.Hex(), u.PrimaryEmail, u.IsEmailVerified, u.Info}, nil
}
func (b *backendMongo) Login(email, password string) error {
_, err := b.LoginAndGetUser(email, password)
return err
}
func (b *backendMongo) AddSecondaryEmail(userID, secondaryEmail string) error {
return nil
}
func (b *backendMongo) UpdatePrimaryEmail(userID, secondaryEmail string) error {
return nil
}
func (b *backendMongo) CreateEmailSession(userID, email string, info map[string]interface{}, emailVerifyHash, csrfToken string) error {
s := b.emailSessions()
c, _ := s.FindId(emailVerifyHash).Count()
if c > 0 {
return errors.New("invalid emailVerifyHash")
}
return s.Insert(&emailSession{userID, strings.ToLower(email), info, emailVerifyHash, csrfToken})
}
func (b *backendMongo) GetEmailSession(verifyHash string) (*emailSession, error) {
session := &emailSession{}
return session, b.emailSessions().FindId(verifyHash).One(session)
}
func (b *backendMongo) UpdateEmailSession(verifyHash, userID string) error {
return b.emailSessions().UpdateId(verifyHash, bson.M{"$set": bson.M{"userID": userID}})
}
func (b *backendMongo) DeleteEmailSession(verifyHash string) error {
return b.emailSessions().RemoveId(verifyHash)
}
func (b *backendMongo) CreateSession(userID, email string, info map[string]interface{}, sessionHash, csrfToken string, renewTimeUTC, expireTimeUTC time.Time) (*LoginSession, error) {
s := LoginSession{userID, strings.ToLower(email), info, sessionHash, csrfToken, renewTimeUTC, expireTimeUTC}
return &s, b.loginSessions().Insert(s)
}
func (b *backendMongo) CreateRememberMe(userID, email, selector, tokenHash string, renewTimeUTC, expireTimeUTC time.Time) (*rememberMeSession, error) {
r := rememberMeSession{userID, strings.ToLower(email), selector, tokenHash, renewTimeUTC, expireTimeUTC}
return &r, b.rememberMeSessions().Insert(&r)
}
func (b *backendMongo) GetSession(sessionHash string) (*LoginSession, error) {
session := &LoginSession{}
return session, b.loginSessions().FindId(sessionHash).One(session)
}
func (b *backendMongo) UpdateSession(sessionHash string, renewTimeUTC, expireTimeUTC time.Time) error {
return b.loginSessions().UpdateId(sessionHash, bson.M{"$set": bson.M{"expireTimeUTC": expireTimeUTC, "renewTimeUTC": renewTimeUTC}})
}
func (b *backendMongo) DeleteSession(sessionHash string) error {
return b.loginSessions().RemoveId(sessionHash)
}
func (b *backendMongo) DeleteSessions(e string) error {
_, err := b.loginSessions().RemoveAll(bson.M{"email": strings.ToLower(e)})
return err
}
func (b *backendMongo) InvalidateSessions(email string) error {
return nil
}
func (b *backendMongo) GetRememberMe(selector string) (*rememberMeSession, error) {
rememberMe := &rememberMeSession{}
return rememberMe, b.rememberMeSessions().FindId(selector).One(rememberMe)
}
func (b *backendMongo) UpdateRememberMe(selector string, renewTimeUTC time.Time) error {
return b.rememberMeSessions().UpdateId(selector, bson.M{"$set": bson.M{"renewTimeUTC": renewTimeUTC}})
}
func (b *backendMongo) DeleteRememberMe(selector string) error {
return b.rememberMeSessions().RemoveId(selector)
}
func (b *backendMongo) DeleteRememberMes(e string) error {
_, err := b.rememberMeSessions().RemoveAll(bson.M{"email": strings.ToLower(e)})
return err
}
func (b *backendMongo) users() mgo.Collectioner {
return b.m.DB("users").C("users")
}
func (b *backendMongo) emailSessions() mgo.Collectioner {
return b.m.DB("users").C("emailSessions")
}
func (b *backendMongo) loginSessions() mgo.Collectioner {
return b.m.DB("users").C("loginSessions")
}
func (b *backendMongo) rememberMeSessions() mgo.Collectioner {
return b.m.DB("users").C("rememberMeSessions")
}