/
persister_login.go
70 lines (55 loc) · 1.56 KB
/
persister_login.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
package sql
import (
"context"
"fmt"
"time"
"github.com/ory/kratos/corp"
"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"
"github.com/ory/x/sqlcon"
"github.com/ory/kratos/selfservice/flow/login"
)
var _ login.FlowPersister = new(Persister)
func (p *Persister) CreateLoginFlow(ctx context.Context, r *login.Flow) error {
r.NID = corp.ContextualizeNID(ctx, p.nid)
r.EnsureInternalContext()
return p.GetConnection(ctx).Create(r)
}
func (p *Persister) UpdateLoginFlow(ctx context.Context, r *login.Flow) error {
r.EnsureInternalContext()
cp := *r
cp.NID = corp.ContextualizeNID(ctx, p.nid)
return p.update(ctx, cp)
}
func (p *Persister) GetLoginFlow(ctx context.Context, id uuid.UUID) (*login.Flow, error) {
conn := p.GetConnection(ctx)
var r login.Flow
if err := conn.Where("id = ? AND nid = ?", id, corp.ContextualizeNID(ctx, p.nid)).First(&r); err != nil {
return nil, sqlcon.HandleError(err)
}
return &r, nil
}
func (p *Persister) ForceLoginFlow(ctx context.Context, id uuid.UUID) error {
return p.Transaction(ctx, func(ctx context.Context, tx *pop.Connection) error {
lr, err := p.GetLoginFlow(ctx, id)
if err != nil {
return err
}
lr.Refresh = true
return tx.Save(lr, "nid")
})
}
func (p *Persister) DeleteExpiredLoginFlows(ctx context.Context, expiresAt time.Time, limit int) error {
// #nosec G201
err := p.GetConnection(ctx).RawQuery(fmt.Sprintf(
"DELETE FROM %s WHERE expires_at <= ? LIMIT ?",
new(login.Flow).TableName(ctx),
),
expiresAt,
limit,
).Exec()
if err != nil {
return sqlcon.HandleError(err)
}
return nil
}