Skip to content

Commit 15052a3

Browse files
committed
Add automatic cleanup of authentications.
1 parent aa7a9fa commit 15052a3

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

internal/config/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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"`

internal/jobs/authentications.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

internal/jobs/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ import (
1010
type Job func(ctx context.Context, ctn *container.Container) *tasks.Task
1111

1212
var All = []Job{
13+
cleanExpiredAuthentications,
1314
cleanExpiredAuthorizations,
1415
}

0 commit comments

Comments
 (0)