/
clientmanager_store.go
179 lines (144 loc) · 4.23 KB
/
clientmanager_store.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
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package clientmanager
import (
"context"
"errors"
"time"
"github.com/ory/fosite"
"github.com/samber/lo"
"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"
"github.com/trustbloc/vcs/pkg/oauth2client"
"github.com/trustbloc/vcs/pkg/storage/mongodb"
)
const (
ClientsSegment = "fosite_clients"
BlacklistedJTIsSegment = "fosite_blacklisted_jtis"
)
var _ fosite.ClientManager = (*Store)(nil)
type Store struct {
mongoClient *mongodb.Client
}
func NewStore(
ctx context.Context,
mongoClient *mongodb.Client,
) (*Store, error) {
cl := &Store{
mongoClient: mongoClient,
}
if err := cl.migrate(ctx); err != nil {
return nil, err
}
return cl, nil
}
func (s *Store) migrate(ctx context.Context) error {
indexes := map[string][]mongo.IndexModel{
ClientsSegment: {
{
Keys: map[string]interface{}{
"_lookupId": -1,
},
Options: options.Index().SetUnique(true),
},
},
BlacklistedJTIsSegment: {
{
Keys: map[string]interface{}{
"_lookupId": -1,
},
Options: options.Index().SetUnique(true),
},
{
Keys: map[string]interface{}{
"expireAt": 1,
},
Options: options.Index().SetExpireAfterSeconds(0),
},
},
}
for collection, targetIndexes := range indexes {
if _, err := s.mongoClient.Database().Collection(collection).Indexes().
CreateMany(ctx, targetIndexes); err != nil {
return err
}
}
return nil
}
// GetClient loads the client by its ID or returns an error
// if the client does not exist or another error occurred.
func (s *Store) GetClient(ctx context.Context, id string) (fosite.Client, error) {
if id == "" { // for pre-auth, we don't have client
return &fosite.DefaultClient{}, nil
}
return getInternal[oauth2client.Client](ctx, s.mongoClient, ClientsSegment, id)
}
// ClientAssertionJWTValid returns an error if the JTI is
// known or the DB check failed and nil if the JTI is not known.
func (s *Store) ClientAssertionJWTValid(ctx context.Context, jti string) error {
_, err := getInternal[string](ctx, s.mongoClient, BlacklistedJTIsSegment, jti)
if errors.Is(err, ErrDataNotFound) {
return nil
}
return fosite.ErrJTIKnown
}
// SetClientAssertionJWT marks a JTI as known for the given
// expiry time. Before inserting the new JTI, it will clean
// up any existing JTIs that have expired as those tokens can
// not be replayed due to the expiry.
func (s *Store) SetClientAssertionJWT(ctx context.Context, jti string, exp time.Time) error {
collection := s.mongoClient.Database().Collection(BlacklistedJTIsSegment)
obj := &genericDocument[string]{
ID: primitive.ObjectID{},
LookupID: jti,
Record: jti,
ExpireAt: lo.ToPtr(exp),
}
_, err := collection.InsertOne(ctx, obj)
return err
}
func (s *Store) InsertClient(ctx context.Context, client *oauth2client.Client) (string, error) {
collection := s.mongoClient.Database().Collection(ClientsSegment)
obj := &genericDocument[*oauth2client.Client]{
ID: primitive.ObjectID{},
LookupID: client.ID,
Record: client,
}
result, err := collection.InsertOne(ctx, obj)
if err != nil {
return "", err
}
insertedID := result.InsertedID.(primitive.ObjectID) //nolint: errcheck
return insertedID.Hex(), nil
}
type genericDocument[T any] struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Record T `bson:"record"`
LookupID string `bson:"_lookupId"`
ExpireAt *time.Time `bson:"expireAt,omitempty"`
}
func getInternal[T any](
ctx context.Context,
mongoClient *mongodb.Client,
dbCollection string,
lookupID string,
) (*T, error) {
var doc genericDocument[T]
collection := mongoClient.Database().Collection(dbCollection)
err := collection.FindOne(ctx, bson.M{"_lookupId": lookupID}).Decode(&doc)
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, ErrDataNotFound
}
if err != nil {
return nil, err
}
if doc.ExpireAt != nil && doc.ExpireAt.Before(time.Now().UTC()) {
// due to nature of mongodb ttlIndex works every minute, so it can be a situation when we receive expired doc
return nil, ErrDataNotFound
}
return &doc.Record, nil
}