Search in sources :

Example 1 with EmailException

use of org.keycloak.email.EmailException in project keycloak by keycloak.

the class VerifyEmail method sendVerifyEmail.

private Response sendVerifyEmail(KeycloakSession session, LoginFormsProvider forms, UserModel user, AuthenticationSessionModel authSession, EventBuilder event) throws UriBuilderException, IllegalArgumentException {
    RealmModel realm = session.getContext().getRealm();
    UriInfo uriInfo = session.getContext().getUri();
    int validityInSecs = realm.getActionTokenGeneratedByUserLifespan(VerifyEmailActionToken.TOKEN_TYPE);
    int absoluteExpirationInSecs = Time.currentTime() + validityInSecs;
    String authSessionEncodedId = AuthenticationSessionCompoundId.fromAuthSession(authSession).getEncodedId();
    VerifyEmailActionToken token = new VerifyEmailActionToken(user.getId(), absoluteExpirationInSecs, authSessionEncodedId, user.getEmail(), authSession.getClient().getClientId());
    UriBuilder builder = Urls.actionTokenBuilder(uriInfo.getBaseUri(), token.serialize(session, realm, uriInfo), authSession.getClient().getClientId(), authSession.getTabId());
    String link = builder.build(realm.getName()).toString();
    long expirationInMinutes = TimeUnit.SECONDS.toMinutes(validityInSecs);
    try {
        session.getProvider(EmailTemplateProvider.class).setAuthenticationSession(authSession).setRealm(realm).setUser(user).sendVerifyEmail(link, expirationInMinutes);
        event.success();
    } catch (EmailException e) {
        logger.error("Failed to send verification email", e);
        event.error(Errors.EMAIL_SEND_FAILED);
    }
    return forms.createResponse(UserModel.RequiredAction.VERIFY_EMAIL);
}
Also used : EmailException(org.keycloak.email.EmailException) VerifyEmailActionToken(org.keycloak.authentication.actiontoken.verifyemail.VerifyEmailActionToken)

Example 2 with EmailException

use of org.keycloak.email.EmailException in project keycloak by keycloak.

the class EmailEventListenerProvider method sendEmail.

