71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package verification
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/tech/sendico/pkg/model"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
type TokenKind = string
|
|
|
|
const (
|
|
TokenKindOTP TokenKind = "otp"
|
|
TokenKindLink TokenKind = "link"
|
|
)
|
|
|
|
type Request struct {
|
|
AccountRef bson.ObjectID
|
|
Purpose model.VerificationPurpose
|
|
Target string
|
|
Ttl time.Duration
|
|
Kind TokenKind
|
|
MaxRetries *int
|
|
Cooldown *time.Duration
|
|
IdempotencyKey *string // Optional key to make Create idempotent for retries.
|
|
}
|
|
|
|
func newRequest(accountRef bson.ObjectID, purpose model.VerificationPurpose, target string, kind TokenKind) *Request {
|
|
return &Request{
|
|
AccountRef: accountRef,
|
|
Purpose: purpose,
|
|
Target: target,
|
|
Kind: kind,
|
|
Ttl: 15 * time.Minute, // default TTL for verification tokens
|
|
}
|
|
}
|
|
|
|
func NewLinkRequest(accountRef bson.ObjectID, purpose model.VerificationPurpose, target string) *Request {
|
|
return newRequest(accountRef, purpose, target, TokenKindLink)
|
|
}
|
|
|
|
func NewOTPRequest(accountRef bson.ObjectID, purpose model.VerificationPurpose, target string) *Request {
|
|
return newRequest(accountRef, purpose, target, TokenKindOTP)
|
|
}
|
|
|
|
func (r *Request) WithTTL(ttl time.Duration) *Request {
|
|
r.Ttl = ttl
|
|
return r
|
|
}
|
|
|
|
func (r *Request) WithMaxRetries(maxRetries int) *Request {
|
|
r.MaxRetries = &maxRetries
|
|
return r
|
|
}
|
|
|
|
func (r *Request) WithCooldown(cooldown time.Duration) *Request {
|
|
r.Cooldown = &cooldown
|
|
return r
|
|
}
|
|
|
|
func (r *Request) WithIdempotencyKey(key string) *Request {
|
|
normalized := strings.TrimSpace(key)
|
|
if normalized == "" {
|
|
r.IdempotencyKey = nil
|
|
return r
|
|
}
|
|
r.IdempotencyKey = &normalized
|
|
return r
|
|
}
|