MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / CreateStepUpCode

Method CreateStepUpCode

internal/store/step_up.go:28–64  ·  view source on GitHub ↗

CreateStepUpCode replaces unused codes across the account and enforces three sends per rolling hour under the account lock. The code is returned once for delivery; the id lets the caller remove a failed delivery.

(userID, sessionID string)

Source from the content-addressed store, hash-verified

26// three sends per rolling hour under the account lock. The code is returned
27// once for delivery; the id lets the caller remove a failed delivery.
28func (s *Store) CreateStepUpCode(userID, sessionID string) (id, code string, expiresAt time.Time, err error) {
29 tx, err := s.db.Begin()
30 if err != nil {
31 return "", "", time.Time{}, fmt.Errorf("create step-up code: begin: %w", err)
32 }
33 defer tx.Rollback()
34 if err := s.lockUserRowTx(tx, userID); err != nil {
35 return "", "", time.Time{}, fmt.Errorf("create step-up code: lock user: %w", err)
36 }
37 current := time.Now().UTC()
38 var count int
39 var oldest *string
40 if err := tx.QueryRow(s.q(`SELECT COUNT(*), MIN(created_at) FROM step_up_codes WHERE user_id=? AND created_at>?`), userID, current.Add(-time.Hour).Format(time.RFC3339)).Scan(&count, &oldest); err != nil {
41 return "", "", time.Time{}, fmt.Errorf("count step-up codes: %w", err)
42 }
43 if count >= 3 {
44 return "", "", time.Time{}, &ErrStepUpSendLimit{RetryAfter: parseTime(*oldest).Add(time.Hour).Sub(current)}
45 }
46 value, err := rand.Int(rand.Reader, big.NewInt(1000000))
47 if err != nil {
48 return "", "", time.Time{}, fmt.Errorf("generate step-up code: %w", err)
49 }
50 id = newID()
51 code = fmt.Sprintf("%06d", value.Int64())
52 expiresAt = current.Add(10 * time.Minute)
53 ts := current.Format(time.RFC3339)
54 if _, err := tx.Exec(s.q(`UPDATE step_up_codes SET used_at=? WHERE user_id=? AND used_at IS NULL`), ts, userID); err != nil {
55 return "", "", time.Time{}, fmt.Errorf("replace step-up codes: %w", err)
56 }
57 if _, err := tx.Exec(s.q(`INSERT INTO step_up_codes (id,user_id,session_id,code_hash,expires_at,created_at) VALUES (?,?,?,?,?,?)`), id, userID, sessionID, stepUpCodeHash(id, code), expiresAt.Format(time.RFC3339), ts); err != nil {
58 return "", "", time.Time{}, fmt.Errorf("insert step-up code: %w", err)
59 }
60 if err := tx.Commit(); err != nil {
61 return "", "", time.Time{}, fmt.Errorf("create step-up code: commit: %w", err)
62 }
63 return id, code, expiresAt, nil
64}
65
66// RetireStepUpCode makes a code whose email delivery failed unusable. The row
67// stays, so the failed send still counts toward the hourly send cap.

Callers 4

TestStepUpCodeLifecycleFunction · 0.80
handleStepUpEmailMethod · 0.80

Calls 13

lockUserRowTxMethod · 0.95
qMethod · 0.95
parseTimeFunction · 0.85
newIDFunction · 0.85
stepUpCodeHashFunction · 0.85
BeginMethod · 0.80
RollbackMethod · 0.80
NowMethod · 0.65
ScanMethod · 0.65
QueryRowMethod · 0.65
AddMethod · 0.65
ExecMethod · 0.65

Tested by 3

TestStepUpCodeLifecycleFunction · 0.64