Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Auth restructure #207

Closed
wants to merge 57 commits into from
Closed
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
05f906b
moving http to a new crate
marioortizmanero Apr 28, 2021
68d625f
rspotify-http now working more or less
marioortizmanero Apr 29, 2021
bbbb3af
moving endpoints to respective traits
marioortizmanero Apr 29, 2021
1a4487b
hit a wall with `impl`
marioortizmanero Apr 29, 2021
61edf5d
it compiles... but at what cost
marioortizmanero Apr 29, 2021
82f1825
format
marioortizmanero Apr 29, 2021
b47f273
numerous fixes
marioortizmanero Apr 29, 2021
1107624
some fixes
marioortizmanero Apr 30, 2021
3a0bb68
Merge with master
marioortizmanero Apr 30, 2021
02c4c0c
format
marioortizmanero Apr 30, 2021
8b40904
now compiling!
marioortizmanero Apr 30, 2021
7ece549
fixed album example
marioortizmanero Apr 30, 2021
ab39089
some fixes to the examples
marioortizmanero Apr 30, 2021
2c63e4c
remove old oauth file
marioortizmanero Apr 30, 2021
6d152a6
fix second example
marioortizmanero Apr 30, 2021
01e581f
tidy up examples, fix current_playing
marioortizmanero Apr 30, 2021
a825ae7
remove println
marioortizmanero Apr 30, 2021
4ac19cc
fixes most examples
marioortizmanero Apr 30, 2021
3ce657e
fix pagination
marioortizmanero Apr 30, 2021
9112d75
all examples work now
marioortizmanero Apr 30, 2021
3b41775
fix tests
marioortizmanero May 1, 2021
17781e5
fully fix pagination
marioortizmanero May 1, 2021
bf3235a
some more fixes
marioortizmanero May 1, 2021
12a97ef
fix clippy
marioortizmanero May 1, 2021
c87b340
i think this should fully pass CI
marioortizmanero May 1, 2021
a4ce9ef
ClientError::InvalidAuth can be removed now
marioortizmanero May 1, 2021
f7721b1
fix compilation conflicts
marioortizmanero May 3, 2021
1651aff
add rspotify-http tests to CI
marioortizmanero May 3, 2021
738f9de
format and add `with_token` constructor
marioortizmanero May 3, 2021
98c0fb3
fix most documentation
marioortizmanero May 3, 2021
2cadd88
mention pkce example
marioortizmanero May 3, 2021
1eb5edf
fix tests
marioortizmanero May 3, 2021
d318b7f
handling more cached cases
marioortizmanero May 8, 2021
decd8c6
add comments to the code auth implementation
marioortizmanero May 8, 2021
6603b1c
comments for the PKCE client as well
marioortizmanero May 8, 2021
b9835e8
comments for client credentials
marioortizmanero May 8, 2021
03fb05b
move oauth utilities to the trait
marioortizmanero May 8, 2021
060cd72
fix comment
marioortizmanero May 8, 2021
3c94d37
comments for the traits
marioortizmanero May 8, 2021
01e172c
remove TODO
marioortizmanero May 8, 2021
f16e148
fix webapp example
marioortizmanero May 8, 2021
1e96b5b
move confusing read_token_cache method
marioortizmanero May 8, 2021
599cea8
fix cli compilation
marioortizmanero May 8, 2021
3409e60
bump url to fix warning
marioortizmanero May 8, 2021
fdafad8
fix tests
marioortizmanero May 8, 2021
4699343
fix CI
marioortizmanero May 8, 2021
2c93222
remove cyclic dependency
marioortizmanero May 9, 2021
e6ed136
fix last CI issue
marioortizmanero May 9, 2021
b45b0f3
add myself as an author!
marioortizmanero May 13, 2021
0c3faed
rename: code_auth -> auth_code
marioortizmanero May 13, 2021
7523da4
rename: CodeAuthSpotify -> AuthCodeSpotify
marioortizmanero May 13, 2021
f44d9e8
rename: CodeAuthPkceSpotify -> AuthCodePkceSpotify
marioortizmanero May 13, 2021
1367931
rename: ClientCredentialsSpotify -> ClientCredsSpotify
marioortizmanero May 13, 2021
8c016bc
rename: endpoints -> clients
marioortizmanero May 13, 2021
5c1a7be
fix maybe-async/is_sync in cargo.toml
marioortizmanero May 14, 2021
56d5c52
fix renames
marioortizmanero May 14, 2021
bc0e0e4
format
marioortizmanero May 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
- arm-unknown-linux-gnueabihf
- armv7-unknown-linux-gnueabihf
client:
- client-ureq,ureq-rustls-tls
- client-reqwest,reqwest-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-ureq,rspotify/ureq-rustls-tls,rspotify-http/client-ureq,rspotify-http/ureq-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-reqwest,rspotify/reqwest-rustls-tls,rspotify-http/client-reqwest,rspotify-http/reqwest-rustls-tls
steps:
- name: Checkout sources
uses: actions/checkout@v2
Expand All @@ -64,16 +64,16 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --workspace --target ${{ matrix.target }} --no-default-features --features=cli,env-file,${{ matrix.client }}
args: -p rspotify -p rspotify-http -p rspotify-model -p rspotify-macros --no-default-features --features=${{ matrix.client }} --target ${{ matrix.target }}

test-client:
name: Test and Lint for each Client
runs-on: ubuntu-latest
strategy:
matrix:
client:
- client-ureq,ureq-rustls-tls
- client-reqwest,reqwest-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-ureq,rspotify/ureq-rustls-tls,rspotify-http/client-ureq,rspotify-http/ureq-rustls-tls
- rspotify/cli,rspotify/env-file,rspotify/client-reqwest,rspotify/reqwest-rustls-tls,rspotify-http/client-reqwest,rspotify-http/reqwest-rustls-tls
steps:
- name: Checkout sources
uses: actions/checkout@v2
Expand All @@ -90,32 +90,10 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --workspace --no-default-features --features=cli,env-file,${{ matrix.client }} -- -D warnings
args: -p rspotify -p rspotify-http -p rspotify-model -p rspotify-macros --no-default-features --features=${{ matrix.client }} -- -D warnings

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features=env-file,${{ matrix.client }}

# The rest of the crates don't need to be tested with multiple feature
# combinations.
test-crates:
name: Simple Tests for Crates
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: -p rspotify-macros -p rspotify-model
args: -p rspotify -p rspotify-http -p rspotify-model -p rspotify-macros --no-default-features --features=${{ matrix.client }}
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

60 changes: 24 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ edition = "2018"
[workspace]
members = [
"rspotify-macros",
"rspotify-model"
"rspotify-model",
"rspotify-http"
]
exclude = [
"examples/webapp"
Expand All @@ -25,19 +26,23 @@ resolver = "2"
[dependencies]
rspotify-macros = { path = "rspotify-macros", version = "0.10.0" }
rspotify-model = { path = "rspotify-model", version = "0.10.0" }
rspotify-http = { path = "rspotify-http", version = "0.10.0", default-features = false }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't rspotify-http enable by default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we need to manually specify which client to use. Perhaps rspotify-http shouldn't enable clients by default, and that should be done by rspotify itself?


### Client ###
async-stream = { version = "0.3.0", optional = true }
async-trait = { version = "0.1.48", optional = true }
base64 = "0.13.0"
chrono = { version = "0.4.13", features = ["serde", "rustc-serialize"] }
derive_builder = "0.10.0"
dotenv = { version = "0.15.0", optional = true }
futures = { version = "0.3.8", optional = true }
futures-util = "0.3.8" # TODO
getrandom = "0.2.0"
log = "0.4.11"
maybe-async = "0.2.1"
serde = { version = "1.0.115", features = ["derive"] }
serde_json = "1.0.57"
thiserror = "1.0.20"
url = "2.2.2"
webbrowser = { version = "0.5.5", optional = true }

### Auth ###
Expand All @@ -46,18 +51,6 @@ webbrowser = { version = "0.5.5", optional = true }
# maybe-async = "0.2.1"
# thiserror = "1.0.20"

### HTTP ###
# Temporary until https://github.com/rust-lang/rfcs/issues/2739, for
# `maybe_async`.
async-trait = { version = "0.1.48", optional = true }
base64 = "0.13.0"
# log = "0.4.11"
# maybe-async = "0.2.1"
reqwest = { version = "0.11.0", default-features = false, features = ["json", "socks"], optional = true }
# thiserror = "1.0.20"
ureq = { version = "2.0", default-features = false, features = ["json", "cookies"], optional = true }
url = "2.1.1"

[dev-dependencies]
env_logger = "0.8.1"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] }
Expand All @@ -73,49 +66,44 @@ env-file = ["dotenv"]
### HTTP ###
# Available clients. By default they don't include a TLS so that it can be
# configured.
client-ureq = ["ureq", "__sync"]
client-reqwest = ["reqwest", "__async"]
client-ureq = ["rspotify-http/client-ureq", "__sync"]
client-reqwest = ["rspotify-http/client-reqwest", "__async"]

