Skip to content

Commit

Permalink
close session that is being created
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Archibald committed Aug 2, 2018
1 parent 4d17a4c commit a9e45ed
Showing 1 changed file with 74 additions and 33 deletions.
107 changes: 74 additions & 33 deletions backendMongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,56 @@ func NewBackendMongo(m mgo.Sessioner, c Crypter) Backender {
return &backendMongo{m, c}
}

func (b *backendMongo) Clone() mgo.Sessioner {
return b.m.Clone()
}

func (b *backendMongo) AddUser(email string, info map[string]interface{}) (string, error) {
u, err := b.getUser(email)
m := b.Clone()
defer m.Close()
u, err := b.getUser(m, 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, Info: info})
return id.Hex(), b.users(m).Insert(mongoUser{ID: id, PrimaryEmail: email, Info: info})
}

func (b *backendMongo) AddUserFull(email, password string, info map[string]interface{}) (*User, error) {
m := b.Clone()
defer m.Close()
passwordHash, err := b.c.Hash(password)
if err != nil {
return nil, err
}
_, err = b.getUser(email)
_, err = b.getUser(m, email)
if err == nil {
return nil, errors.New("user already exists")
}

id := bson.NewObjectId()
return &User{id.Hex(), email, info}, b.users().Insert(mongoUser{ID: id, PrimaryEmail: email, PasswordHash: passwordHash, Info: info})
return &User{id.Hex(), email, info}, b.users(m).Insert(mongoUser{ID: id, PrimaryEmail: email, PasswordHash: passwordHash, Info: info})
}

func (b *backendMongo) getUser(email string) (*mongoUser, error) {
func (b *backendMongo) getUser(m mgo.Sessioner, email string) (*mongoUser, error) {
u := &mongoUser{}
return u, b.users().Find(bson.M{"primaryEmail": email}).One(u)
return u, b.users(m).Find(bson.M{"primaryEmail": email}).One(u)
}

func (b *backendMongo) GetUser(email string) (*User, error) {
u, err := b.getUser(email)
m := b.Clone()
defer m.Close()
u, err := b.getUser(m, email)
if err != nil {
return nil, err
}
return &User{u.ID.Hex(), u.PrimaryEmail, u.Info}, nil
}

func (b *backendMongo) UpdateUser(userID, password string, info map[string]interface{}) error {
m := b.Clone()
defer m.Close()
passwordHash, err := b.c.Hash(password)
if err != nil {
return err
Expand All @@ -81,23 +93,27 @@ func (b *backendMongo) UpdateUser(userID, password string, info map[string]inter
set["info."+key] = info[key]
}
set["passwordHash"] = passwordHash
return b.users().UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": set})
return b.users(m).UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": set})
}

func (b *backendMongo) UpdatePassword(userID, password string) error {
m := b.Clone()
defer m.Close()
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}})
return b.users(m).UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": bson.M{"passwordHash": passwordHash}})
}

func (b *backendMongo) UpdateInfo(userID string, info map[string]interface{}) error {
m := b.Clone()
defer m.Close()
var set bson.M
for key := range info {
set["info."+key] = info[key]
}
return b.users().UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": set})
return b.users(m).UpdateId(bson.ObjectIdHex(userID), bson.M{"$set": set})
}

