-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage.go
107 lines (88 loc) · 3.02 KB
/
storage.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
package main
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// Storage provides high-level interactions with an underlying storage mechanism.
type Storage interface {
CreateAccount(account *Account) error
FindAccount(name string) (*Account, error)
AddKeyToAccount(name, key string) error
RevokeKeyFromAccount(name, key string) error
AccountHasKey(name, key string) (bool, error)
}
// MongoStorage is a Storage implementation that connects to a real MongoDB cluster.
type MongoStorage struct {
Database *mgo.Database
}
// NewMongoStorage establishes a connection to a MongoDB cluster.
func NewMongoStorage(c *Context) (*MongoStorage, error) {
session, err := mgo.Dial(c.MongoURL)
if err != nil {
return nil, err
}
return &MongoStorage{Database: session.DB("auth")}, nil
}
func (storage *MongoStorage) accounts() *mgo.Collection {
return storage.Database.C("accounts")
}
// CreateAccount persists an Account model into Mongo as it's currently populated.
func (storage *MongoStorage) CreateAccount(account *Account) error {
return storage.accounts().Insert(account)
}
// FindAccount queries for an existing account with a specified name. If no such account exists,
// nil is returned.
func (storage *MongoStorage) FindAccount(name string) (*Account, error) {
var account Account
err := storage.accounts().FindId(name).One(&account)
if err == mgo.ErrNotFound {
return nil, nil
}
return &account, err
}
// AddKeyToAccount appends a newly generated API key to an existing account.
func (storage *MongoStorage) AddKeyToAccount(name, key string) error {
return storage.accounts().UpdateId(name, bson.M{
"$push": bson.M{"api_keys": key},
})
}
// RevokeKeyFromAccount removes an API key from an account.
func (storage *MongoStorage) RevokeKeyFromAccount(name, key string) error {
return storage.accounts().UpdateId(name, bson.M{
"$pull": bson.M{"api_keys": key},
})
}
// AccountHasKey returns true if the named account has an associated API key that matches the
// provided one, or false if it does not.
func (storage *MongoStorage) AccountHasKey(name, key string) (bool, error) {
n, err := storage.accounts().Find(bson.M{
"_id": name,
"api_keys": key,
}).Count()
return n == 1, err
}
// NullStorage provides no-op implementations of Storage methods. It's useful for selective
// overriding in unit tests.
type NullStorage struct{}
// CreateAccount is a no-op.
func (storage NullStorage) CreateAccount(*Account) error {
return nil
}
// FindAccount always fails to find an account.
func (storage NullStorage) FindAccount(name string) (*Account, error) {
return nil, nil
}
// AddKeyToAccount is a no-op.
func (storage NullStorage) AddKeyToAccount(name, key string) error {
return nil
}
// RevokeKeyFromAccount is a no-op.
func (storage NullStorage) RevokeKeyFromAccount(name, key string) error {
return nil
}
// AccountHasKey always returns false.
func (storage NullStorage) AccountHasKey(name, key string) (bool, error) {
return false, nil
}
// Ensure that NullStorage obeys the Storage interface.
var _ Storage = NullStorage{}