private void sendEmail(Event event) {
    RealmModel realm = model.getRealm(event.getRealmId());
    UserModel user = session.users().getUserById(realm, event.getUserId());
    if (user != null && user.getEmail() != null && user.isEmailVerified()) {
        try {
            emailTemplateProvider.setRealm(realm).setUser(user).sendEvent(event);
        } catch (EmailException e) {
            log.error("Failed to send type mail", e);
        }
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) EmailException(org.keycloak.email.EmailException)

Example 3 with EmailException

use of org.keycloak.email.EmailException in project keycloak by keycloak.

the class FreeMarkerEmailTemplateProvider method processTemplate.

protected EmailTemplate processTemplate(String subjectKey, List<Object> subjectAttributes, String template, Map<String, Object> attributes) throws EmailException {
    try {
        Theme theme = getTheme();
        Locale locale = session.getContext().resolveLocale(user);
        attributes.put("locale", locale);
        Properties rb = new Properties();
        rb.putAll(theme.getMessages(locale));
        rb.putAll(realm.getRealmLocalizationTextsByLocale(locale.toLanguageTag()));
        attributes.put("msg", new MessageFormatterMethod(locale, rb));
        attributes.put("properties", theme.getProperties());
        String subject = new MessageFormat(rb.getProperty(subjectKey, subjectKey), locale).format(subjectAttributes.toArray());
        String textTemplate = String.format("text/%s", template);
        String textBody;
        try {
            textBody = freeMarker.processTemplate(attributes, textTemplate, theme);
        } catch (final FreeMarkerException e) {
            throw new EmailException("Failed to template plain text email.", e);
        }
        String htmlTemplate = String.format("html/%s", template);
        String htmlBody;
        try {
            htmlBody = freeMarker.processTemplate(attributes, htmlTemplate, theme);
        } catch (final FreeMarkerException e) {
            throw new EmailException("Failed to template html email.", e);
        }
        return new EmailTemplate(subject, textBody, htmlBody);
    } catch (Exception e) {
        throw new EmailException("Failed to template email", e);
    }
}
Also used : Locale(java.util.Locale) MessageFormat(java.text.MessageFormat) EmailException(org.keycloak.email.EmailException) Theme(org.keycloak.theme.Theme) FreeMarkerException(org.keycloak.theme.FreeMarkerException) Properties(java.util.Properties) MessageFormatterMethod(org.keycloak.theme.beans.MessageFormatterMethod) EmailException(org.keycloak.email.EmailException) IOException(java.io.IOException) FreeMarkerException(org.keycloak.theme.FreeMarkerException)

Example 4 with EmailException

use of org.keycloak.email.EmailException in project keycloak by keycloak.

the class FreeMarkerEmailTemplateProvider method send.

@Override
public void send(String subjectFormatKey, List<Object> subjectAttributes, String bodyTemplate, Map<String, Object> bodyAttributes) throws EmailException {
    try {
        EmailTemplate email = processTemplate(subjectFormatKey, subjectAttributes, bodyTemplate, bodyAttributes);
        send(email.getSubject(), email.getTextBody(), email.getHtmlBody());
    } catch (EmailException e) {
        throw e;
    } catch (Exception e) {
        throw new EmailException("Failed to template email", e);
    }
}
Also used : EmailException(org.keycloak.email.EmailException) EmailException(org.keycloak.email.EmailException) IOException(java.io.IOException) FreeMarkerException(org.keycloak.theme.FreeMarkerException)

Example 5 with EmailException

use of org.keycloak.email.EmailException in project keycloak by keycloak.

the class FreeMarkerEmailTemplateProvider method addLinkInfoIntoAttributes.

/**
 * Add link info into template attributes.
 *
 * @param link to add
 * @param expirationInMinutes to add
 * @param attributes to add link info into
 */
protected void addLinkInfoIntoAttributes(String link, long expirationInMinutes, Map<String, Object> attributes) throws EmailException {
    attributes.put("link", link);
    attributes.put("linkExpiration", expirationInMinutes);
    KeycloakUriInfo uriInfo = session.getContext().getUri();
    URI baseUri = uriInfo.getBaseUri();
    try {
        Locale locale = session.getContext().resolveLocale(user);
        attributes.put("linkExpirationFormatter", new LinkExpirationFormatterMethod(getTheme().getMessages(locale), locale));
        attributes.put("url", new UrlBean(realm, getTheme(), baseUri, null));
    } catch (IOException e) {
        throw new EmailException("Failed to template email", e);
    }
}
Also used : Locale(java.util.Locale) KeycloakUriInfo(org.keycloak.models.KeycloakUriInfo) UrlBean(org.keycloak.forms.login.freemarker.model.UrlBean) EmailException(org.keycloak.email.EmailException) LinkExpirationFormatterMethod(org.keycloak.theme.beans.LinkExpirationFormatterMethod) IOException(java.io.IOException) URI(java.net.URI)

Aggregations

EmailException (org.keycloak.email.EmailException)9 IOException (java.io.IOException)3 EventBuilder (org.keycloak.events.EventBuilder)3 AuthenticationSessionModel (org.keycloak.sessions.AuthenticationSessionModel)3 Locale (java.util.Locale)2 Response (javax.ws.rs.core.Response)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 RealmModel (org.keycloak.models.RealmModel)2 FreeMarkerException (org.keycloak.theme.FreeMarkerException)2 URI (java.net.URI)1 MessageFormat (java.text.MessageFormat)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ExecuteActionsActionToken (org.keycloak.authentication.actiontoken.execactions.ExecuteActionsActionToken)1 IdpVerifyAccountLinkActionToken (org.keycloak.authentication.actiontoken.idpverifyemail.IdpVerifyAccountLinkActionToken)1 ResetCredentialsActionToken (org.keycloak.authentication.actiontoken.resetcred.ResetCredentialsActionToken)1