Skip to content

Commit

Permalink
Use accept terms and conditions v2
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Aug 19, 2024
1 parent 660f3f4 commit 09ebf30
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 39 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ jobs:
uses: actions/checkout@v3
- name: Linelint
uses: fernandrone/linelint@master
id: linelint
id: linelint
unused_dependencies:
name: Look for unused dependencies
runs-on: ubuntu-latest
steps:
- name: rust-toolchain
uses: actions-rs/[email protected]
with:
toolchain: nightly-2023-12-28
toolchain: nightly-2024-08-14
override: true
- name: Install unused dependency checker
run: cargo install cargo-udeps --locked
- name: Checkout
uses: actions/checkout@v3
- name: Run unused dependency checker
run: cargo +nightly-2023-12-28 udeps
run: cargo +nightly-2024-08-14 udeps
8 changes: 4 additions & 4 deletions graphql/schemas/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ mutation UnlockWallet($challenge: String!, $challengeSignature: String!, $prepar
}
}

mutation AcceptTermsAndConditions($serviceProvider: String!, $version: Int!) {
accept_terms_conditions(args: {service_provider: $serviceProvider, version: $version}) {
serviceProvider
acceptedTerms
mutation AcceptTermsAndConditionsV2($fingerprint: String!, $service: Service, $version: Int!) {
accept_terms_conditions_v2(args: {fingerprint: $fingerprint, service: $service, version: $version}) {
acceptDate
accepted
service
version
}
}
Expand Down
4 changes: 3 additions & 1 deletion graphql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use reqwest;
use chrono::{DateTime, Utc};
use graphql_client::reqwest::{post_graphql, post_graphql_blocking};
use graphql_client::Response;
use perro::{permanent_failure, runtime_error, MapToError, OptionToError};
use perro::{invalid_input, permanent_failure, runtime_error, MapToError, OptionToError};
use reqwest::blocking::Client;
use reqwest::header::{HeaderValue, AUTHORIZATION};
use reqwest::StatusCode;
Expand Down Expand Up @@ -160,6 +160,7 @@ fn map_error_code(code: &str) -> Error {
const MISSING_HTTP_HEADER_EXCEPTION_CODE: &str = "http-header-missing-exception";
const INVALID_INVITATION_EXCEPTION_CODE: &str = "invalid-invitation-exception";
const REMOTE_SCHEMA_ERROR_CODE: &str = "remote-schema-error";
const INVALID_FINGERPRINT: &str = "invalid-fingerprint";

match code {
AUTH_EXCEPTION_CODE => runtime_error(
Expand All @@ -179,6 +180,7 @@ fn map_error_code(code: &str) -> Error {
REMOTE_SCHEMA_ERROR_CODE => {
permanent_failure("A remote schema call has failed on the backend")
}
INVALID_FINGERPRINT => invalid_input("The provided fingerprint is invalid"),
_ => permanent_failure(format!(
"Unexpected backend response: unknown error code: {code}"
)),
Expand Down
2 changes: 1 addition & 1 deletion graphql/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct RefreshSession;
query_path = "schemas/operations.graphql",
response_derives = "Debug"
)]
pub struct AcceptTermsAndConditions;
pub struct AcceptTermsAndConditionsV2;

#[derive(GraphQLQuery)]
#[graphql(
Expand Down
3 changes: 2 additions & 1 deletion honeybadger/src/asynchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ impl Auth {
&self,
terms: TermsAndConditions,
version: i64,
fingerprint: String,
) -> Result<()> {
let token = self.query_token().await?;
let provider = self.provider.lock().await;
provider
.accept_terms_and_conditions(token, terms, version)
.accept_terms_and_conditions(token, terms, version, fingerprint)
.await
}

Expand Down
13 changes: 8 additions & 5 deletions honeybadger/src/asynchronous/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,27 @@ impl AuthProvider {
access_token: String,
terms: TermsAndConditions,
version: i64,
fingerprint: String,
) -> Result<()> {
info!("Accepting T&C ({:?})...", terms);
ensure!(
self.auth_level == AuthLevel::Pseudonymous,
invalid_input("Accepting T&C not supported for auth levels other than Pseudonymous")
);

let variables = accept_terms_and_conditions::Variables {
service_provider: terms.into(),
let variables = accept_terms_and_conditions_v2::Variables {
fingerprint,
service: Some(terms.into()),
version,
};
let client = build_async_client(Some(&access_token))?;
let data = post::<AcceptTermsAndConditions>(&client, &self.backend_url, variables).await?;
let data =
post::<AcceptTermsAndConditionsV2>(&client, &self.backend_url, variables).await?;
ensure!(
matches!(
data.accept_terms_conditions,
data.accept_terms_conditions_v2,
Some(
accept_terms_and_conditions::AcceptTermsAndConditionsAcceptTermsConditions { .. }
accept_terms_and_conditions_v2::AcceptTermsAndConditionsV2AcceptTermsConditionsV2 { .. }
)
),
permanent_failure("Backend rejected accepting Terms and Conditions")
Expand Down
3 changes: 2 additions & 1 deletion honeybadger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ impl Auth {
&self,
terms: TermsAndConditions,
version: i64,
fingerprint: String,
) -> Result<()> {
let token = self.query_token()?;
let provider = self.provider.lock().unwrap();
provider.accept_terms_and_conditions(token, terms, version)
provider.accept_terms_and_conditions(token, terms, version, fingerprint)
}

pub fn get_terms_and_conditions_status(
Expand Down
20 changes: 11 additions & 9 deletions honeybadger/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::signing::sign;
use crate::TermsAndConditionsStatus;
use graphql::perro::{ensure, invalid_input, permanent_failure, runtime_error, OptionToError};
use graphql::reqwest::blocking::Client;
use graphql::schema::accept_terms_and_conditions_v2::Service;
use graphql::schema::get_terms_and_conditions_status::ServiceProviderEnum;
use graphql::schema::*;
use graphql::{build_client, perro, post_blocking};
Expand All @@ -24,13 +25,12 @@ pub enum TermsAndConditions {
Pocket,
}

impl From<TermsAndConditions> for String {
impl From<TermsAndConditions> for Service {
fn from(value: TermsAndConditions) -> Self {
match value {
TermsAndConditions::Lipa => "LIPA_WALLET",
TermsAndConditions::Pocket => "POCKET_EXCHANGE",
TermsAndConditions::Lipa => Service::LIPA_WALLET,
TermsAndConditions::Pocket => Service::POCKET_EXCHANGE,
}
.to_string()
}
}

Expand Down Expand Up @@ -114,25 +114,27 @@ impl AuthProvider {
access_token: String,
terms: TermsAndConditions,
version: i64,
fingerprint: String,
) -> Result<()> {
info!("Accepting T&C ({:?})...", terms);
ensure!(
self.auth_level == AuthLevel::Pseudonymous,
invalid_input("Accepting T&C not supported for auth levels other than Pseudonymous")
);

let variables = accept_terms_and_conditions::Variables {
service_provider: terms.into(),
let variables = accept_terms_and_conditions_v2::Variables {
fingerprint,
service: Some(terms.into()),
version,
};
let client = build_client(Some(&access_token))?;
let data =
post_blocking::<AcceptTermsAndConditions>(&client, &self.backend_url, variables)?;
post_blocking::<AcceptTermsAndConditionsV2>(&client, &self.backend_url, variables)?;
ensure!(
matches!(
data.accept_terms_conditions,
data.accept_terms_conditions_v2,
Some(
accept_terms_and_conditions::AcceptTermsAndConditionsAcceptTermsConditions { .. }
accept_terms_and_conditions_v2::AcceptTermsAndConditionsV2AcceptTermsConditionsV2 { .. }
)
),
permanent_failure("Backend rejected accepting Terms and Conditions")
Expand Down
35 changes: 21 additions & 14 deletions honeybadger/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,17 @@ fn test_accept_terms_and_conditions() {
auth_keypair,
)
.unwrap();
auth.accept_terms_and_conditions(TermsAndConditions::Lipa, 1)
.unwrap();
auth.accept_terms_and_conditions(
TermsAndConditions::Lipa,
3,
"dcceac0c67d2946e3ea44ccbb439a5b33ee9d36d79df2b0f9070b66938".into(),
)
.unwrap();
assert_eq!(
auth.get_terms_and_conditions_status(TermsAndConditions::Lipa)
.unwrap()
.version,
1
3
);

let (wallet_keypair, auth_keypair) = generate_keys();
Expand All @@ -209,8 +213,11 @@ fn test_accept_terms_and_conditions() {
auth_keypair,
)
.unwrap();
let result = auth.accept_terms_and_conditions(TermsAndConditions::Lipa, 2);
assert!(matches!(result, Err(Error::InvalidInput { .. })));
let result =
auth.accept_terms_and_conditions(TermsAndConditions::Lipa, 3, "fingerprint2".into());
assert!(
matches!(result, Err(Error::InvalidInput { msg }) if msg.contains("Accepting T&C not supported for auth levels other than Pseudonymous"))
);

let (wallet_keypair, auth_keypair) = generate_keys();
let auth = Auth::new(
Expand All @@ -220,13 +227,10 @@ fn test_accept_terms_and_conditions() {
auth_keypair,
)
.unwrap();
auth.accept_terms_and_conditions(TermsAndConditions::Pocket, 3)
.unwrap();
assert_eq!(
auth.get_terms_and_conditions_status(TermsAndConditions::Pocket)
.unwrap()
.version,
3
let result =
auth.accept_terms_and_conditions(TermsAndConditions::Pocket, 3, "fingerprint3".into());
assert!(
matches!(result, Err(Error::InvalidInput { msg }) if msg.contains("The provided fingerprint is invalid"))
);

let (wallet_keypair, auth_keypair) = generate_keys();
Expand All @@ -237,8 +241,11 @@ fn test_accept_terms_and_conditions() {
auth_keypair,
)
.unwrap();
let result = auth.accept_terms_and_conditions(TermsAndConditions::Pocket, 4);
assert!(matches!(result, Err(Error::InvalidInput { .. })));
let result =
auth.accept_terms_and_conditions(TermsAndConditions::Pocket, 4, "fingerprint4".into());
assert!(
matches!(result, Err(Error::InvalidInput { msg }) if msg.contains("Accepting T&C not supported for auth levels other than Pseudonymous"))
);
}

fn generate_keys() -> (KeyPair, KeyPair) {
Expand Down

0 comments on commit 09ebf30

Please sign in to comment.