Skip to content

Commit

Permalink
Unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksongoode committed Aug 25, 2024
1 parent 538b5dc commit d288f39
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 79 deletions.
78 changes: 0 additions & 78 deletions psst-core/src/oauth.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
use log::{debug, error, trace};
use oauth2::{
basic::BasicClient, reqwest::http_client, AuthUrl, AuthorizationCode, ClientId, CsrfToken,
PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, Scope, TokenResponse, TokenUrl,
};
use std::io;
use std::{
io::{BufRead, BufReader, Write},
net::TcpStream,
net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener},
process::exit,
sync::mpsc,
time::Duration,
};
use url::Url;

fn get_code(redirect_url: &str) -> AuthorizationCode {
Url::parse(redirect_url)
.unwrap()
.query_pairs()
.find(|(key, _)| key == "code")
.map(|(_, code)| AuthorizationCode::new(code.into_owned()))
.expect("No code found in redirect URL")
}

fn get_authcode_stdin() -> AuthorizationCode {
println!("Provide redirect URL");
let mut buffer = String::new();
let stdin = io::stdin();
stdin.read_line(&mut buffer).unwrap();

get_code(buffer.trim())
}

pub fn get_authcode_listener(
socket_address: SocketAddr,
timeout: Duration,
Expand Down Expand Up @@ -90,63 +69,6 @@ fn send_success_response(stream: &mut TcpStream) {
let _ = stream.write_all(response.as_bytes());
}

pub fn get_access_token(redirect_port: u16) -> String {
let redirect_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), redirect_port);
let redirect_uri = format!("http://{redirect_address}/login");

let client = BasicClient::new(
ClientId::new(crate::session::access_token::CLIENT_ID.to_string()),
None,
AuthUrl::new("https://accounts.spotify.com/authorize".to_string()).unwrap(),
Some(TokenUrl::new("https://accounts.spotify.com/api/token".to_string()).unwrap()),
)
.set_redirect_uri(RedirectUrl::new(redirect_uri).expect("Invalid redirect URL"));

let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();

let scopes: Vec<oauth2::Scope> = crate::session::access_token::ACCESS_SCOPES
.split(',')
.map(|s| Scope::new(s.trim().to_string()))
.collect();
let (auth_url, _) = client
.authorize_url(CsrfToken::new_random)
.add_scopes(scopes)
.set_pkce_challenge(pkce_challenge)
.url();

println!("Browse to: {}", auth_url);

let code = if redirect_port > 0 {
get_authcode_listener(redirect_address, Duration::from_secs(300)).unwrap()
} else {
get_authcode_stdin()
};
debug!("Exchange {code:?} for access token");

let (tx, rx) = mpsc::channel();
let client_clone = client.clone();
std::thread::spawn(move || {
let resp = client_clone
.exchange_code(code)
.set_pkce_verifier(pkce_verifier)
.request(http_client);
tx.send(resp).unwrap();
});
let token_response = rx.recv().unwrap();
let token = match token_response {
Ok(tok) => {
trace!("Obtained new access token: {tok:?}");
tok
}
Err(e) => {
error!("Failed to exchange code for access token: {e:?}");
exit(1);
}
};

token.access_token().secret().to_string()
}

fn create_spotify_oauth_client(redirect_port: u16) -> BasicClient {
let redirect_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), redirect_port);
let redirect_uri = format!("http://{redirect_address}/login");
Expand Down
2 changes: 1 addition & 1 deletion psst-gui/src/data/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,4 @@ impl Default for SortCriteria {
fn default() -> Self {
Self::DateAdded
}
}
}

0 comments on commit d288f39

Please sign in to comment.