Skip to content

Commit

Permalink
Add channel to magic link senders
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Sep 14, 2024
1 parent 348951f commit 62fe676
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 6 deletions.
9 changes: 9 additions & 0 deletions v-api/src/context/magic_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ where
client_id: TypedUuid<MagicLinkId>,
redirect_uri: &Url,
medium: MagicLinkMedium,
channel: &str,
scope: &str,
expiration: DateTime<Utc>,
recipient: &str,
Expand Down Expand Up @@ -338,6 +339,7 @@ where
magic_link_client_id: client_id,
recipient: recipient_signature.to_string(),
medium: medium.to_string(),
channel: channel.to_string(),
redirect_uri: redirect_uri.to_string(),
scope: scope.to_string(),
nonce_signature: signature,
Expand Down Expand Up @@ -484,6 +486,7 @@ mod tests {
magic_link_client_id: arg.magic_link_client_id,
recipient: arg.recipient,
medium: arg.medium,
channel: arg.channel,
redirect_uri: arg.redirect_uri,
scope: arg.scope,
nonce_signature: arg.nonce_signature,
Expand All @@ -505,6 +508,7 @@ mod tests {
TypedUuid::new_v4(),
&Url::parse("http://127.0.0.1").unwrap(),
MagicLinkMedium::Email,
"all",
"",
Utc::now().add(Duration::seconds(60)),
"[email protected]",
Expand All @@ -525,6 +529,7 @@ mod tests {
magic_link_client_id: arg.magic_link_client_id,
recipient: arg.recipient,
medium: arg.medium,
channel: arg.channel,
redirect_uri: arg.redirect_uri,
scope: arg.scope,
nonce_signature: arg.nonce_signature,
Expand Down Expand Up @@ -566,6 +571,7 @@ mod tests {
TypedUuid::new_v4(),
&Url::parse("http://127.0.0.1").unwrap(),
MagicLinkMedium::Email,
"all",
"",
Utc::now().add(Duration::seconds(60)),
"[email protected]",
Expand All @@ -587,6 +593,7 @@ mod tests {
magic_link_client_id: arg.magic_link_client_id,
recipient: arg.recipient,
medium: arg.medium,
channel: arg.channel,
redirect_uri: arg.redirect_uri,
scope: arg.scope,
nonce_signature: arg.nonce_signature,
Expand Down Expand Up @@ -630,6 +637,7 @@ mod tests {
TypedUuid::new_v4(),
&Url::parse("http://127.0.0.1").unwrap(),
MagicLinkMedium::Email,
"all",
"",
Utc::now().add(Duration::seconds(60)),
"[email protected]",
Expand Down Expand Up @@ -657,6 +665,7 @@ mod tests {
magic_link_client_id: TypedUuid::new_v4(),
recipient: String::new(),
medium: String::new(),
channel: String::new(),
redirect_uri: String::new(),
scope: String::new(),
nonce_signature: signature,
Expand Down
2 changes: 1 addition & 1 deletion v-api/src/endpoints/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ mod macros {

#[endpoint {
method = POST,
path = "/login/magic/{medium}/send"
path = "/login/magic/{channel}/send"
}]
pub async fn magic_link_send(
rqctx: RequestContext<$context_type>,
Expand Down
12 changes: 7 additions & 5 deletions v-api/src/endpoints/login/magic_link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ pub mod client;

#[derive(Debug, Deserialize, JsonSchema)]
pub struct MagicLinkPath {
medium: MagicLinkMedium,
channel: String,
}

#[derive(Debug, Deserialize, JsonSchema)]
pub struct MagicLinkSendRequest {
medium: MagicLinkMedium,
secret: String,
recipient: String,
redirect_uri: Url,
Expand Down Expand Up @@ -65,7 +66,8 @@ where
Ok(HttpResponseOk(
magic_link_send_op_inner(
ctx,
path.medium,
body.medium,
&path.channel,
body.secret,
body.recipient,
body.redirect_uri,
Expand All @@ -80,6 +82,7 @@ where
async fn magic_link_send_op_inner<T>(
ctx: &VContext<T>,
medium: MagicLinkMedium,
channel: &str,
secret: String,
recipient: String,
redirect_uri: Url,
Expand Down Expand Up @@ -114,6 +117,7 @@ where
client.id,
&redirect_uri,
medium,
channel,
&scope,
Utc::now().add(Duration::seconds(expires_in)),
&recipient,
Expand All @@ -139,9 +143,7 @@ impl From<MagicLinkSendError> for HttpError {
MagicLinkSendError::NoMessageSender(medium) => {
internal_error(format!("No message sender is available for {}", medium))
}
MagicLinkSendError::Send(err) => {
internal_error(err.to_string())
}
MagicLinkSendError::Send(err) => internal_error(err.to_string()),
MagicLinkSendError::Signing(err) => ResourceError::InternalError(err).into(),
MagicLinkSendError::Storage(err) => ResourceError::InternalError(err).into(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CREATE TABLE magic_link_attempt(
magic_link_client_id UUID REFERENCES magic_link_client (id) NOT NULL,

medium VARCHAR NOT NULL,
channel VARCHAR NOT NULL,
recipient VARCHAR NOT NULL,
redirect_uri VARCHAR NOT NULL,
scope VARCHAR NOT NULL DEFAULT '',
Expand Down
1 change: 1 addition & 0 deletions v-model/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub struct MagicLinkAttemptModel {
pub magic_link_client_id: Uuid,
// TODO: This needs to be the MagicLinkMedium enum
pub medium: String,
pub channel: String,
pub recipient: String,
pub redirect_uri: String,
pub scope: String,
Expand Down
2 changes: 2 additions & 0 deletions v-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ pub struct MagicLinkAttempt {
pub magic_link_client_id: TypedUuid<MagicLinkId>,
pub recipient: String,
pub medium: String,
pub channel: String,
pub redirect_uri: String,
pub scope: String,
pub nonce_signature: String,
Expand All @@ -584,6 +585,7 @@ impl From<MagicLinkAttemptModel> for MagicLinkAttempt {
magic_link_client_id: TypedUuid::from_untyped_uuid(value.magic_link_client_id),
recipient: value.recipient,
medium: value.medium,
channel: value.channel,
redirect_uri: value.redirect_uri,
scope: value.scope,
nonce_signature: value.nonce_signature,
Expand Down
1 change: 1 addition & 0 deletions v-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ diesel::table! {
attempt_state -> MlinkAttemptState,
magic_link_client_id -> Uuid,
medium -> Varchar,
channel -> Varchar,
recipient -> Varchar,
redirect_uri -> Varchar,
scope -> Varchar,
Expand Down
1 change: 1 addition & 0 deletions v-model/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ pub struct MagicLinkAttemptFilter {
pub client_id: Option<Vec<TypedUuid<OAuthClientId>>>,
pub attempt_state: Option<Vec<MagicLinkAttemptState>>,
pub medium: Option<Vec<String>>,
pub channel: Option<Vec<String>>,
pub signature: Option<Vec<String>>,
}

Expand Down
5 changes: 5 additions & 0 deletions v-model/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ impl MagicLinkAttemptStore for PostgresStore {
client_id,
attempt_state,
medium,
channel,
signature,
} = filter;

Expand Down Expand Up @@ -1089,6 +1090,10 @@ impl MagicLinkAttemptStore for PostgresStore {
query = query.filter(magic_link_attempt::medium.eq_any(medium));
}

if let Some(channel) = channel {
query = query.filter(magic_link_attempt::channel.eq_any(channel));
}

if let Some(signature) = signature {
query = query.filter(magic_link_attempt::nonce_signature.eq_any(signature));
}
Expand Down
2 changes: 2 additions & 0 deletions v-model/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ async fn test_magic_link_attempt() {
magic_link_client_id: client_id,
recipient: String::new(),
medium: String::new(),
channel: String::new(),
redirect_uri: String::new(),
scope: String::new(),
nonce_signature: "xxxxx".to_string(),
Expand Down Expand Up @@ -468,6 +469,7 @@ async fn test_magic_link_attempt() {
magic_link_client_id: client_id,
recipient: String::new(),
medium: String::new(),
channel: String::new(),
redirect_uri: String::new(),
scope: String::new(),
nonce_signature: "xxxxx".to_string(),
Expand Down

0 comments on commit 62fe676

Please sign in to comment.