mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-02-11 16:29:00 -06:00
Refactor email verification
This commit is contained in:
@@ -219,6 +219,7 @@ func run(ctx context.Context, cfg common.ConfigStore, stderr io.Writer, listener
|
||||
CountryCodeHeader: cfg.Get(common.CountryCodeHeaderKey),
|
||||
UserLimiter: userLimiter,
|
||||
SubscriptionLimits: subscriptionLimits,
|
||||
EmailVerifier: &portal.PortalEmailVerifier{},
|
||||
}
|
||||
|
||||
templatesBuilder := portal.NewTemplatesBuilder()
|
||||
|
||||
@@ -82,3 +82,7 @@ type AuditLog interface {
|
||||
RecordEvent(ctx context.Context, event *AuditLogEvent, source AuditLogSource)
|
||||
RecordEvents(ctx context.Context, events []*AuditLogEvent, source AuditLogSource)
|
||||
}
|
||||
|
||||
type EmailVerifier interface {
|
||||
VerifyEmail(ctx context.Context, email string) error
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/badoux/checkmail"
|
||||
"github.com/medama-io/go-useragent"
|
||||
|
||||
"github.com/PrivateCaptcha/PrivateCaptcha/pkg/common"
|
||||
@@ -18,6 +19,14 @@ var (
|
||||
errInvalidEmail = errors.New("email is not valid")
|
||||
)
|
||||
|
||||
type PortalEmailVerifier struct{}
|
||||
|
||||
var _ common.EmailVerifier = (*PortalEmailVerifier)(nil)
|
||||
|
||||
func (ev *PortalEmailVerifier) VerifyEmail(ctx context.Context, email string) error {
|
||||
return checkmail.ValidateFormat(email)
|
||||
}
|
||||
|
||||
type PortalMailer struct {
|
||||
Mailer emailpkg.Sender
|
||||
CDNURL string
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/PrivateCaptcha/PrivateCaptcha/pkg/db"
|
||||
dbgen "github.com/PrivateCaptcha/PrivateCaptcha/pkg/db/generated"
|
||||
"github.com/PrivateCaptcha/PrivateCaptcha/pkg/session"
|
||||
"github.com/badoux/checkmail"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
@@ -137,7 +136,7 @@ func (s *Server) postRegister(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
email := strings.TrimSpace(r.FormValue(common.ParamEmail))
|
||||
if err := checkmail.ValidateFormat(email); err != nil {
|
||||
if err := s.EmailVerifier.VerifyEmail(ctx, email); err != nil {
|
||||
slog.WarnContext(ctx, "Failed to validate email format", common.ErrAttr(err))
|
||||
data.EmailError = "Email address is not valid."
|
||||
s.render(w, r, registerContentsTemplate, data)
|
||||
|
||||
@@ -144,6 +144,7 @@ type Server struct {
|
||||
UserLimiter api.UserLimiter
|
||||
AuditLogsFunc AuditLogsConstructor
|
||||
SubscriptionLimits db.SubscriptionLimits
|
||||
EmailVerifier common.EmailVerifier
|
||||
}
|
||||
|
||||
func (s *Server) createSettingsTabs() []*SettingsTab {
|
||||
|
||||
@@ -77,6 +77,7 @@ func TestMain(m *testing.M) {
|
||||
DataCtx: dataCtx,
|
||||
PlatformCtx: platformCtx,
|
||||
SubscriptionLimits: &db.StubSubscriptionLimits{},
|
||||
EmailVerifier: &PortalEmailVerifier{},
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
@@ -135,6 +136,7 @@ func TestMain(m *testing.M) {
|
||||
CountryCodeHeader: cfg.Get(common.CountryCodeHeaderKey),
|
||||
UserLimiter: api.NewUserLimiter(store),
|
||||
SubscriptionLimits: db.NewSubscriptionLimits(common.StageTest, store, planService),
|
||||
EmailVerifier: &PortalEmailVerifier{},
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
Reference in New Issue
Block a user