You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-11-20 12:02:22 +03:00
Actually send emails
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use mas_config::RootConfig;
|
||||
use mas_email::Mailer;
|
||||
use mas_jose::StaticKeystore;
|
||||
use mas_static_files::filter as static_files;
|
||||
use mas_templates::Templates;
|
||||
@@ -42,6 +43,7 @@ pub fn root(
|
||||
pool: &PgPool,
|
||||
templates: &Templates,
|
||||
key_store: &Arc<StaticKeystore>,
|
||||
mailer: &Mailer,
|
||||
config: &RootConfig,
|
||||
) -> BoxedFilter<(impl Reply,)> {
|
||||
let health = health(pool);
|
||||
@@ -49,6 +51,7 @@ pub fn root(
|
||||
let views = views(
|
||||
pool,
|
||||
templates,
|
||||
mailer,
|
||||
&config.oauth2,
|
||||
&config.csrf,
|
||||
&config.cookies,
|
||||
|
||||
@@ -12,8 +12,10 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use lettre::{message::Mailbox, Address};
|
||||
use mas_config::{CookiesConfig, CsrfConfig};
|
||||
use mas_data_model::BrowserSession;
|
||||
use mas_email::Mailer;
|
||||
use mas_storage::{
|
||||
user::{
|
||||
add_user_email, get_user_email, get_user_emails, remove_user_email,
|
||||
@@ -21,7 +23,7 @@ use mas_storage::{
|
||||
},
|
||||
PostgresqlBackend,
|
||||
};
|
||||
use mas_templates::{AccountEmailsContext, TemplateContext, Templates};
|
||||
use mas_templates::{AccountEmailsContext, EmailVerificationContext, TemplateContext, Templates};
|
||||
use mas_warp_utils::{
|
||||
errors::WrapError,
|
||||
filters::{
|
||||
@@ -35,14 +37,18 @@ use mas_warp_utils::{
|
||||
use serde::Deserialize;
|
||||
use sqlx::{pool::PoolConnection, PgExecutor, PgPool, Postgres, Transaction};
|
||||
use tracing::info;
|
||||
use url::Url;
|
||||
use warp::{filters::BoxedFilter, reply::html, Filter, Rejection, Reply};
|
||||
|
||||
pub(super) fn filter(
|
||||
pool: &PgPool,
|
||||
templates: &Templates,
|
||||
mailer: &Mailer,
|
||||
csrf_config: &CsrfConfig,
|
||||
cookies_config: &CookiesConfig,
|
||||
) -> BoxedFilter<(Box<dyn Reply>,)> {
|
||||
let mailer = mailer.clone();
|
||||
|
||||
let get = with_templates(templates)
|
||||
.and(encrypted_cookie_saver(cookies_config))
|
||||
.and(updated_csrf_token(cookies_config, csrf_config))
|
||||
@@ -51,6 +57,7 @@ pub(super) fn filter(
|
||||
.and_then(get);
|
||||
|
||||
let post = with_templates(templates)
|
||||
.and(warp::any().map(move || mailer.clone()))
|
||||
.and(encrypted_cookie_saver(cookies_config))
|
||||
.and(updated_csrf_token(cookies_config, csrf_config))
|
||||
.and(session(pool, cookies_config))
|
||||
@@ -108,6 +115,7 @@ async fn render(
|
||||
|
||||
async fn post(
|
||||
templates: Templates,
|
||||
mailer: Mailer,
|
||||
cookie_saver: EncryptedCookieSaver,
|
||||
csrf_token: CsrfToken,
|
||||
mut session: BrowserSession<PostgresqlBackend>,
|
||||
@@ -131,10 +139,28 @@ async fn post(
|
||||
}
|
||||
Form::ResendConfirmation { data } => {
|
||||
let id: i64 = data.parse().wrap_error()?;
|
||||
info!(
|
||||
email.id = id,
|
||||
"Not implemented yet: re-send confirmation email"
|
||||
|
||||
let email: Address = get_user_email(&mut txn, &session.user, id)
|
||||
.await
|
||||
.wrap_error()?
|
||||
.email
|
||||
.parse()
|
||||
.wrap_error()?;
|
||||
|
||||
let mailbox = Mailbox::new(Some(session.user.username.clone()), email);
|
||||
|
||||
// TODO: actually generate a verification link
|
||||
let context = EmailVerificationContext::new(
|
||||
session.user.clone().into(),
|
||||
Url::parse("https://example.com/verify").unwrap(),
|
||||
);
|
||||
|
||||
mailer
|
||||
.send_verification_email(mailbox, &context)
|
||||
.await
|
||||
.wrap_error()?;
|
||||
|
||||
info!(email.id = id, "Verification email sent");
|
||||
}
|
||||
Form::SetPrimary { data } => {
|
||||
let id = data.parse().wrap_error()?;
|
||||
|
||||
@@ -17,6 +17,7 @@ mod password;
|
||||
|
||||
use mas_config::{CookiesConfig, CsrfConfig};
|
||||
use mas_data_model::BrowserSession;
|
||||
use mas_email::Mailer;
|
||||
use mas_storage::{
|
||||
user::{count_active_sessions, get_user_emails},
|
||||
PostgresqlBackend,
|
||||
@@ -40,6 +41,7 @@ use self::{emails::filter as emails, password::filter as password};
|
||||
pub(super) fn filter(
|
||||
pool: &PgPool,
|
||||
templates: &Templates,
|
||||
mailer: &Mailer,
|
||||
csrf_config: &CsrfConfig,
|
||||
cookies_config: &CookiesConfig,
|
||||
) -> BoxedFilter<(Box<dyn Reply>,)> {
|
||||
@@ -53,7 +55,7 @@ pub(super) fn filter(
|
||||
|
||||
let index = warp::path::end().and(get);
|
||||
let password = password(pool, templates, csrf_config, cookies_config);
|
||||
let emails = emails(pool, templates, csrf_config, cookies_config);
|
||||
let emails = emails(pool, templates, mailer, csrf_config, cookies_config);
|
||||
|
||||
let filter = index.or(password).unify().or(emails).unify();
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
use mas_config::{CookiesConfig, CsrfConfig, OAuth2Config};
|
||||
use mas_email::Mailer;
|
||||
use mas_templates::Templates;
|
||||
use sqlx::PgPool;
|
||||
use warp::{filters::BoxedFilter, Filter, Reply};
|
||||
@@ -36,12 +37,13 @@ pub(crate) use self::{
|
||||
pub(super) fn filter(
|
||||
pool: &PgPool,
|
||||
templates: &Templates,
|
||||
mailer: &Mailer,
|
||||
oauth2_config: &OAuth2Config,
|
||||
csrf_config: &CsrfConfig,
|
||||
cookies_config: &CookiesConfig,
|
||||
) -> BoxedFilter<(Box<dyn Reply>,)> {
|
||||
let index = index(pool, templates, oauth2_config, csrf_config, cookies_config);
|
||||
let account = account(pool, templates, csrf_config, cookies_config);
|
||||
let account = account(pool, templates, mailer, csrf_config, cookies_config);
|
||||
let login = login(pool, templates, csrf_config, cookies_config);
|
||||
let register = register(pool, templates, csrf_config, cookies_config);
|
||||
let logout = logout(pool, cookies_config);
|
||||
|
||||
Reference in New Issue
Block a user