ory / kratos

Next-gen identity server replacing your Auth0, Okta, Firebase with hardened security and PassKeys, SMS, OIDC, Social Sign In, MFA, FIDO, TOTP and OTP, WebAuthn, passwordless and much more. Golang, headless, API-first. Available as a worry-free SaaS with the fairest pricing on the market!
https://www.ory.sh/kratos/?utm_source=github&utm_medium=banner&utm_campaign=kratos
Apache License 2.0
11.05k stars 950 forks source link

Add FlowId variable for use in Courier templates for use with API implementations and custom/native UI #3381

Open kulogix opened 1 year ago

kulogix commented 1 year ago

Preflight checklist

Describe your problem

Existing VerificationURL and RecoveryURL variables assumes use of /self-serve/ endpoints, and not compatible with custom UI for native API implementations. Add VerificationFlowId / RecoveryFlowId (or just FlowId) variable for those who want to easily customize the templates.

Would also be nice to add a bank of additional variables that can be registered in the main kratos.yml file, that can be used for brand name, domains, URLs, disclaimers, etc. Variables from this config section could then easily be referenced by any Go template for email and later SMS, using dot notation like Identity.

Describe your ideal solution

Add additional variable(s) for FlowId that can be used when composing custom email templates.

See https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates

Workarounds or alternatives

Use Go’s regex functions (see http://masterminds.github.io/sprig/strings.html#regexmatch-mustregexmatch) in the verification_code/valid/email.body.gotmpl and verification_code/valid/email.body.plaintext.gotmpl templates.

Example for verification_code/valid/email.body.gotmpl

Hello,

please verify your account by entering the following code:

{{ .VerificationCode }}

or clicking the following link:

{{ $flowParam := mustRegexFind "flow=([^&]+)" .VerificationURL }}
{{ $VerificationFlowId := mustRegexReplaceAll "flow=([^&]+)" $flowParam "${1}" }}
{{ $VerificationURL := print "http://127.0.0.1:8080/verify?code=" (urlquery .VerificationCode) "&id=" $VerificationFlowId }}
<a href="{{ $VerificationURL }}">{{ $VerificationURL }}</a>

Additional useful setup for testing/reproducing: Download Courier templates locally: OLD (Github no longer supports SVN)

brew install svn

svn export https://github.com/ory/kratos/trunk/courier/template/courier/builtin/templates $PWD/ory/kratos/courier-templates

NEW (more complicated process)

mkdir -p $PWD/ory/kratos/courier-templates

curl -L https://api.github.com/repos/ory/kratos/tarball --output kratos.tgz

MAIN_FOLDER=$(tar -tzf kratos.tgz | grep -o '^[^/]\+' | head -1)

tar -xzf kratos.tgz -C $PWD/ory/kratos/courier-templates --strip-components=6 "$MAIN_FOLDER/courier/template/courier/builtin/templates"

unset MAIN_FOLDER

rm kratos.tgz

Add template override path to ory/kratos/kratos.yml:

courier:
  template_override_path: /etc/config/courier-templates

Map local templates folder into Kratos container:

docker run -it --rm --name kratos ... -v $PWD/ory/kratos/courier-templates:/etc/config/courier-templates oryd/kratos:v1.0.0 serve --config /home/ory/kratos.yml --watch-courier

Version

v1.0.0

Additional Context

No response

aeneasr commented 1 year ago

That makes sense to me!