Skip to content

Commit

Permalink
Fix telnyx auth api response parsing
Browse files Browse the repository at this point in the history
This is clearly a terrible bug that would authenticate anyone regardles
of if they input the correct code. I'm not sure if the API changed out
from under me, or more likley I templated this in and didn't follow up
to finish it.

Co-authored-by: Chiara Siem <chiara@althea.systems>
  • Loading branch information
jkilpatr and ch-iara committed Sep 19, 2024
1 parent 8e6ac0d commit 6a5ff1b
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions rita_client_registration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use althea_types::{ExitClientIdentity, Identity, WgKey};
use awc::error::SendRequestError;
use awc::error::{JsonPayloadError, SendRequestError};
use phonenumber::PhoneNumber;
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -50,6 +50,14 @@ impl Display for TextApiError {

impl Error for TextApiError {}

impl From<JsonPayloadError> for TextApiError {
fn from(value: JsonPayloadError) -> Self {
TextApiError::InternalServerError {
error: value.to_string(),
}
}
}

impl From<SendRequestError> for TextApiError {
fn from(value: SendRequestError) -> Self {
TextApiError::SendRequestError { error: value }
Expand Down Expand Up @@ -236,6 +244,18 @@ pub struct TelnyxSmsAuthCheck {
code: String,
}

#[derive(Debug, Deserialize)]
pub struct TelnyxSmsAuthResponseBody {
pub data: TelnyxSmsAuthResponse,
}

/// Response code is either accepted or rejected
#[derive(Debug, Deserialize)]
pub struct TelnyxSmsAuthResponse {
pub phone_number: String,
pub response_code: String,
}

/// Posts to the validation endpoint with the code, will return success if the code
/// is the same as the one sent to the user
pub async fn check_sms_auth_result(
Expand All @@ -261,7 +281,14 @@ pub async fn check_sms_auth_result(
})
.await
{
Ok(a) => Ok(a.status().is_success()),
Ok(mut a) => {
let response = a.json::<TelnyxSmsAuthResponseBody>().await?;
if response.data.response_code == "accepted" {
Ok(true)
} else {
Ok(false)
}
}
Err(e) => {
error!("Failed to verify code with {:?}", e);
Err(e.into())
Expand Down

0 comments on commit 6a5ff1b

Please sign in to comment.