*: add the ability to define passwords statically
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package storage
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Tests for this code are in the "memory" package, since this package doesn't
|
||||
// define a concrete storage implementation.
|
||||
@@ -53,3 +56,39 @@ func (s staticClientsStorage) DeleteClient(id string) error {
|
||||
func (s staticClientsStorage) UpdateClient(id string, updater func(old Client) (Client, error)) error {
|
||||
return errors.New("static clients: read-only cannot update client")
|
||||
}
|
||||
|
||||
type staticPasswordsStorage struct {
|
||||
Storage
|
||||
|
||||
passwordsByEmail map[string]Password
|
||||
}
|
||||
|
||||
// WithStaticPasswords returns a storage with a read-only set of passwords. Write actions,
|
||||
// such as creating other passwords, will fail.
|
||||
func WithStaticPasswords(s Storage, staticPasswords []Password) Storage {
|
||||
passwordsByEmail := make(map[string]Password, len(staticPasswords))
|
||||
for _, p := range staticPasswords {
|
||||
p.Email = strings.ToLower(p.Email)
|
||||
passwordsByEmail[p.Email] = p
|
||||
}
|
||||
return staticPasswordsStorage{s, passwordsByEmail}
|
||||
}
|
||||
|
||||
func (s staticPasswordsStorage) GetPassword(email string) (Password, error) {
|
||||
if password, ok := s.passwordsByEmail[strings.ToLower(email)]; ok {
|
||||
return password, nil
|
||||
}
|
||||
return Password{}, ErrNotFound
|
||||
}
|
||||
|
||||
func (s staticPasswordsStorage) CreatePassword(p Password) error {
|
||||
return errors.New("static passwords: read-only cannot create password")
|
||||
}
|
||||
|
||||
func (s staticPasswordsStorage) DeletePassword(id string) error {
|
||||
return errors.New("static passwords: read-only cannot create password")
|
||||
}
|
||||
|
||||
func (s staticPasswordsStorage) UpdatePassword(id string, updater func(old Password) (Password, error)) error {
|
||||
return errors.New("static passwords: read-only cannot update password")
|
||||
}
|
Reference in New Issue
Block a user