func (b *backendMongo) Close() error {
Expand All @@ -106,7 +122,9 @@ func (b *backendMongo) Close() error {
}

func (b *backendMongo) LoginAndGetUser(email, password string) (*User, error) {
u, err := b.getUser(email)
m := b.Clone()
defer m.Close()
u, err := b.getUser(m, email)
if err != nil {
return nil, err
}
Expand All @@ -130,70 +148,93 @@ func (b *backendMongo) UpdatePrimaryEmail(userID, secondaryEmail string) error {
}

func (b *backendMongo) CreateEmailSession(email string, info map[string]interface{}, emailVerifyHash, csrfToken string) error {
s := b.emailSessions()
c, _ := s.FindId(emailVerifyHash).Count()
m := b.Clone()
defer m.Close()
c, _ := b.emailSessions(m).FindId(emailVerifyHash).Count()
if c > 0 {
return errors.New("invalid emailVerifyHash")
}
return s.Insert(&emailSession{"", email, info, emailVerifyHash, csrfToken})
return b.emailSessions(m).Insert(&emailSession{"", email, info, emailVerifyHash, csrfToken})
}

func (b *backendMongo) GetEmailSession(verifyHash string) (*emailSession, error) {
m := b.Clone()
defer m.Close()
session := &emailSession{}
return session, b.emailSessions().FindId(verifyHash).One(session)
return session, b.emailSessions(m).FindId(verifyHash).One(session)
}

func (b *backendMongo) UpdateEmailSession(verifyHash, userID string) error {
return b.emailSessions().UpdateId(verifyHash, bson.M{"$set": bson.M{"userID": userID}})
m := b.Clone()
defer m.Close()
return b.emailSessions(m).UpdateId(verifyHash, bson.M{"$set": bson.M{"userID": userID}})
}
func (b *backendMongo) DeleteEmailSession(verifyHash string) error {
return b.emailSessions().RemoveId(verifyHash)
m := b.Clone()
defer m.Close()
return b.emailSessions(m).RemoveId(verifyHash)
}
func (b *backendMongo) CreateSession(userID, email string, info map[string]interface{}, sessionHash, csrfToken string, renewTimeUTC, expireTimeUTC time.Time) (*LoginSession, error) {
m := b.Clone()
defer m.Close()
s := LoginSession{userID, email, info, sessionHash, csrfToken, renewTimeUTC, expireTimeUTC}
return &s, b.loginSessions().Insert(s)
return &s, b.loginSessions(m).Insert(s)
}

func (b *backendMongo) CreateRememberMe(userID, email, selector, tokenHash string, renewTimeUTC, expireTimeUTC time.Time) (*rememberMeSession, error) {
m := b.Clone()
defer m.Close()
r := rememberMeSession{userID, email, selector, tokenHash, renewTimeUTC, expireTimeUTC}
return &r, b.rememberMeSessions().Insert(&r)
return &r, b.rememberMeSessions(m).Insert(&r)
}

func (b *backendMongo) GetSession(sessionHash string) (*LoginSession, error) {
m := b.Clone()
defer m.Close()
session := &LoginSession{}
return session, b.loginSessions().FindId(sessionHash).One(session)
return session, b.loginSessions(m).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}})
m := b.Clone()
defer m.Close()
return b.loginSessions(m).UpdateId(sessionHash, bson.M{"$set": bson.M{"expireTimeUTC": expireTimeUTC, "renewTimeUTC": renewTimeUTC}})
}

func (b *backendMongo) DeleteSession(sessionHash string) error {
return b.loginSessions().RemoveId(sessionHash)
m := b.Clone()
defer m.Close()
return b.loginSessions(m).RemoveId(sessionHash)
}
func (b *backendMongo) InvalidateSessions(email string) error {
return nil
}
func (b *backendMongo) GetRememberMe(selector string) (*rememberMeSession, error) {
m := b.Clone()
defer m.Close()
rememberMe := &rememberMeSession{}
return rememberMe, b.rememberMeSessions().FindId(selector).One(rememberMe)
return rememberMe, b.rememberMeSessions(m).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}})
m := b.Clone()
defer m.Close()
return b.rememberMeSessions(m).UpdateId(selector, bson.M{"$set": bson.M{"renewTimeUTC": renewTimeUTC}})
}
func (b *backendMongo) DeleteRememberMe(selector string) error {
return b.rememberMeSessions().RemoveId(selector)
m := b.Clone()
defer m.Close()
return b.rememberMeSessions(m).RemoveId(selector)
}

func (b *backendMongo) users() mgo.Collectioner {
return b.m.Clone().DB("users").C("users")
func (b *backendMongo) users(s mgo.Sessioner) mgo.Collectioner {
return s.DB("users").C("users")
}
func (b *backendMongo) emailSessions() mgo.Collectioner {
return b.m.Clone().DB("users").C("emailSessions")
func (b *backendMongo) emailSessions(s mgo.Sessioner) mgo.Collectioner {
return s.DB("users").C("emailSessions")
}
func (b *backendMongo) loginSessions() mgo.Collectioner {
return b.m.Clone().DB("users").C("loginSessions")
func (b *backendMongo) loginSessions(s mgo.Sessioner) mgo.Collectioner {
return s.DB("users").C("loginSessions")
}
func (b *backendMongo) rememberMeSessions() mgo.Collectioner {
return b.m.Clone().DB("users").C("rememberMeSessions")
func (b *backendMongo) rememberMeSessions(s mgo.Sessioner) mgo.Collectioner {
return s.DB("users").C("rememberMeSessions")
}

0 comments on commit a9e45ed

Please sign in to comment.