/
management_keys.go
160 lines (129 loc) · 4.44 KB
/
management_keys.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
package mongo
import (
"context"
"encoding/json"
"errors"
"log"
"time"
"github.com/madappgang/identifo/v2/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const managementKeysCollectionName = "ManagementKeys"
// ManagementKeysStorage is a MongoDB management keys storage.
type ManagementKeysStorage struct {
coll *mongo.Collection
}
// NewManagementKeysStorage creates a management keys invite storage.
func NewManagementKeysStorage(settings model.MongoDatabaseSettings) (model.ManagementKeysStorage, error) {
if len(settings.ConnectionString) == 0 || len(settings.DatabaseName) == 0 {
return nil, ErrorEmptyConnectionStringDatabase
}
// create database
db, err := NewDB(settings.ConnectionString, settings.DatabaseName)
if err != nil {
return nil, err
}
coll := db.Database.Collection(managementKeysCollectionName)
return &ManagementKeysStorage{coll: coll}, nil
}
func (ms *ManagementKeysStorage) GetKey(ctx context.Context, id string) (model.ManagementKey, error) {
filter := bson.M{"_id": id}
var key model.ManagementKey
if err := ms.coll.FindOne(ctx, filter).Decode(&key); err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return model.ManagementKey{}, model.ErrorNotFound
}
return model.ManagementKey{}, err
}
return key, nil
}
func (ms *ManagementKeysStorage) CreateKey(ctx context.Context, name string, scopes []string) (model.ManagementKey, error) {
key := model.ManagementKey{
Name: name,
Scopes: scopes,
ID: primitive.NewObjectID().Hex(),
Active: true,
CreatedAt: time.Now(),
LastUsed: time.Now(),
}
_, err := ms.coll.InsertOne(ctx, key)
return key, err
}
func (ms *ManagementKeysStorage) AddKey(ctx context.Context, key model.ManagementKey) (model.ManagementKey, error) {
_, err := ms.coll.InsertOne(ctx, key)
return key, err
}
func (ms *ManagementKeysStorage) DisableKey(ctx context.Context, id string) (model.ManagementKey, error) {
update := bson.M{"$set": bson.M{"active": false}}
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
var key model.ManagementKey
if err := ms.coll.FindOneAndUpdate(ctx, bson.M{"_id": id}, update, opts).Decode(&key); err != nil {
return key, err
}
return key, nil
}
func (ms *ManagementKeysStorage) RenameKey(ctx context.Context, id, name string) (model.ManagementKey, error) {
update := bson.M{"$set": bson.M{"name": name}}
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
var key model.ManagementKey
if err := ms.coll.FindOneAndUpdate(ctx, bson.M{"_id": id}, update, opts).Decode(&key); err != nil {
return key, err
}
return key, nil
}
func (ms *ManagementKeysStorage) ChangeScopesForKey(ctx context.Context, id string, scopes []string) (model.ManagementKey, error) {
update := bson.M{"$set": bson.M{"scopes": scopes}}
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
var key model.ManagementKey
if err := ms.coll.FindOneAndUpdate(ctx, bson.M{"_id": id}, update, opts).Decode(&key); err != nil {
return key, err
}
return key, nil
}
func (ms *ManagementKeysStorage) UseKey(ctx context.Context, id string) (model.ManagementKey, error) {
update := bson.M{"$set": bson.M{"lastUsed": time.Now()}}
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
var key model.ManagementKey
if err := ms.coll.FindOneAndUpdate(ctx, bson.M{"_id": id}, update, opts).Decode(&key); err != nil {
return key, err
}
return key, nil
}
func (ms *ManagementKeysStorage) GeyAllKeys(ctx context.Context) ([]model.ManagementKey, error) {
curr, err := ms.coll.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
var keys []model.ManagementKey
if err = curr.All(ctx, &keys); err != nil {
return nil, err
}
return keys, nil
}
func (ms *ManagementKeysStorage) ClearAllData() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if _, err := ms.coll.DeleteMany(ctx, bson.M{}); err != nil {
log.Printf("Error cleaning all user data: %s\n", err)
}
}
// ImportJSON imports data from JSON.
func (ms *ManagementKeysStorage) ImportJSON(data []byte, cleanOldData bool) error {
if cleanOldData {
ms.ClearAllData()
}
keys := []model.ManagementKey{}
if err := json.Unmarshal(data, &keys); err != nil {
log.Println(err)
return err
}
for _, a := range keys {
if _, err := ms.AddKey(context.TODO(), a); err != nil {
return err
}
}
return nil
}