-
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 pull request #2 from rano-oss/provider/microsoft
Added microsoft provider
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 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
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,67 @@ | ||
mod utils; | ||
use std::sync::Arc; | ||
|
||
use axum::extract::Query; | ||
use axum::Router; | ||
use axum::{routing::get, Extension}; | ||
use oauth_axum::providers::microsoft::MicrosoftProvider; | ||
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/microsoft/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 { | ||
MicrosoftProvider::new( | ||
std::env::var("MICROSOFT_TENANT_ID").expect("MICROSOFT_TENANT_ID must be set"), | ||
std::env::var("MICROSOFT_CLIENT_ID").expect("MICROSOFT_CLIENT_ID must be set"), | ||
std::env::var("MICROSOFT_SECRET").expect("MICROSOFT_SECRET must be set"), | ||
"http://localhost:3000/api/v1/microsoft/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".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,23 @@ | ||
use crate::CustomProvider; | ||
|
||
pub struct MicrosoftProvider {} | ||
|
||
impl MicrosoftProvider { | ||
pub fn new( | ||
tenant_id: String, | ||
client_id: String, | ||
client_secret: String, | ||
redirect_url: String, | ||
) -> CustomProvider { | ||
let base_url = String::from( | ||
"https://login.microsoftonline.com/".to_string() + tenant_id.as_str() + "/oauth2/v2.0", | ||
); | ||
CustomProvider::new( | ||
String::from(base_url.clone() + "/authorize"), | ||
String::from(base_url + "/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