fleet/datastore/inmem_password_reset.go
Victor Vrantchan df19fd4b7c Update users service (#156)
Closes #144 #145 #160 
Implements PATCH method on user and endpoint middleware for authnz
Implements `reset_password` (with token) and `forgot_password` endpoints
Added godoc comments for UserService interface
Shift to using testify/assert in test code
Multiple fixes/changes to the UserService API
2016-09-15 10:52:17 -04:00

104 lines
2.2 KiB
Go

package datastore
import "github.com/kolide/kolide-ose/kolide"
func (orm *inmem) NewPasswordResetRequest(req *kolide.PasswordResetRequest) (*kolide.PasswordResetRequest, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
req.ID = uint(len(orm.passwordResets) + 1)
orm.passwordResets[req.ID] = req
return req, nil
}
func (orm *inmem) SavePasswordResetRequest(req *kolide.PasswordResetRequest) error {
orm.mtx.Lock()
defer orm.mtx.Unlock()
if _, ok := orm.passwordResets[req.ID]; !ok {
return ErrNotFound
}
orm.passwordResets[req.ID] = req
return nil
}
func (orm *inmem) DeletePasswordResetRequest(req *kolide.PasswordResetRequest) error {
orm.mtx.Lock()
defer orm.mtx.Unlock()
if _, ok := orm.passwordResets[req.ID]; !ok {
return ErrNotFound
}
delete(orm.passwordResets, req.ID)
return nil
}
func (orm *inmem) DeletePasswordResetRequestsForUser(userID uint) error {
orm.mtx.Lock()
defer orm.mtx.Unlock()
for _, pr := range orm.passwordResets {
if pr.UserID == userID {
delete(orm.passwordResets, pr.ID)
}
}
return nil
}
func (orm *inmem) FindPassswordResetByID(id uint) (*kolide.PasswordResetRequest, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
if req, ok := orm.passwordResets[id]; ok {
return req, nil
}
return nil, ErrNotFound
}
func (orm *inmem) FindPassswordResetsByUserID(userID uint) ([]*kolide.PasswordResetRequest, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
resets := make([]*kolide.PasswordResetRequest, 0)
for _, pr := range orm.passwordResets {
if pr.UserID == userID {
resets = append(resets, pr)
}
}
if len(resets) == 0 {
return nil, ErrNotFound
}
return resets, nil
}
func (orm *inmem) FindPassswordResetByToken(token string) (*kolide.PasswordResetRequest, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
for _, pr := range orm.passwordResets {
if pr.Token == token {
return pr, nil
}
}
return nil, ErrNotFound
}
func (orm *inmem) FindPassswordResetByTokenAndUserID(token string, userID uint) (*kolide.PasswordResetRequest, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
for _, pr := range orm.passwordResets {
if pr.Token == token && pr.UserID == userID {
return pr, nil
}
}
return nil, ErrNotFound
}