-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66bfb6c
commit 7fb7b77
Showing
8 changed files
with
426 additions
and
19 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,10 +1,74 @@ | ||
use super::super::app_config::*; | ||
use cfg_if::cfg_if; | ||
use leptos::{ev::MouseEvent, *}; | ||
use leptos_meta::*; | ||
use leptos_router::*; | ||
use leptos_server::*; | ||
use std::fmt; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::app_config::TENANT_ID; | ||
|
||
pub fn microsoft_login_url() -> String { | ||
format!("https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={}&response_type=code&redirect_uri=http%3A%2F%2Flocalhost:3000%2Fteam&response_mode=query&scope=https%3A%2F%2Fgraph.microsoft.com%2Femail", CLIENT_ID) | ||
format!("https://login.microsoftonline.com/{}/oauth2/v2.0/authorize?client_id={}&response_type=code&redirect_uri={}&response_mode=query&scope=User.Read", "common", CLIENT_ID, REDIRECT_URI) | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct AzureMeResponse { | ||
displayName: String, | ||
givenName: String, | ||
mail: String, | ||
userPrincipalName: String, | ||
id: String, | ||
} | ||
|
||
cfg_if! { | ||
if #[cfg(feature = "ssr")] { | ||
use actix_web::{get, HttpResponse, HttpRequest, web}; | ||
use crate::get_azure_secret; | ||
|
||
use actix_session::Session; | ||
|
||
#[derive(Deserialize)] | ||
pub struct MicrosoftLoginCode { | ||
code: Option<String>, | ||
} | ||
|
||
// https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow | ||
#[derive(Deserialize)] | ||
pub struct AzureAuthTokenResopnse { | ||
access_token: Option<String>, | ||
error: Option<String> | ||
} | ||
|
||
// By the end of this method, if given a valid authorization code, the email address field in the session should be set | ||
pub async fn handle_login(req: web::Query<MicrosoftLoginCode>, session: Session) -> Result<HttpResponse, Box<dyn std::error::Error>> { | ||
let code = req.code.clone().unwrap_or_default(); | ||
// TODO: Is it bad to make a new client for every login? | ||
let client = reqwest::Client::new(); | ||
let secret = get_azure_secret(); | ||
|
||
let response: AzureAuthTokenResopnse = client.post(format!("https://login.microsoftonline.com/{}/oauth2/v2.0/token", "common")).body( | ||
format!("code={}&client_id={}&redirect_uri={}&grant_type=authorization_code&client_secret={}", | ||
code, | ||
CLIENT_ID, | ||
REDIRECT_URI, | ||
secret | ||
) | ||
).send().await?.json().await?; | ||
if response.access_token.is_some() { | ||
let me: AzureMeResponse = client.get("https://graph.microsoft.com/v1.0/me") | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", response.access_token.unwrap()) | ||
.send().await?.json().await?; | ||
session.insert("me", me.clone())?; | ||
Ok(HttpResponse::Found().append_header(("Location", "/team")).finish()) | ||
} | ||
else { | ||
Ok(HttpResponse::Found().append_header(("Location", "/login")).finish()) | ||
} | ||
//Ok(HttpResponse::Ok().body(response.access_token.unwrap_or(response.error.unwrap_or("wtf".to_string())))) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
use cfg_if::cfg_if; | ||
pub const TEAM_SIZE: i32 = 5; | ||
pub const CLIENT_ID: &str = "42cb7f3f-e8fc-4e59-9bb6-2422f6dadbb0"; | ||
pub const CLIENT_ID: &str = "cc6185f1-7e94-4314-a79e-7d72d8fd68fc"; | ||
pub const REDIRECT_URI: &str = "http%3A%2F%2Flocalhost:3000%2Fapi%2Flogin"; | ||
pub const TENANT_ID: &str = "f8cdef31-a31e-4b4a-93e4-5f571e91255a"; |
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
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