mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-02-16 13:48:26 +00:00
Update openid example (#2474)
* Update openid example * Trim unnecessary deps * Update serve instructions * Remove unnecessary version in patch directive * Remove outdated workspace instructions * use storage signals instead of global signals with manual synchronization --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
parent
5494e38cf8
commit
054351139f
10 changed files with 6623 additions and 306 deletions
5
examples/openid_connect_demo/.cargo/config.toml
Normal file
5
examples/openid_connect_demo/.cargo/config.toml
Normal file
|
@ -0,0 +1,5 @@
|
|||
[env]
|
||||
DIOXUS_FRONT_ISSUER_URL = "TODO"
|
||||
DIOXUS_FRONT_CLIENT_ID = "TODO"
|
||||
DIOXUS_FRONT_CLIENT_SECRET = "TODO"
|
||||
DIOXUS_FRONT_URL = "http://localhost:8080"
|
6372
examples/openid_connect_demo/Cargo.lock
generated
Normal file
6372
examples/openid_connect_demo/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -7,19 +7,30 @@ publish = false
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
console_error_panic_hook = "0.1"
|
||||
dioxus-logger = "0.4.1"
|
||||
dioxus = { path = "../../packages/dioxus", version = "*" }
|
||||
dioxus-router = { path = "../../packages/router", version = "*" }
|
||||
dioxus-web = { path = "../../packages/web", version = "*" }
|
||||
fermi = { path = "../../packages/fermi", version = "*" }
|
||||
form_urlencoded = "1.2.0"
|
||||
gloo-storage = "0.3.0"
|
||||
dioxus = { path = "../../packages/dioxus", default_features = true, features = [
|
||||
"router",
|
||||
"signals",
|
||||
], version = "*" }
|
||||
dioxus-logger = "0.5.1"
|
||||
dioxus-sdk = { git = "https://github.com/Dioxuslabs/sdk", features = [
|
||||
"storage",
|
||||
] }
|
||||
form_urlencoded = "1.2.1"
|
||||
log = "0.4"
|
||||
openidconnect = "3.4.0"
|
||||
reqwest = "0.11.20"
|
||||
serde = { version = "1.0.188", features = ["derive"] }
|
||||
serde_json = "1.0.105"
|
||||
thiserror = "1.0.48"
|
||||
uuid = "1.4"
|
||||
web-sys = { version = "0.3", features = ["Request", "Document"] }
|
||||
openidconnect = "3.5.0"
|
||||
serde = { version = "1.0.203", features = ["derive"] }
|
||||
uuid = "1.8"
|
||||
|
||||
[features]
|
||||
default = ["web"]
|
||||
server = ["dioxus/axum"]
|
||||
web = ["dioxus/web"]
|
||||
desktop = ["dioxus/desktop"]
|
||||
fullstack = ["dioxus/fullstack"]
|
||||
|
||||
# since we're using dioxus from local path, inform dioxus-sdk to use it as well
|
||||
[patch.crates-io]
|
||||
dioxus = { path = "../../packages/dioxus" }
|
||||
dioxus-signals = { path = "../../packages/signals" }
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
# OpenID Connect example to show how to authenticate an user
|
||||
|
||||
The environment variables in `.cargo/config.toml` must be set in order for this example to work(if this example is just being compiled from the root workspace, the `.cargo/config.toml` from the root workspace must be set as stated in the [Cargo book](https://doc.rust-lang.org/cargo/reference/config.html)).
|
||||
The environment variables in [`.cargo/config.toml`](./.cargo/config.toml) must be set in order for this example to work.
|
||||
|
||||
Once they are set, you can run `dx serve`
|
||||
Once they are set, you can run `dx serve --platform web` or `dx serve --platform desktop`.
|
||||
|
||||
### Environment variables summary
|
||||
|
||||
```DIOXUS_FRONT_ISSUER_URL``` The openid-connect's issuer url
|
||||
|
||||
```DIOXUS_FRONT_CLIENT_ID``` The openid-connect's client id
|
||||
|
||||
```DIOXUS_FRONT_URL``` The url the frontend is supposed to be running on, it could be for example `http://localhost:8080`
|
||||
- `DIOXUS_FRONT_ISSUER_URL`: The openid-connect's issuer url
|
||||
- `DIOXUS_FRONT_CLIENT_ID`: The openid-connect's client id
|
||||
- `DIOXUS_FRONT_CLIENT_SECRET`: The openid-connect's client secret
|
||||
- `DIOXUS_FRONT_URL`: The url the frontend is supposed to be running on, it could be for example `http://localhost:8080`
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
use openidconnect::{core::CoreErrorResponseType, url, RequestTokenError, StandardErrorResponse};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Discovery error: {0}")]
|
||||
OpenIdConnect(
|
||||
#[from] openidconnect::DiscoveryError<openidconnect::reqwest::Error<reqwest::Error>>,
|
||||
),
|
||||
#[error("Parsing error: {0}")]
|
||||
Parse(#[from] url::ParseError),
|
||||
#[error("Request token error: {0}")]
|
||||
RequestToken(
|
||||
#[from]
|
||||
RequestTokenError<
|
||||
openidconnect::reqwest::Error<reqwest::Error>,
|
||||
StandardErrorResponse<CoreErrorResponseType>,
|
||||
>,
|
||||
),
|
||||
}
|
|
@ -1,58 +1,36 @@
|
|||
#![allow(non_snake_case)]
|
||||
use dioxus::prelude::*;
|
||||
use fermi::*;
|
||||
use gloo_storage::{LocalStorage, Storage};
|
||||
use log::LevelFilter;
|
||||
use dioxus_logger::tracing::Level;
|
||||
use router::Route;
|
||||
|
||||
use crate::oidc::ClientState;
|
||||
use crate::storage::{use_auth_request_provider, use_auth_token_provider};
|
||||
|
||||
pub(crate) mod constants;
|
||||
pub(crate) mod errors;
|
||||
pub(crate) mod model;
|
||||
pub(crate) mod oidc;
|
||||
pub(crate) mod props;
|
||||
pub(crate) mod router;
|
||||
pub(crate) mod storage;
|
||||
pub(crate) mod views;
|
||||
use oidc::{AuthRequestState, AuthTokenState};
|
||||
use router::Route;
|
||||
|
||||
use crate::{
|
||||
constants::{DIOXUS_FRONT_AUTH_REQUEST, DIOXUS_FRONT_AUTH_TOKEN},
|
||||
oidc::ClientState,
|
||||
};
|
||||
pub static FERMI_CLIENT: fermi::AtomRef<ClientState> = AtomRef(|_| ClientState::default());
|
||||
|
||||
// An option is required to prevent the component from being constantly refreshed
|
||||
pub static FERMI_AUTH_TOKEN: fermi::AtomRef<Option<AuthTokenState>> = AtomRef(|_| None);
|
||||
pub static FERMI_AUTH_REQUEST: fermi::AtomRef<Option<AuthRequestState>> = AtomRef(|_| None);
|
||||
pub static CLIENT: GlobalSignal<ClientState> = Signal::global(ClientState::default);
|
||||
|
||||
pub static DIOXUS_FRONT_ISSUER_URL: &str = env!("DIOXUS_FRONT_ISSUER_URL");
|
||||
pub static DIOXUS_FRONT_CLIENT_ID: &str = env!("DIOXUS_FRONT_CLIENT_ID");
|
||||
pub static DIOXUS_FRONT_CLIENT_SECRET: &str = env!("DIOXUS_FRONT_CLIENT_SECRET");
|
||||
pub static DIOXUS_FRONT_URL: &str = env!("DIOXUS_FRONT_URL");
|
||||
|
||||
fn App() -> Element {
|
||||
use_init_atom_root(cx);
|
||||
|
||||
// Retrieve the value stored in the browser's storage
|
||||
let stored_auth_token = LocalStorage::get(DIOXUS_FRONT_AUTH_TOKEN)
|
||||
.ok()
|
||||
.unwrap_or(AuthTokenState::default());
|
||||
let fermi_auth_token = use_atom_ref(&FERMI_AUTH_TOKEN);
|
||||
if fermi_auth_token.read().is_none() {
|
||||
*fermi_auth_token.write() = Some(stored_auth_token);
|
||||
}
|
||||
|
||||
let stored_auth_request = LocalStorage::get(DIOXUS_FRONT_AUTH_REQUEST)
|
||||
.ok()
|
||||
.unwrap_or(AuthRequestState::default());
|
||||
let fermi_auth_request = use_atom_ref(&FERMI_AUTH_REQUEST);
|
||||
if fermi_auth_request.read().is_none() {
|
||||
*fermi_auth_request.write() = Some(stored_auth_request);
|
||||
}
|
||||
use_auth_request_provider();
|
||||
use_auth_token_provider();
|
||||
rsx! { Router::<Route> {} }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dioxus_logger::init(LevelFilter::Info).expect("failed to init logger");
|
||||
dioxus_logger::init(Level::DEBUG).expect("failed to init logger");
|
||||
dioxus_sdk::set_dir!();
|
||||
console_error_panic_hook::set_once();
|
||||
log::info!("starting app");
|
||||
dioxus_web::launch(App);
|
||||
launch(App);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use anyhow::Result;
|
||||
use openidconnect::{
|
||||
core::{CoreClient, CoreErrorResponseType, CoreIdToken, CoreResponseType, CoreTokenResponse},
|
||||
core::{CoreClient, CoreIdToken, CoreResponseType, CoreTokenResponse},
|
||||
reqwest::async_http_client,
|
||||
url::Url,
|
||||
AuthenticationFlow, AuthorizationCode, ClaimsVerificationError, ClientId, CsrfToken, IssuerUrl,
|
||||
LogoutRequest, Nonce, ProviderMetadataWithLogout, RedirectUrl, RefreshToken, RequestTokenError,
|
||||
StandardErrorResponse,
|
||||
AuthenticationFlow, AuthorizationCode, ClaimsVerificationError, ClientId, ClientSecret,
|
||||
CsrfToken, IssuerUrl, LogoutRequest, Nonce, ProviderMetadataWithLogout, RedirectUrl,
|
||||
RefreshToken,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -16,12 +17,12 @@ pub struct ClientState {
|
|||
}
|
||||
|
||||
/// State that holds the nonce and authorization url and the nonce generated to log in an user
|
||||
#[derive(Clone, Deserialize, Serialize, Default)]
|
||||
#[derive(Clone, PartialEq, Deserialize, Serialize, Default)]
|
||||
pub struct AuthRequestState {
|
||||
pub auth_request: Option<AuthRequest>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
#[derive(Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct AuthRequest {
|
||||
pub nonce: Nonce,
|
||||
pub authorize_url: String,
|
||||
|
@ -36,6 +37,14 @@ pub struct AuthTokenState {
|
|||
pub refresh_token: Option<RefreshToken>,
|
||||
}
|
||||
|
||||
impl PartialEq for AuthTokenState {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id_token == other.id_token
|
||||
&& self.refresh_token.as_ref().map(|t| t.secret().clone())
|
||||
== other.refresh_token.as_ref().map(|t| t.secret().clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn email(
|
||||
client: CoreClient,
|
||||
id_token: CoreIdToken,
|
||||
|
@ -63,15 +72,17 @@ pub fn authorize_url(client: CoreClient) -> AuthRequest {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn init_provider_metadata() -> Result<ProviderMetadataWithLogout, crate::errors::Error> {
|
||||
pub async fn init_provider_metadata() -> Result<ProviderMetadataWithLogout> {
|
||||
let issuer_url = IssuerUrl::new(crate::DIOXUS_FRONT_ISSUER_URL.to_string())?;
|
||||
Ok(ProviderMetadataWithLogout::discover_async(issuer_url, async_http_client).await?)
|
||||
}
|
||||
|
||||
pub async fn init_oidc_client() -> Result<(ClientId, CoreClient), crate::errors::Error> {
|
||||
pub async fn init_oidc_client() -> Result<(ClientId, CoreClient)> {
|
||||
let client_id = ClientId::new(crate::DIOXUS_FRONT_CLIENT_ID.to_string());
|
||||
let provider_metadata = init_provider_metadata().await?;
|
||||
let client_secret = None;
|
||||
let client_secret = Some(ClientSecret::new(
|
||||
crate::DIOXUS_FRONT_CLIENT_SECRET.to_string(),
|
||||
));
|
||||
let redirect_url = RedirectUrl::new(format!("{}/login", crate::DIOXUS_FRONT_URL))?;
|
||||
|
||||
Ok((
|
||||
|
@ -82,10 +93,7 @@ pub async fn init_oidc_client() -> Result<(ClientId, CoreClient), crate::errors:
|
|||
}
|
||||
|
||||
///TODO: Add pkce_pacifier
|
||||
pub async fn token_response(
|
||||
oidc_client: CoreClient,
|
||||
code: String,
|
||||
) -> Result<CoreTokenResponse, crate::errors::Error> {
|
||||
pub async fn token_response(oidc_client: CoreClient, code: String) -> Result<CoreTokenResponse> {
|
||||
// let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
|
||||
Ok(oidc_client
|
||||
.exchange_code(AuthorizationCode::new(code.clone()))
|
||||
|
@ -97,20 +105,14 @@ pub async fn token_response(
|
|||
pub async fn exchange_refresh_token(
|
||||
oidc_client: CoreClient,
|
||||
refresh_token: RefreshToken,
|
||||
) -> Result<
|
||||
CoreTokenResponse,
|
||||
RequestTokenError<
|
||||
openidconnect::reqwest::Error<reqwest::Error>,
|
||||
StandardErrorResponse<CoreErrorResponseType>,
|
||||
>,
|
||||
> {
|
||||
oidc_client
|
||||
) -> Result<CoreTokenResponse> {
|
||||
Ok(oidc_client
|
||||
.exchange_refresh_token(&refresh_token)
|
||||
.request_async(async_http_client)
|
||||
.await
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn log_out_url(id_token_hint: CoreIdToken) -> Result<Url, crate::errors::Error> {
|
||||
pub async fn log_out_url(id_token_hint: CoreIdToken) -> Result<Url> {
|
||||
let provider_metadata = init_provider_metadata().await?;
|
||||
let end_session_url = provider_metadata
|
||||
.additional_metadata()
|
||||
|
|
|
@ -1,38 +1,35 @@
|
|||
use fermi::UseAtomRef;
|
||||
use gloo_storage::{LocalStorage, Storage};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_sdk::storage::*;
|
||||
|
||||
use crate::{
|
||||
constants::{DIOXUS_FRONT_AUTH_REQUEST, DIOXUS_FRONT_AUTH_TOKEN},
|
||||
oidc::{AuthRequestState, AuthTokenState},
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct StorageEntry<T> {
|
||||
pub key: String,
|
||||
pub value: T,
|
||||
pub fn use_auth_token_provider() {
|
||||
let stored_token =
|
||||
use_storage::<LocalStorage, _>(DIOXUS_FRONT_AUTH_TOKEN.to_owned(), AuthTokenState::default);
|
||||
|
||||
use_context_provider(move || stored_token);
|
||||
}
|
||||
|
||||
pub trait PersistentWrite<T: Serialize + Clone> {
|
||||
fn persistent_set(atom_ref: &UseAtomRef<Option<T>>, entry: Option<T>);
|
||||
pub fn use_auth_token() -> Signal<AuthTokenState> {
|
||||
use_context()
|
||||
}
|
||||
|
||||
impl PersistentWrite<AuthTokenState> for AuthTokenState {
|
||||
fn persistent_set(
|
||||
atom_ref: &UseAtomRef<Option<AuthTokenState>>,
|
||||
entry: Option<AuthTokenState>,
|
||||
) {
|
||||
*atom_ref.write() = entry.clone();
|
||||
LocalStorage::set(DIOXUS_FRONT_AUTH_TOKEN, entry).unwrap();
|
||||
}
|
||||
pub fn use_auth_request_provider() {
|
||||
let stored_req = use_storage::<LocalStorage, _>(
|
||||
DIOXUS_FRONT_AUTH_REQUEST.to_owned(),
|
||||
AuthRequestState::default,
|
||||
);
|
||||
|
||||
use_context_provider(move || stored_req);
|
||||
}
|
||||
|
||||
impl PersistentWrite<AuthRequestState> for AuthRequestState {
|
||||
fn persistent_set(
|
||||
atom_ref: &UseAtomRef<Option<AuthRequestState>>,
|
||||
entry: Option<AuthRequestState>,
|
||||
) {
|
||||
*atom_ref.write() = entry.clone();
|
||||
LocalStorage::set(DIOXUS_FRONT_AUTH_REQUEST, entry).unwrap();
|
||||
}
|
||||
pub fn use_auth_request() -> Signal<AuthRequestState> {
|
||||
use_context()
|
||||
}
|
||||
|
||||
pub fn auth_request() -> Signal<AuthRequestState> {
|
||||
consume_context()
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::storage::{auth_request, use_auth_request, use_auth_token};
|
||||
use crate::{
|
||||
oidc::{
|
||||
authorize_url, email, exchange_refresh_token, init_oidc_client, log_out_url,
|
||||
|
@ -5,149 +6,124 @@ use crate::{
|
|||
},
|
||||
props::client::ClientProps,
|
||||
router::Route,
|
||||
storage::PersistentWrite,
|
||||
FERMI_AUTH_REQUEST, FERMI_AUTH_TOKEN, FERMI_CLIENT,
|
||||
CLIENT,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use dioxus::prelude::*;
|
||||
use dioxus::router::prelude::{Link, Outlet};
|
||||
use fermi::*;
|
||||
use openidconnect::{url::Url, OAuth2TokenResponse, TokenResponse};
|
||||
|
||||
#[component]
|
||||
pub fn LogOut(cx: Scope<ClientProps>) -> Element {
|
||||
let fermi_auth_token = use_atom_ref(&FERMI_AUTH_TOKEN);
|
||||
let fermi_auth_token_read = fermi_auth_token.read().clone();
|
||||
let log_out_url_state = use_signal(|| None::<Option<Result<Url, crate::errors::Error>>>);
|
||||
cx.render(match fermi_auth_token_read {
|
||||
Some(fermi_auth_token_read) => match fermi_auth_token_read.id_token.clone() {
|
||||
Some(id_token) => match log_out_url_state.get() {
|
||||
Some(log_out_url_result) => match log_out_url_result {
|
||||
Some(uri) => match uri {
|
||||
Ok(uri) => {
|
||||
rsx! {
|
||||
Link {
|
||||
onclick: move |_| {
|
||||
{
|
||||
AuthTokenState::persistent_set(
|
||||
fermi_auth_token,
|
||||
Some(AuthTokenState::default()),
|
||||
);
|
||||
}
|
||||
},
|
||||
to: uri.to_string(),
|
||||
"Log out"
|
||||
}
|
||||
pub fn LogOut() -> Element {
|
||||
let mut auth_token = use_auth_token();
|
||||
let log_out_url_state = use_signal(|| None::<Option<Result<Url>>>);
|
||||
match auth_token().id_token {
|
||||
Some(id_token) => match &*log_out_url_state.read() {
|
||||
Some(log_out_url_result) => match log_out_url_result {
|
||||
Some(uri) => match uri {
|
||||
Ok(uri) => {
|
||||
rsx! {
|
||||
Link {
|
||||
onclick: move |_| {
|
||||
auth_token.take();
|
||||
},
|
||||
to: uri.to_string(),
|
||||
"Log out"
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
rsx! { div { "Failed to load disconnection url: {error:?}" } }
|
||||
}
|
||||
},
|
||||
None => {
|
||||
rsx! { div { "Loading... Please wait" } }
|
||||
}
|
||||
Err(error) => {
|
||||
rsx! { div { "Failed to load disconnection url: {error:?}" } }
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let logout_url_task = move || {
|
||||
cx.spawn({
|
||||
let log_out_url_state = log_out_url_state.to_owned();
|
||||
async move {
|
||||
let logout_url = log_out_url(id_token).await;
|
||||
let logout_url_option = Some(logout_url);
|
||||
log_out_url_state.set(Some(logout_url_option));
|
||||
}
|
||||
})
|
||||
};
|
||||
logout_url_task();
|
||||
rsx! { div { "Loading log out url... Please wait" } }
|
||||
rsx! { div { "Loading... Please wait" } }
|
||||
}
|
||||
},
|
||||
None => {
|
||||
rsx! {{}}
|
||||
let logout_url_task = move || {
|
||||
spawn({
|
||||
let mut log_out_url_state = log_out_url_state.to_owned();
|
||||
async move {
|
||||
let logout_url = log_out_url(id_token).await;
|
||||
let logout_url_option = Some(logout_url);
|
||||
log_out_url_state.set(Some(logout_url_option));
|
||||
}
|
||||
})
|
||||
};
|
||||
logout_url_task();
|
||||
rsx! { div { "Loading log out url... Please wait" } }
|
||||
}
|
||||
},
|
||||
None => {
|
||||
rsx! {{}}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn RefreshToken(cx: Scope<ClientProps>) -> Element {
|
||||
let fermi_auth_token = use_atom_ref(&FERMI_AUTH_TOKEN);
|
||||
let fermi_auth_request = use_atom_ref(&FERMI_AUTH_REQUEST);
|
||||
let fermi_auth_token_read = fermi_auth_token.read().clone();
|
||||
cx.render(match fermi_auth_token_read {
|
||||
Some(fermi_auth_client_read) => match fermi_auth_client_read.refresh_token {
|
||||
Some(refresh_token) => {
|
||||
let fermi_auth_token = fermi_auth_token.to_owned();
|
||||
let fermi_auth_request = fermi_auth_request.to_owned();
|
||||
let client = cx.props.client.clone();
|
||||
let exchange_refresh_token_spawn = move || {
|
||||
cx.spawn({
|
||||
pub fn RefreshToken(props: ClientProps) -> Element {
|
||||
let mut auth_token = use_auth_token();
|
||||
match auth_token().refresh_token {
|
||||
Some(refresh_token) => {
|
||||
rsx! { div {
|
||||
onmounted: {
|
||||
move |_| {
|
||||
let client = props.client.clone();
|
||||
let refresh_token = refresh_token.clone();
|
||||
async move {
|
||||
let exchange_refresh_token =
|
||||
exchange_refresh_token(client, refresh_token).await;
|
||||
match exchange_refresh_token {
|
||||
Ok(response_token) => {
|
||||
AuthTokenState::persistent_set(
|
||||
&fermi_auth_token,
|
||||
Some(AuthTokenState {
|
||||
id_token: response_token.id_token().cloned(),
|
||||
refresh_token: response_token.refresh_token().cloned(),
|
||||
}),
|
||||
);
|
||||
auth_token.set(AuthTokenState {
|
||||
id_token: response_token.id_token().cloned(),
|
||||
refresh_token: response_token.refresh_token().cloned(),
|
||||
});
|
||||
}
|
||||
Err(_error) => {
|
||||
AuthTokenState::persistent_set(
|
||||
&fermi_auth_token,
|
||||
Some(AuthTokenState::default()),
|
||||
);
|
||||
AuthRequestState::persistent_set(
|
||||
&fermi_auth_request,
|
||||
Some(AuthRequestState::default()),
|
||||
);
|
||||
auth_token.take();
|
||||
auth_request().take();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
exchange_refresh_token_spawn();
|
||||
rsx! { div { "Refreshing session, please wait" } }
|
||||
}
|
||||
None => {
|
||||
rsx! { div { "Id token expired and no refresh token found" } }
|
||||
}
|
||||
},
|
||||
None => {
|
||||
rsx! {{}}
|
||||
}
|
||||
},
|
||||
"Refreshing session, please wait"
|
||||
} }
|
||||
}
|
||||
})
|
||||
None => {
|
||||
rsx! { div { "Id token expired and no refresh token found" } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn LoadClient() -> Element {
|
||||
let init_client_future = use_future(move || async move { init_oidc_client().await });
|
||||
let fermi_client: &UseAtomRef<ClientState> = use_atom_ref(&FERMI_CLIENT);
|
||||
cx.render(match init_client_future.value() {
|
||||
Some(client_props) => match client_props {
|
||||
Ok((client_id, client)) => {
|
||||
*fermi_client.write() = ClientState {
|
||||
oidc_client: Some(ClientProps::new(client_id.clone(), client.clone())),
|
||||
};
|
||||
rsx! {
|
||||
div { "Client successfully loaded" }
|
||||
Outlet::<Route> {}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
log::info! {"Failed to load client: {:?}", error};
|
||||
rsx! {
|
||||
div { "Failed to load client: {error:?}" }
|
||||
Outlet::<Route> {}
|
||||
}
|
||||
let init_client_future = use_resource(move || async move { init_oidc_client().await });
|
||||
match &*init_client_future.read_unchecked() {
|
||||
Some(Ok((client_id, client))) => rsx! {
|
||||
div {
|
||||
onmounted: {
|
||||
let client_id = client_id.clone();
|
||||
let client = client.clone();
|
||||
move |_| {
|
||||
*CLIENT.write() = ClientState {
|
||||
oidc_client: Some(ClientProps::new(client_id.clone(), client.clone())),
|
||||
};
|
||||
}
|
||||
},
|
||||
"Client successfully loaded"
|
||||
}
|
||||
Outlet::<Route> {}
|
||||
},
|
||||
Some(Err(error)) => {
|
||||
log::info! {"Failed to load client: {:?}", error};
|
||||
rsx! {
|
||||
div { "Failed to load client: {error:?}" }
|
||||
Outlet::<Route> {}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
rsx! {
|
||||
div {
|
||||
|
@ -156,34 +132,31 @@ pub fn LoadClient() -> Element {
|
|||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn AuthHeader() -> Element {
|
||||
let auth_token = use_atom_ref(&FERMI_AUTH_TOKEN);
|
||||
let fermi_auth_request = use_atom_ref(&FERMI_AUTH_REQUEST);
|
||||
let fermi_client: &UseAtomRef<ClientState> = use_atom_ref(&FERMI_CLIENT);
|
||||
let client = fermi_client.read().oidc_client.clone();
|
||||
let auth_request_read = fermi_auth_request.read().clone();
|
||||
let auth_token_read = auth_token.read().clone();
|
||||
cx.render(match (client, auth_request_read, auth_token_read) {
|
||||
let client = CLIENT.read().oidc_client.clone();
|
||||
let mut auth_request = use_auth_request();
|
||||
let auth_token = use_auth_token();
|
||||
match (client, auth_request(), auth_token()) {
|
||||
// We have everything we need to attempt to authenticate the user
|
||||
(Some(client_props), Some(auth_request), Some(auth_token)) => {
|
||||
match auth_request.auth_request {
|
||||
Some(auth_request) => {
|
||||
match auth_token.id_token {
|
||||
(Some(client_props), current_auth_request, current_auth_token) => {
|
||||
match current_auth_request.auth_request {
|
||||
Some(new_auth_request) => {
|
||||
match current_auth_token.id_token {
|
||||
Some(id_token) => {
|
||||
match email(
|
||||
client_props.client.clone(),
|
||||
id_token.clone(),
|
||||
auth_request.nonce.clone(),
|
||||
new_auth_request.nonce.clone(),
|
||||
) {
|
||||
Ok(email) => {
|
||||
rsx! {
|
||||
div {
|
||||
div { {email} }
|
||||
LogOut { client_id: client_props.client_id, client: client_props.client }
|
||||
LogOut {}
|
||||
Outlet::<Route> {}
|
||||
}
|
||||
}
|
||||
|
@ -217,7 +190,7 @@ pub fn AuthHeader() -> Element {
|
|||
None => {
|
||||
rsx! {
|
||||
div {
|
||||
Link { to: auth_request.authorize_url.clone(), "Log in" }
|
||||
Link { to: new_auth_request.authorize_url.clone(), "Log in" }
|
||||
Outlet::<Route> {}
|
||||
}
|
||||
}
|
||||
|
@ -225,14 +198,18 @@ pub fn AuthHeader() -> Element {
|
|||
}
|
||||
}
|
||||
None => {
|
||||
let auth_request = authorize_url(client_props.client);
|
||||
AuthRequestState::persistent_set(
|
||||
fermi_auth_request,
|
||||
Some(AuthRequestState {
|
||||
auth_request: Some(auth_request),
|
||||
}),
|
||||
);
|
||||
rsx! { div { "Loading nonce" } }
|
||||
rsx! { div {
|
||||
onmounted: {
|
||||
let client = client_props.client;
|
||||
move |_| {
|
||||
let new_auth_request = authorize_url(client.clone());
|
||||
auth_request.set(AuthRequestState {
|
||||
auth_request: Some(new_auth_request),
|
||||
});
|
||||
}
|
||||
},
|
||||
"Loading nonce"
|
||||
} }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -240,9 +217,5 @@ pub fn AuthHeader() -> Element {
|
|||
(None, _, _) => {
|
||||
rsx! { LoadClient {} }
|
||||
}
|
||||
// We need everything loaded before doing anything
|
||||
(_client, _auth_request, _auth_token) => {
|
||||
rsx! {{}}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,29 +1,28 @@
|
|||
use crate::{
|
||||
oidc::{token_response, AuthRequestState, AuthTokenState},
|
||||
oidc::{token_response, AuthTokenState},
|
||||
router::Route,
|
||||
storage::PersistentWrite,
|
||||
DIOXUS_FRONT_URL, FERMI_AUTH_REQUEST, FERMI_AUTH_TOKEN, FERMI_CLIENT,
|
||||
storage::{auth_request, use_auth_token},
|
||||
CLIENT,
|
||||
};
|
||||
use dioxus::prelude::*;
|
||||
use dioxus::router::prelude::{Link, NavigationTarget};
|
||||
use fermi::*;
|
||||
use dioxus::router::prelude::Link;
|
||||
use openidconnect::{OAuth2TokenResponse, TokenResponse};
|
||||
|
||||
#[component]
|
||||
pub fn Login(query_string: String) -> Element {
|
||||
let fermi_client = use_atom_ref(&FERMI_CLIENT);
|
||||
let fermi_auth_token = use_atom_ref(&FERMI_AUTH_TOKEN);
|
||||
let home_url: NavigationTarget<Route> = DIOXUS_FRONT_URL.parse().unwrap();
|
||||
let fermi_auth_request = use_atom_ref(&FERMI_AUTH_REQUEST);
|
||||
let client = fermi_client.read().oidc_client.clone();
|
||||
let auth_token_read = fermi_auth_token.read().clone();
|
||||
cx.render(match (client, auth_token_read) {
|
||||
(Some(client_props), Some(auth_token_read)) => {
|
||||
match (auth_token_read.id_token, auth_token_read.refresh_token) {
|
||||
let client = CLIENT.read().oidc_client.clone();
|
||||
let mut auth_token = use_auth_token();
|
||||
let current_auth_token = auth_token();
|
||||
match client {
|
||||
Some(client_props) => {
|
||||
match (
|
||||
current_auth_token.id_token,
|
||||
current_auth_token.refresh_token,
|
||||
) {
|
||||
(Some(_id_token), Some(_refresh_token)) => {
|
||||
rsx! {
|
||||
div { "Sign in successful" }
|
||||
Link { to: home_url, "Go back home" }
|
||||
Link { to: Route::Home {}, "Go back home" }
|
||||
}
|
||||
}
|
||||
// If the refresh token is set but not the id_token, there was an error, we just go back home and reset their value
|
||||
|
@ -31,13 +30,10 @@ pub fn Login(query_string: String) -> Element {
|
|||
rsx! {
|
||||
div { "Error while attempting to log in" }
|
||||
Link {
|
||||
to: home_url,
|
||||
to: Route::Home {},
|
||||
onclick: move |_| {
|
||||
AuthTokenState::persistent_set(fermi_auth_token, Some(AuthTokenState::default()));
|
||||
AuthRequestState::persistent_set(
|
||||
fermi_auth_request,
|
||||
Some(AuthRequestState::default()),
|
||||
);
|
||||
auth_token.take();
|
||||
auth_request().take();
|
||||
},
|
||||
"Go back home"
|
||||
}
|
||||
|
@ -45,42 +41,46 @@ pub fn Login(query_string: String) -> Element {
|
|||
}
|
||||
(None, None) => {
|
||||
let mut query_pairs = form_urlencoded::parse(query_string.as_bytes());
|
||||
let code_pair = query_pairs.find(|(key, _value)| key == "code");
|
||||
match code_pair {
|
||||
Some((_key, code)) => {
|
||||
let auth_code = code.to_string();
|
||||
let token_response_spawn = move ||{
|
||||
cx.spawn({
|
||||
let fermi_auth_token = fermi_auth_token.to_owned();
|
||||
async move {
|
||||
let token_response_result = token_response(client_props.client, auth_code).await;
|
||||
match token_response_result{
|
||||
Ok(token_response) => {
|
||||
let id_token = token_response.id_token().unwrap();
|
||||
AuthTokenState::persistent_set(&fermi_auth_token, Some(AuthTokenState {
|
||||
id_token: Some(id_token.clone()),
|
||||
refresh_token: token_response.refresh_token().cloned()
|
||||
}));
|
||||
}
|
||||
Err(error) => {
|
||||
log::warn!{"{error}"};
|
||||
let code_pair = query_pairs.find(|(key, _value)| key == "code");
|
||||
match code_pair {
|
||||
Some((_key, code)) => {
|
||||
let code = code.to_string();
|
||||
rsx! { div {
|
||||
onmounted: {
|
||||
move |_| {
|
||||
let auth_code = code.to_string();
|
||||
let client_props = client_props.clone();
|
||||
async move {
|
||||
let token_response_result =
|
||||
token_response(client_props.client, auth_code).await;
|
||||
match token_response_result {
|
||||
Ok(token_response) => {
|
||||
let id_token = token_response.id_token().unwrap();
|
||||
auth_token.set(AuthTokenState {
|
||||
id_token: Some(id_token.clone()),
|
||||
refresh_token: token_response
|
||||
.refresh_token()
|
||||
.cloned(),
|
||||
});
|
||||
}
|
||||
Err(error) => {
|
||||
log::warn! {"{error}"};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
token_response_spawn();
|
||||
rsx!{ div {} }
|
||||
}}
|
||||
}
|
||||
None => {
|
||||
rsx! { div { "No code provided" } }
|
||||
}
|
||||
}
|
||||
None => {
|
||||
rsx! { div { "No code provided" } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(_, _) => {
|
||||
_ => {
|
||||
rsx! {{}}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue