From e47cddf46eca543a8e0263a1a3d887bba821b395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Em=C4=ABls?= Date: Tue, 30 Apr 2024 18:32:04 +0200 Subject: [PATCH] Use configured hostname for access token --- ios/MullvadVPNUITests/AccountTests.swift | 7 +++++++ mullvad-api/src/abortable_stream.rs | 4 +--- mullvad-api/src/access.rs | 10 ++++++---- mullvad-api/src/ffi/error.rs | 2 +- mullvad-api/src/lib.rs | 6 +++--- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/ios/MullvadVPNUITests/AccountTests.swift b/ios/MullvadVPNUITests/AccountTests.swift index 6ca45169f3bf..d417c66d19ec 100644 --- a/ios/MullvadVPNUITests/AccountTests.swift +++ b/ios/MullvadVPNUITests/AccountTests.swift @@ -8,6 +8,13 @@ import XCTest +class AccountApi: XCTestCase { + func testApi() throws { + let temporaryAccountNumber = try MullvadAPIWrapper().createAccount() + try MullvadAPIWrapper().addDevices(5, account: temporaryAccountNumber) + } +} + class AccountTests: LoggedOutUITestCase { override func setUpWithError() throws { continueAfterFailure = false diff --git a/mullvad-api/src/abortable_stream.rs b/mullvad-api/src/abortable_stream.rs index c848bd9f923f..0258a7e650a0 100644 --- a/mullvad-api/src/abortable_stream.rs +++ b/mullvad-api/src/abortable_stream.rs @@ -1,9 +1,7 @@ //! Wrapper around a stream to make it abortable. This allows in-flight requests to be cancelled //! immediately instead of after the socket times out. -use futures::channel::oneshot; -use futures::future::Fuse; -use futures::FutureExt; +use futures::{channel::oneshot, future::Fuse, FutureExt}; use hyper::client::connect::{Connected, Connection}; use std::{ future::Future, diff --git a/mullvad-api/src/access.rs b/mullvad-api/src/access.rs index 569c01f085a7..dba9e4befd7c 100644 --- a/mullvad-api/src/access.rs +++ b/mullvad-api/src/access.rs @@ -1,7 +1,6 @@ use crate::{ rest, rest::{RequestFactory, RequestServiceHandle}, - API, }; use futures::{ channel::{mpsc, oneshot}, @@ -9,7 +8,7 @@ use futures::{ }; use hyper::StatusCode; use mullvad_types::account::{AccessToken, AccessTokenData, AccountToken}; -use std::collections::HashMap; +use std::{borrow::Cow, collections::HashMap}; use tokio::select; pub const AUTH_URL_PREFIX: &str = "auth/v1"; @@ -37,8 +36,11 @@ struct AccountState { } impl AccessTokenStore { - pub(crate) fn new(service: RequestServiceHandle) -> Self { - let factory = rest::RequestFactory::new(API.host(), None); + pub(crate) fn new( + service: RequestServiceHandle, + hostname: impl Into>, + ) -> Self { + let factory = rest::RequestFactory::new(hostname, None); let (tx, rx) = mpsc::unbounded(); tokio::spawn(Self::service_requests(rx, service, factory)); Self { tx } diff --git a/mullvad-api/src/ffi/error.rs b/mullvad-api/src/ffi/error.rs index 8f3095fee0a5..539a6c23a052 100644 --- a/mullvad-api/src/ffi/error.rs +++ b/mullvad-api/src/ffi/error.rs @@ -21,7 +21,7 @@ pub struct MullvadApiError { impl MullvadApiError { pub fn new(kind: MullvadApiErrorKind, error: &dyn std::error::Error) -> Self { - let description = CString::new(format!("{error:?}")).unwrap_or_default(); + let description = CString::new(format!("{error:?}: {error}")).unwrap_or_default(); Self { description: description.into_raw(), kind, diff --git a/mullvad-api/src/lib.rs b/mullvad-api/src/lib.rs index 82cabddff0cc..85214a9fc0f1 100644 --- a/mullvad-api/src/lib.rs +++ b/mullvad-api/src/lib.rs @@ -434,8 +434,8 @@ impl Runtime { #[cfg(target_os = "android")] self.socket_bypass_tx.clone(), ); - let token_store = access::AccessTokenStore::new(service.clone()); - let factory = rest::RequestFactory::new(API.host(), Some(token_store)); + let token_store = access::AccessTokenStore::new(service.clone(), API.host()); + let factory = rest::RequestFactory::new(API.host().to_owned(), Some(token_store)); rest::MullvadRestHandle::new(service, factory, self.availability_handle()) } @@ -448,7 +448,7 @@ impl Runtime { #[cfg(target_os = "android")] self.socket_bypass_tx.clone(), ); - let token_store = access::AccessTokenStore::new(service.clone()); + let token_store = access::AccessTokenStore::new(service.clone(), hostname.clone()); let factory = rest::RequestFactory::new(hostname, Some(token_store)); rest::MullvadRestHandle::new(service, factory, self.availability_handle())