# Passing the TLS features to reqwest.
reqwest-default-tls = ["reqwest/default-tls"]
reqwest-rustls-tls = ["reqwest/rustls-tls"]
reqwest-native-tls = ["reqwest/native-tls"]
reqwest-native-tls-vendored = ["reqwest/native-tls-vendored"]
reqwest-default-tls = ["rspotify-http/reqwest-default-tls"]
reqwest-rustls-tls = ["rspotify-http/reqwest-rustls-tls"]
reqwest-native-tls = ["rspotify-http/reqwest-native-tls"]
reqwest-native-tls-vendored = ["rspotify-http/reqwest-native-tls-vendored"]
# Same for ureq.
ureq-rustls-tls = ["ureq/tls"]
ureq-rustls-tls = ["rspotify-http/ureq-rustls-tls"]

# Internal features for checking async or sync compilation
__async = ["async-trait", "async-stream", "futures"]
__sync = ["maybe-async/is_sync"]
__async = ["futures", "async-stream", "async-trait"]
__sync = []
marioortizmanero marked this conversation as resolved.
Show resolved Hide resolved

[package.metadata.docs.rs]
# Also documenting the CLI methods
features = ["cli"]

[[example]]
name = "album"
required-features = ["env-file", "cli", "client-reqwest"]
path = "examples/album.rs"

[[example]]
name = "current_user_recently_played"
name = "client_credentials"
required-features = ["env-file", "cli", "client-reqwest"]
path = "examples/current_user_recently_played.rs"
path = "examples/client_credentials.rs"

[[example]]
name = "oauth_tokens"
name = "code_auth"
required-features = ["env-file", "cli", "client-reqwest"]
path = "examples/oauth_tokens.rs"
path = "examples/code_auth.rs"

[[example]]
name = "track"
name = "code_auth_pkce"
required-features = ["env-file", "cli", "client-reqwest"]
path = "examples/track.rs"
path = "examples/code_auth_pkce.rs"

[[example]]
name = "tracks"
name = "oauth_tokens"
required-features = ["env-file", "cli", "client-reqwest"]
path = "examples/tracks.rs"
path = "examples/oauth_tokens.rs"

[[example]]
name = "with_refresh_token"
Expand Down
24 changes: 10 additions & 14 deletions examples/album.rs → examples/client_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use rspotify::client::SpotifyBuilder;
use rspotify::model::Id;
use rspotify::oauth2::CredentialsBuilder;
use rspotify::{model::Id, prelude::*, ClientCredentialsSpotify, Credentials};

