1package idp
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "log/slog"
8 "time"
9
10 "tug/internal/service/identity"
11 "tug/internal/service/refusalcap"
12 "tug/internal/storage"
13)
14
15const (
16 ActionLoginSuccess = "login.success"
17 ActionLoginRefused = "login.refused"
18)
19
20type clientDetail struct {
21 Client string `json:"client"`
22}
23
24func loginSuccess(userID, clientID string) storage.AppendAuditInput {
25 detail, _ := json.Marshal(clientDetail{Client: clientID})
26 return storage.AppendAuditInput{Actor: userID, Action: ActionLoginSuccess, Subject: userID, Detail: string(detail)}
27}
28
29func refusalsOrOwn(c *refusalcap.Cap, store *storage.Store, now func() time.Time, logger *slog.Logger) *refusalcap.Cap {
30 if c != nil {
31 return c
32 }
33 return refusalcap.New(store, now, logger)
34}
35
36func (v *Verifier) loginRefused(ctx context.Context, clientID string, err error) {
37 var wrong *identity.WrongPassword
38 if !errors.As(err, &wrong) {
39 return
40 }
41 detail, _ := json.Marshal(clientDetail{Client: clientID})
42 v.deps.refusals.Refused(ctx, storage.AppendAuditInput{Actor: "system", Action: ActionLoginRefused, Subject: wrong.UserID, Detail: string(detail)})
43}