From 953ec49184f984f911ce7b55c326292369cdab9b Mon Sep 17 00:00:00 2001 From: Petr Vanek Date: Wed, 29 Jul 2026 08:56:49 +0200 Subject: [PATCH] feat: static passcode generator for tests New type of passcode charset is created: "static". It always returns a predefined code - `123456` for now. This code generator is not targeted to real usage, its main purpose is to serve static codes in tests using Hanko in e.g. Docker compose with custom applications. --- backend/config/config_email.go | 1 + backend/crypto/passcode.go | 13 +++++++++++++ backend/crypto/passcode_test.go | 12 ++++++++++++ backend/flow_api/services/passcode.go | 2 ++ backend/json_schema/hanko.config.json | 7 ++++--- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/backend/config/config_email.go b/backend/config/config_email.go index c57823259..bb35354e9 100644 --- a/backend/config/config_email.go +++ b/backend/config/config_email.go @@ -38,6 +38,7 @@ type PasscodeCharset string var ( PasscodeCharsetNumeric PasscodeCharset = "numeric" PasscodeCharsetAlphanumeric PasscodeCharset = "alphanumeric" + PasscodeCharsetStatic PasscodeCharset = "static" ) func (e *Email) Validate() error { diff --git a/backend/crypto/passcode.go b/backend/crypto/passcode.go index b20e15b7d..27304586f 100644 --- a/backend/crypto/passcode.go +++ b/backend/crypto/passcode.go @@ -48,3 +48,16 @@ func (a *alphanumericPasscodeGenerator) Generate() (string, error) { } return string(b), nil } + +type staticPasscodeGenerator struct { +} + +func NewStaticPasscodeGenerator() PasscodeGenerator { + return &staticPasscodeGenerator{} +} + +func (g *staticPasscodeGenerator) Generate() (string, error) { + // this is just a POC for now. I can imagine config option for it, or an env variable... + n := 123456 + return fmt.Sprintf("%06d", n), nil +} diff --git a/backend/crypto/passcode_test.go b/backend/crypto/passcode_test.go index 55bd8e7bf..700b2f257 100644 --- a/backend/crypto/passcode_test.go +++ b/backend/crypto/passcode_test.go @@ -51,3 +51,15 @@ func TestAlphanumericPasscodeGenerator_Generate_Different_Codes(t *testing.T) { assert.NotEqual(t, passcode1, passcode2) } + +func TestStaticPasscodeGenerator_Generate(t *testing.T) { + pg := NewStaticPasscodeGenerator() + passcode1, err := pg.Generate() + + assert.NoError(t, err) + assert.NotEmpty(t, passcode1) + assert.Equal(t, 6, len(passcode1)) + + passcode2, err := pg.Generate() + assert.Equal(t, passcode1, passcode2) +} diff --git a/backend/flow_api/services/passcode.go b/backend/flow_api/services/passcode.go index 4892dee65..2c9b8ccb7 100644 --- a/backend/flow_api/services/passcode.go +++ b/backend/flow_api/services/passcode.go @@ -116,6 +116,8 @@ func (s *passcode) SendPasscode(tx *pop.Connection, p SendPasscodeParams) (*Send switch p.Cfg.Email.PasscodeCharset { case config.PasscodeCharsetAlphanumeric: passcodeGenerator = crypto.NewAlphanumericPasscodeGenerator() + case config.PasscodeCharsetStatic: + passcodeGenerator = crypto.NewStaticPasscodeGenerator() } code, err := passcodeGenerator.Generate() if err != nil { diff --git a/backend/json_schema/hanko.config.json b/backend/json_schema/hanko.config.json index fa8ff3bb2..92f6068a8 100644 --- a/backend/json_schema/hanko.config.json +++ b/backend/json_schema/hanko.config.json @@ -643,9 +643,10 @@ "type": "string", "enum": [ "numeric", - "alphanumeric" + "alphanumeric", + "static" ], - "description": "`passcode_charset` specifies the characters that can be used in passcodes.\nE.g. `numeric` allows only numbers, `alphanumeric` allows both numbers and letters.", + "description": "`passcode_charset` specifies the characters that can be used in passcodes.\nE.g. `numeric` allows only numbers, `alphanumeric` allows both numbers and letters, `static` returns always the same passcode 123456.\nThe `static` should be used in local test runs only.", "default": "numeric" }, "require_verification": { @@ -2018,4 +2019,4 @@ } }, "title": "Config" -} \ No newline at end of file +}