diff --git a/core/src/authentication.rs b/core/src/authentication.rs index 8122d6591..b2448ab2a 100644 --- a/core/src/authentication.rs +++ b/core/src/authentication.rs @@ -50,19 +50,27 @@ impl Credentials { /// /// let creds = Credentials::with_password("my account", "my password"); /// ``` - pub fn with_password(username: impl Into, password: impl Into) -> Credentials { - Credentials { + pub fn with_password(username: impl Into, password: impl Into) -> Self { + Self { username: username.into(), auth_type: AuthenticationType::AUTHENTICATION_USER_PASS, auth_data: password.into().into_bytes(), } } + pub fn with_access_token(username: impl Into, token: impl Into) -> Self { + Self { + username: username.into(), + auth_type: AuthenticationType::AUTHENTICATION_SPOTIFY_TOKEN, + auth_data: token.into().into_bytes(), + } + } + pub fn with_blob( username: impl Into, encrypted_blob: impl AsRef<[u8]>, device_id: impl AsRef<[u8]>, - ) -> Result { + ) -> Result { fn read_u8(stream: &mut R) -> io::Result { let mut data = [0u8]; stream.read_exact(&mut data)?; @@ -136,7 +144,7 @@ impl Credentials { read_u8(&mut cursor)?; let auth_data = read_bytes(&mut cursor)?; - Ok(Credentials { + Ok(Self { username, auth_type, auth_data, diff --git a/examples/get_token.rs b/examples/get_token.rs index 0473e1226..8a9cd5730 100644 --- a/examples/get_token.rs +++ b/examples/get_token.rs @@ -7,23 +7,39 @@ const SCOPES: &str = #[tokio::main] async fn main() { - let session_config = SessionConfig::default(); + let mut session_config = SessionConfig::default(); let args: Vec<_> = env::args().collect(); - if args.len() != 3 { - eprintln!("Usage: {} USERNAME PASSWORD", args[0]); + if args.len() == 4 { + session_config.client_id = args[3].clone() + } else if args.len() != 3 { + eprintln!("Usage: {} USERNAME PASSWORD [CLIENT_ID]", args[0]); return; } + let username = &args[1]; + let password = &args[2]; - println!("Connecting..."); - let credentials = Credentials::with_password(&args[1], &args[2]); - let session = Session::new(session_config, None); + let session = Session::new(session_config.clone(), None); + let credentials = Credentials::with_password(username, password); + println!("Connecting with password.."); + let token = match session.connect(credentials, false).await { + Ok(()) => { + println!("Session username: {:#?}", session.username()); + session.token_provider().get_token(SCOPES).await.unwrap() + } + Err(e) => { + println!("Error connecting: {}", e); + return; + } + }; + println!("Token: {:#?}", token); + // Now create a new session with that token. + let session = Session::new(session_config, None); + let credentials = Credentials::with_access_token(username, token.access_token); + println!("Connecting with token.."); match session.connect(credentials, false).await { - Ok(()) => println!( - "Token: {:#?}", - session.token_provider().get_token(SCOPES).await.unwrap() - ), + Ok(()) => println!("Session username: {:#?}", session.username()), Err(e) => println!("Error connecting: {}", e), } }