From 6f46c7a44ea7fbbd7ae099fbf5f5f1181e7cbad4 Mon Sep 17 00:00:00 2001 From: Nick Steel Date: Wed, 14 Aug 2024 12:29:31 +0100 Subject: [PATCH] oauth: break-out code parsing --- oauth/src/lib.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/oauth/src/lib.rs b/oauth/src/lib.rs index a8e294128..911f36217 100644 --- a/oauth/src/lib.rs +++ b/oauth/src/lib.rs @@ -13,13 +13,23 @@ use std::{ }; use url::Url; +fn get_code(redirect_url: &str) -> AuthorizationCode { + let url = Url::parse(redirect_url).unwrap(); + let code = url + .query_pairs() + .find(|(key, _)| key == "code") + .map(|(_, code)| AuthorizationCode::new(code.into_owned())) + .unwrap(); + code +} + fn get_authcode_stdin() -> AuthorizationCode { - println!("Provide code"); + println!("Provide redirect URL"); let mut buffer = String::new(); let stdin = io::stdin(); // We get `Stdin` here. stdin.read_line(&mut buffer).unwrap(); - AuthorizationCode::new(buffer.trim().into()) + get_code(buffer.trim()) } fn get_authcode_listener(socket_address: SocketAddr) -> AuthorizationCode { @@ -39,13 +49,7 @@ fn get_authcode_listener(socket_address: SocketAddr) -> AuthorizationCode { reader.read_line(&mut request_line).unwrap(); let redirect_url = request_line.split_whitespace().nth(1).unwrap(); - let url = Url::parse(&("http://localhost".to_string() + redirect_url)).unwrap(); - - let code = url - .query_pairs() - .find(|(key, _)| key == "code") - .map(|(_, code)| AuthorizationCode::new(code.into_owned())) - .unwrap(); + let code = get_code(&("http://localhost".to_string() + redirect_url)); let message = "Go back to your terminal :)"; let response = format!( @@ -60,6 +64,7 @@ fn get_authcode_listener(socket_address: SocketAddr) -> AuthorizationCode { // TODO: Return a Result? // TODO: Pass in redirect_address instead since the redirect host depends on client ID? +// TODO: Pass in scopes. pub fn get_access_token(client_id: &str, redirect_port: u16) -> String { // Must use host 127.0.0.1 with Spotify Desktop client ID. let redirect_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), redirect_port);