diff options
author | HampusM <hampus@hampusmat.com> | 2022-09-09 20:03:02 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-09-09 20:04:54 +0200 |
commit | db42316544c65951c7781357ec5aaba1b9abb8ab (patch) | |
tree | 13fd19efb80cf29f054c1760e8580f4379ce6a6a /src/auth/mod.rs | |
parent | 8a02d3386d4ce0b58de943fcf42bd072af1e0b42 (diff) |
refactor: make AuthPromptHandler use the Hyper web server
Diffstat (limited to 'src/auth/mod.rs')
-rw-r--r-- | src/auth/mod.rs | 106 |
1 files changed, 0 insertions, 106 deletions
diff --git a/src/auth/mod.rs b/src/auth/mod.rs deleted file mode 100644 index ce550e1..0000000 --- a/src/auth/mod.rs +++ /dev/null @@ -1,106 +0,0 @@ -//! Deezer API authentication. -use std::error::Error; -use std::fmt::Display; -use std::time::Duration; - -use actix_web::web::Data; -use actix_web::{App, HttpServer}; -use serde::Deserialize; -use tokio::sync::mpsc; -use tokio::task::JoinHandle; -use tokio::{select, spawn}; - -use crate::auth::service::retrieve_auth_code; -use crate::errors::auth::AuthPromptHandlerError; - -mod service; - -const AUTH_URL: &str = "https://connect.deezer.com/oauth/auth.php"; - -/// A Deezer authentication code. -#[derive(Debug, Deserialize, Clone)] -pub struct AuthCode(String); - -impl Display for AuthCode -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result - { - self.0.fmt(f) - } -} - -/// A Deezer authentication prompt handler. -pub struct AuthPromptHandler -{ - /// URL to the Deezer authentication prompt. - pub auth_prompt_url: String, - - /// Handle for the running authentication prompt handler. - /// - /// Finishes when a single authentication has occurred. - pub handler: JoinHandle<Result<AuthCode, Box<dyn Error + Send + Sync>>>, -} - -impl AuthPromptHandler -{ - /// Runs a web server that handles a Deezer authentication prompt. - /// - /// The argument `address` must be in the same domain as the domain you defined when - /// you created the application with the ID `app_id`. - /// - /// # Errors - /// Will return Err if the web server created is unable to bind to `address`. - pub async fn run( - app_id: u32, - address: String, - port: u16, - uri_scheme: String, - ) -> Result<Self, AuthPromptHandlerError> - { - let (done_tx, mut done_rx) = mpsc::channel::<AuthCode>(1); - - let done_tx_data = Data::new(done_tx); - - let server = HttpServer::new(move || { - App::new() - .app_data(done_tx_data.clone()) - .service(retrieve_auth_code) - }) - .bind((address.clone(), port)) - .map_err(|_| AuthPromptHandlerError::BindAddressFailed)?; - - let server_future = server.run(); - - let handle = spawn(async move { - let opt_auth_code = select! { - result = server_future => { - result.map(|_| None) - }, - auth_code = async { - done_rx.recv().await - } => Ok(auth_code) - }?; - - Ok(opt_auth_code.map_or_else(|| Err("No auth code was received"), Ok)?) - }); - - Ok(Self { - auth_prompt_url: format!( - "{}?app_id={}&redirect_uri={}://{}:{}&perms=basic_access", - AUTH_URL, app_id, uri_scheme, address, port - ), - handler: handle, - }) - } -} - -/// A Deezer access token. -#[derive(Debug, Clone)] -pub struct AccessToken -{ - /// The access token. - pub access_token: String, - - /// The duration until the access token expires. - pub expires: Duration, -} |