Skip to content

Commit

Permalink
Create credentials from access token
Browse files Browse the repository at this point in the history
  • Loading branch information
kingosticks committed Jul 31, 2024
1 parent 299b7de commit 2053d6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
16 changes: 12 additions & 4 deletions core/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,27 @@ impl Credentials {
///
/// let creds = Credentials::with_password("my account", "my password");
/// ```
pub fn with_password(username: impl Into<String>, password: impl Into<String>) -> Credentials {
Credentials {
pub fn with_password(username: impl Into<String>, password: impl Into<String>) -> 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<String>, token: impl Into<String>) -> Self {
Self {
username: username.into(),
auth_type: AuthenticationType::AUTHENTICATION_SPOTIFY_TOKEN,
auth_data: token.into().into_bytes(),
}
}

pub fn with_blob(
username: impl Into<String>,
encrypted_blob: impl AsRef<[u8]>,
device_id: impl AsRef<[u8]>,
) -> Result<Credentials, Error> {
) -> Result<Self, Error> {
fn read_u8<R: Read>(stream: &mut R) -> io::Result<u8> {
let mut data = [0u8];
stream.read_exact(&mut data)?;
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 26 additions & 10 deletions examples/get_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

0 comments on commit 2053d6e

Please sign in to comment.