File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed
Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ type Config struct {
3030 AwsKeyId string `env:"AWS_KEY_ID"`
3131 AwsKeySecret string `env:"AWS_KEY_SECRET"`
3232
33+ AuthenticationMaxAge time.Duration `env:"AUTHENTICATION_MAX_AGE" envDefault:"336h"` // 14 days
3334 AuthenticationPasswordChallengeRateLimit int `env:"AUTHENTICATION_PASSWORD_RATE_LIMIT" envDefault:"5"` // per (person ID/email) and hour
3435 AuthenticationCaptchaChallengeRateLimit int `env:"AUTHENTICATION_CAPTCHA_RATE_LIMIT" envDefault:"5"` // per person ID and hour
3536 AuthorizationMaxAge time.Duration `env:"AUTHORIZATION_MAX_AGE" envDefault:"24h"`
Original file line number Diff line number Diff line change 1+ package jobs
2+
3+ import (
4+ "context"
5+ "time"
6+
7+ "github.com/avptp/brain/internal/generated/container"
8+ "github.com/avptp/brain/internal/generated/data/authentication"
9+ "github.com/avptp/brain/internal/generated/data/privacy"
10+ "github.com/madflojo/tasks"
11+ )
12+
13+ func cleanExpiredAuthentications (ctx context.Context , ctn * container.Container ) * tasks.Task {
14+ return & tasks.Task {
15+ Interval : time .Hour ,
16+ TaskFunc : func () error {
17+ cfg := ctn .GetConfig ()
18+ log := ctn .GetLogger ()
19+ data := ctn .GetData ()
20+
21+ allowCtx := privacy .DecisionContext (ctx , privacy .Allow )
22+
23+ vertices , err := data .Authentication .
24+ Delete ().
25+ Where (
26+ authentication .LastUsedAtLT (
27+ time .Now ().Add (- cfg .AuthenticationMaxAge ),
28+ ),
29+ ).
30+ Exec (allowCtx )
31+
32+ if err != nil {
33+ return err
34+ }
35+
36+ log .Info (
37+ "task completed: clean expired authentications" ,
38+ "vertices" , vertices ,
39+ )
40+
41+ return nil
42+ },
43+ ErrFunc : func (e error ) {
44+ log := ctn .GetLogger ()
45+
46+ log .Error (
47+ e .Error (),
48+ )
49+ },
50+ }
51+ }
Original file line number Diff line number Diff line change @@ -10,5 +10,6 @@ import (
1010type Job func (ctx context.Context , ctn * container.Container ) * tasks.Task
1111
1212var All = []Job {
13+ cleanExpiredAuthentications ,
1314 cleanExpiredAuthorizations ,
1415}
You can’t perform that action at this time.
0 commit comments