Using FreeMarkerEmailTemplateProvider in an Authenticator SPI

422 views
Skip to first unread message

Simon.L...@eventim.de

unread,
Jan 20, 2022, 7:03:25 AM1/20/22
to keyclo...@googlegroups.com
Hi,

we're currently implementing a small custom Authenticator to mirror some legacy behaviour, and we need to send an email to the user in there. I've implemented this now with a regular EmailSenderProvider, which I can acquire with 'keycloakSession.getProvider(...)'. I can see there's also a FreeMarkerEmailTemplateProvider class in the org.keycloak.email package, which would be much more convenient to actually template the email using a (plain text and html?) freemarker template. However, using 'keycloakSession.getProvider(FreeMarkerEmailTemplateProvider.class)' seems to just return null. So I suppose I have 2 questions:

1.) Can the FreeMarkerEmailTemplateProvider be acquired in an SPI?
2.) If the answer is yes, where in the resulting JAR do the templates need to be so they can be accessed by the Provider?

Best regards,
--
Simon Levermann
Software Development Expert Cross Product Services

simon.l...@eventim.de | www.eventim.de <http://www.eventim.de>
CTS EVENTIM Solutions GmbH | Contrescarpe 75A | D- 28195 Bremen

Verwaltungsanschrift: Contrescarpe 75A– D- 28195 Bremen // Geschäftsführer: Alexander Ruoff, Christoph Bodi
Sitz: Bremen, HRB 19598 – Ust.-ID Nr.: DE 211161916


Simon.L...@eventim.de

unread,
Jan 20, 2022, 8:36:03 AM1/20/22
to keyclo...@googlegroups.com
I've actually been able to solve this by simply constructing my own instance with "new FreeMarkerEmailTemplateProvider(keycloakSession, new FreeMarkerUtil())".

The templates need to be structured into a directory structure like this, then a call to templateProvider.send() using "authenticator-email.ftl" as the template name will correctly format an email with both plain text and html email and send it to the user.

src/main/resources
├── META-INF
│   └── services
│   └── org.keycloak.authentication.AuthenticatorFactory
└── theme-resources
├── messages
│   ├── messages_de.properties
│   └── messages_en.properties
└── templates
├── authenticator-form.ftl
├── html
│   └── authenticator-email.ftl
└── text
└── authenticator-email.ftl

7 directories, 6 files

Cheers,
Simon



Reply all
Reply to author
Forward
0 new messages