/
backend.go
179 lines (158 loc) · 4.5 KB
/
backend.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package backend_mongo
import (
"context"
"errors"
"fmt"
"github.com/themakers/identity/identity"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
collUsers = "users"
collAuthentications = "authentications"
)
type Options struct {
DBName string
CollectionPrefix string
URI string
}
var _ identity.Backend = new(Backend)
type Backend struct {
ops Options
client *mongo.Client
}
func New(ops Options) (*Backend, error) {
b := &Backend{
ops: ops,
}
client, err := mongo.NewClient(options.Client().ApplyURI(b.ops.URI))
if err != nil {
return nil, err
} else {
if err := client.Connect(context.TODO()); err != nil {
return nil, err
}
b.client = client
}
return b, nil
}
func (b *Backend) db() *mongo.Database {
return b.client.Database(b.ops.DBName)
}
func (b *Backend) coll(coll string) *mongo.Collection {
return b.db().Collection(fmt.Sprintf("%s%s", b.ops.CollectionPrefix, coll))
}
func (b *Backend) txn(ctx context.Context, txfn func(ctx mongo.SessionContext) error) error {
return b.client.UseSession(ctx, func(ctx mongo.SessionContext) error {
if _, err := ctx.WithTransaction(ctx, func(ctx mongo.SessionContext) (interface{}, error) {
return nil, txfn(ctx)
}); err != nil {
return err
} else {
return nil
}
})
}
func (b *Backend) Cleanup(ctx context.Context) error {
return b.txn(ctx, func(ctx mongo.SessionContext) error {
if _, err := b.coll(collAuthentications).DeleteMany(ctx, bson.M{}); err != nil {
return err
}
if _, err := b.coll(collUsers).DeleteMany(ctx, bson.M{}); err != nil {
return err
}
return nil
})
}
func (b *Backend) GetAuthentication(ctx context.Context, id string) (*identity.Authentication, error) {
var auth identity.Authentication
if err := b.coll(collAuthentications).FindOne(ctx, bson.M{"_id": id}).Decode(&auth); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &auth, nil
}
func (b *Backend) CreateAuthentication(ctx context.Context, id string, objective identity.AuthenticationObjective, userID string) (*identity.Authentication, error) {
auth := identity.Authentication{
ID: id,
Objective: objective,
UserID: userID,
RequiredFactorsCount: 1,
Version: 1,
}
if _, err := b.coll(collAuthentications).InsertOne(ctx, auth); err != nil {
return nil, err
}
return &auth, nil
}
func (b *Backend) SaveAuthentication(ctx context.Context, auth *identity.Authentication) (result *identity.Authentication, err error) {
ver := auth.Version
auth.Version++
if err := b.coll(collAuthentications).FindOneAndUpdate(ctx, bson.M{
"_id": auth.ID,
"Version": ver,
}, bson.M{
"$set": auth,
}, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&result); err != nil {
return nil, err
}
return result, nil
}
func (b *Backend) RemoveAuthentication(ctx context.Context, id string) error {
if _, err := b.coll(collAuthentications).DeleteOne(ctx, bson.M{
"_id": id,
}); err != nil {
return err
}
return nil
}
func (b *Backend) GetUser(ctx context.Context, id string) (*identity.User, error) {
var user identity.User
if err := b.coll(collUsers).FindOne(ctx, bson.M{"_id": id}).Decode(&user); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &user, nil
}
func (b *Backend) CreateUser(ctx context.Context, user *identity.User) (*identity.User, error) {
user.Version = 1
if _, err := b.coll(collUsers).InsertOne(ctx, user); err != nil {
return nil, err
}
return user, nil
}
func (b *Backend) SaveUser(ctx context.Context, user *identity.User) (result *identity.User, err error) {
if user.ID == "" {
return nil, errors.New("user id missing")
}
ver := user.Version
user.Version++
if err := b.coll(collUsers).FindOneAndUpdate(ctx, bson.M{
"_id": user.ID,
"Version": ver,
}, bson.M{
"$set": user,
}, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&result); err != nil {
return nil, err
}
return result, nil
}
func (b *Backend) GetUserByIdentity(ctx context.Context, idnName, idn string) (*identity.User, error) {
var user identity.User
if err := b.coll(collUsers).FindOne(ctx, bson.M{
"Identities": bson.M{
"$elemMatch": bson.M{ // FIXME *
"Name": idnName,
"Identity": idn,
},
},
}).Decode(&user); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &user, nil
}