-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into provider/microsoft
- Loading branch information
Showing
9 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @torto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,9 @@ name = "discord" | |
|
||
[[example]] | ||
name = "twitter" | ||
|
||
[[example]] | ||
name = "facebook" | ||
|
||
[[example]] | ||
name = "spotify" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
mod utils; | ||
use std::sync::Arc; | ||
|
||
use axum::extract::Query; | ||
use axum::Router; | ||
use axum::{routing::get, Extension}; | ||
use oauth_axum::providers::facebook::FacebookProvider; | ||
use oauth_axum::{CustomProvider, OAuthClient}; | ||
|
||
use crate::utils::memory_db_util::AxumState; | ||
|
||
#[derive(Clone, serde::Deserialize)] | ||
pub struct QueryAxumCallback { | ||
pub code: String, | ||
pub state: String, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
dotenv::from_filename("examples/.env").ok(); | ||
println!("Starting server..."); | ||
|
||
let state = Arc::new(AxumState::new()); | ||
let app = Router::new() | ||
.route("/", get(create_url)) | ||
.route("/api/v1/facebook/callback", get(callback)) | ||
.layer(Extension(state.clone())); | ||
|
||
println!("🚀 Server started successfully"); | ||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") | ||
.await | ||
.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
} | ||
|
||
fn get_client() -> CustomProvider { | ||
FacebookProvider::new( | ||
std::env::var("FACEBOOK_CLIENT_ID").expect("FACEBOOK_CLIENT_ID must be set"), | ||
std::env::var("FACEBOOK_SECRET").expect("FACEBOOK_SECRET must be set"), | ||
"http://localhost:3000/api/v1/facebook/callback".to_string(), | ||
) | ||
} | ||
|
||
pub async fn create_url(Extension(state): Extension<Arc<AxumState>>) -> String { | ||
let state_oauth = get_client() | ||
.generate_url( | ||
Vec::from(["public_profile".to_string(), "email".to_string()]), | ||
|state_e| async move { | ||
state.set(state_e.state, state_e.verifier); | ||
}, | ||
) | ||
.await | ||
.unwrap() | ||
.state | ||
.unwrap(); | ||
|
||
state_oauth.url_generated.unwrap() | ||
} | ||
|
||
pub async fn callback( | ||
Extension(state): Extension<Arc<AxumState>>, | ||
Query(queries): Query<QueryAxumCallback>, | ||
) -> String { | ||
println!("{:?}", state.clone().get_all_items()); | ||
let item = state.get(queries.state.clone()); | ||
get_client() | ||
.generate_token(queries.code, item.unwrap()) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
mod utils; | ||
use std::sync::Arc; | ||
|
||
use axum::extract::Query; | ||
use axum::Router; | ||
use axum::{routing::get, Extension}; | ||
use oauth_axum::providers::spotify::SpotifyProvider; | ||
use oauth_axum::{CustomProvider, OAuthClient}; | ||
|
||
use crate::utils::memory_db_util::AxumState; | ||
|
||
#[derive(Clone, serde::Deserialize)] | ||
pub struct QueryAxumCallback { | ||
pub code: String, | ||
pub state: String, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
dotenv::from_filename("examples/.env").ok(); | ||
println!("Starting server..."); | ||
|
||
let state = Arc::new(AxumState::new()); | ||
let app = Router::new() | ||
.route("/", get(create_url)) | ||
.route("/api/v1/spotify/callback", get(callback)) | ||
.layer(Extension(state.clone())); | ||
|
||
println!("🚀 Server started successfully"); | ||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") | ||
.await | ||
.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
} | ||
|
||
fn get_client() -> CustomProvider { | ||
SpotifyProvider::new( | ||
std::env::var("SPOTIFY_CLIENT_ID").expect("SPOTIFY_CLIENT_ID must be set"), | ||
std::env::var("SPOTIFY_SECRET").expect("SPOTIFY_SECRET must be set"), | ||
"http://localhost:3000/api/v1/spotify/callback".to_string(), | ||
) | ||
} | ||
|
||
pub async fn create_url(Extension(state): Extension<Arc<AxumState>>) -> String { | ||
let state_oauth = get_client() | ||
.generate_url( | ||
Vec::from([ | ||
"user-read-email".to_string(), | ||
"user-read-private".to_string(), | ||
]), | ||
|state_e| async move { | ||
state.set(state_e.state, state_e.verifier); | ||
}, | ||
) | ||
.await | ||
.unwrap() | ||
.state | ||
.unwrap(); | ||
|
||
state_oauth.url_generated.unwrap() | ||
} | ||
|
||
pub async fn callback( | ||
Extension(state): Extension<Arc<AxumState>>, | ||
Query(queries): Query<QueryAxumCallback>, | ||
) -> String { | ||
println!("{:?}", state.clone().get_all_items()); | ||
let item = state.get(queries.state.clone()); | ||
get_client() | ||
.generate_token(queries.code, item.unwrap()) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::CustomProvider; | ||
|
||
pub struct FacebookProvider {} | ||
|
||
impl FacebookProvider { | ||
pub fn new(client_id: String, client_secret: String, redirect_url: String) -> CustomProvider { | ||
CustomProvider::new( | ||
String::from("https://www.facebook.com/v19.0/dialog/oauth"), | ||
String::from("https://graph.facebook.com/v19.0/oauth/access_token"), | ||
client_id, | ||
client_secret, | ||
redirect_url, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub mod discord; | ||
pub mod facebook; | ||
pub mod github; | ||
pub mod google; | ||
pub mod microsoft; | ||
pub mod spotify; | ||
pub mod twitter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::CustomProvider; | ||
|
||
pub struct SpotifyProvider {} | ||
|
||
impl SpotifyProvider { | ||
pub fn new(client_id: String, client_secret: String, redirect_url: String) -> CustomProvider { | ||
CustomProvider::new( | ||
String::from("https://accounts.spotify.com/authorize"), | ||
String::from("https://accounts.spotify.com/api/token"), | ||
client_id, | ||
client_secret, | ||
redirect_url, | ||
) | ||
} | ||
} |