#[tokio::main]
async fn main() {
Expand All @@ -17,22 +15,20 @@ async fn main() {
//
// Otherwise, set client_id and client_secret explictly:
//
// let creds = CredentialsBuilder::default()
// .client_id("this-is-my-client-id")
// .client_secret("this-is-my-client-secret")
// .build()
// .unwrap();
let creds = CredentialsBuilder::from_env().build().unwrap();
// ```
// let creds = Credentials {
// id: "this-is-my-client-id".to_string(),
// secret: "this-is-my-client-secret".to_string()
// };
// ```
let creds = Credentials::from_env().unwrap();

let mut spotify = SpotifyBuilder::default()
.credentials(creds)
.build()
.unwrap();
let mut spotify = ClientCredentialsSpotify::new(creds);

// Obtaining the access token. Requires to be mutable because the internal
// token will be modified. We don't need OAuth for this specific endpoint,
// so `...` is used instead of `prompt_for_user_token`.
spotify.request_client_token().await.unwrap();
spotify.request_token().await.unwrap();

// Running the requests
let birdy_uri = Id::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
Expand Down
55 changes: 55 additions & 0 deletions examples/code_auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use rspotify::{
model::{AdditionalType, Country, Market},
prelude::*,
scopes, CodeAuthSpotify, Credentials, OAuth,
};

#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();

// Set RSPOTIFY_CLIENT_ID, RSPOTIFY_CLIENT_SECRET and
// RSPOTIFY_REDIRECT_URI in an .env file or export them manually:
//
// export RSPOTIFY_CLIENT_ID="your client_id"
// export RSPOTIFY_CLIENT_SECRET="secret"
//
// These will then be read with `from_env`.
//
// Otherwise, set client_id and client_secret explictly:
//
// ```
// let creds = Credentials {
// id: "this-is-my-client-id".to_string(),
// secret: "this-is-my-client-secret".to_string()
// };
// ```
let creds = Credentials::from_env().unwrap();

// Or set the redirect_uri explictly:
//
// ```
// let oauth = OAuth {
// redirect_uri: "http://localhost:8888/callback".to_string(),
// scope: scopes!("user-read-recently-played"),
// ..Default::default(),
// };
// ```
let oauth = OAuth::from_env(scopes!("user-read-currently-playing")).unwrap();

let mut spotify = CodeAuthSpotify::new(creds, oauth);

// Obtaining the access token
let url = spotify.get_authorize_url(false).unwrap();
spotify.prompt_for_token(&url).await.unwrap();

// Running the requests
let market = Market::Country(Country::Spain);
let additional_types = [AdditionalType::Episode];
let artists = spotify
.current_playing(Some(&market), Some(&additional_types))
.await;

println!("Response: {:?}", artists);
}
47 changes: 47 additions & 0 deletions examples/code_auth_pkce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use rspotify::{prelude::*, scopes, CodeAuthPkceSpotify, Credentials, OAuth};

#[tokio::main]
async fn main() {
// You can use any logger for debugging.
env_logger::init();

// Set RSPOTIFY_CLIENT_ID, RSPOTIFY_CLIENT_SECRET and
// RSPOTIFY_REDIRECT_URI in an .env file or export them manually:
//
// export RSPOTIFY_CLIENT_ID="your client_id"
// export RSPOTIFY_CLIENT_SECRET="secret"
//
// These will then be read with `from_env`.
//
// Otherwise, set client_id and client_secret explictly:
//
// ```
// let creds = Credentials {
// id: "this-is-my-client-id".to_string(),
// secret: "this-is-my-client-secret".to_string()
// };
// ```
let creds = Credentials::from_env().unwrap();

// Or set the redirect_uri explictly:
//
// ```
// let oauth = OAuth {
// redirect_uri: "http://localhost:8888/callback".to_string(),
// scope: scopes!("user-read-recently-played"),
// ..Default::default(),
// };
// ```
let oauth = OAuth::from_env(scopes!("user-read-recently-played")).unwrap();

let mut spotify = CodeAuthPkceSpotify::new(creds, oauth);

// Obtaining the access token
let url = spotify.get_authorize_url().unwrap();
spotify.prompt_for_token(&url).await.unwrap();

// Running the requests
let history = spotify.current_playback(None, None::<Vec<_>>).await;

println!("Response: {:?}", history);
}
Loading