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
|
package models
import (
"context"
"time"
"code.crute.us/mcrute/ssh-proxy/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type AuthSessionStoreMongodb struct {
*db.MongoDbBasicStore[*AuthSession]
}
var _ AuthSessionStore = (*AuthSessionStoreMongodb)(nil)
func (s *AuthSessionStoreMongodb) getBy(ctx context.Context, field, value string) (*AuthSession, error) {
var out AuthSession
if err := s.Db.Collection(s.CollectionName).FindOne(ctx, &bson.M{
field: value,
"expires": bson.M{
"$gte": primitive.NewDateTimeFromTime(time.Now()),
},
}).Decode(&out); err != nil {
return nil, err
}
return &out, nil
}
func (s *AuthSessionStoreMongodb) GetByUserCode(ctx context.Context, userCode string) (*AuthSession, error) {
return s.getBy(ctx, "usercode", userCode)
}
func (s *AuthSessionStoreMongodb) GetByAccessCode(ctx context.Context, accessCode string) (*AuthSession, error) {
return s.getBy(ctx, "accesscode", accessCode)
}
func (s *AuthSessionStoreMongodb) Insert(ctx context.Context, session *AuthSession) error {
_, err := s.Db.Collection(s.CollectionName).InsertOne(ctx, session)
return err
}
|