1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-22 06:51:10 +00:00

Use auth header

This commit is contained in:
Felix Ableitner 2024-11-14 11:27:55 +01:00
parent d9020a15d4
commit 66f89d3bfd
22 changed files with 61 additions and 77 deletions

View file

@ -12,20 +12,12 @@ use crate::{
search_article,
},
instance::{follow_instance, get_instance, resolve_instance},
user::{
get_user,
login_user,
logout_user,
my_profile,
register_user,
validate,
AUTH_COOKIE,
},
user::{get_user, login_user, logout_user, my_profile, register_user, validate},
},
database::IbisData,
error::MyResult,
},
common::LocalUserView,
common::{LocalUserView, AUTH_COOKIE},
};
use activitypub_federation::config::Data;
use anyhow::anyhow;
@ -78,8 +70,15 @@ async fn auth(
mut request: Request<Body>,
next: Next,
) -> Result<Response, StatusCode> {
if let Some(auth) = jar.get(AUTH_COOKIE) {
if let Ok(user) = validate(auth.value(), &data).await {
let auth = request
.headers()
.get(AUTH_COOKIE)
.map(|h| h.to_str().ok())
.flatten()
.or(jar.get(AUTH_COOKIE).map(|c| c.value()));
if let Some(auth) = auth {
if let Ok(user) = validate(auth, &data).await {
request.extensions_mut().insert(user);
}
}

View file

@ -12,6 +12,7 @@ use crate::{
LoginUserForm,
Notification,
RegisterUserForm,
AUTH_COOKIE,
},
};
use activitypub_federation::config::Data;
@ -34,8 +35,6 @@ use jsonwebtoken::{
use serde::{Deserialize, Serialize};
use time::{Duration, OffsetDateTime};
pub static AUTH_COOKIE: &str = "auth";
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
/// person.username

View file

@ -13,6 +13,7 @@ use crate::{
DbInstance,
DbPerson,
EditVersion,
AUTH_COOKIE,
MAIN_PAGE_NAME,
},
frontend::app::App,
@ -22,7 +23,7 @@ use activitypub_federation::{
fetch::object_id::ObjectId,
http_signatures::generate_actor_keypair,
};
use api::{api_routes, user::AUTH_COOKIE};
use api::api_routes;
use assets::file_and_error_handler;
use axum::{
body::Body,

View file

@ -21,6 +21,8 @@ use {
pub const MAIN_PAGE_NAME: &str = "Main_Page";
pub static AUTH_COOKIE: &str = "auth";
#[derive(Clone)]
pub struct Auth(pub Option<String>);

View file

@ -1,5 +1,3 @@
use std::sync::LazyLock;
use crate::{
common::{
newtypes::ArticleId,
@ -32,11 +30,10 @@ use crate::{
use anyhow::anyhow;
use reqwest::{Client, RequestBuilder, StatusCode};
use serde::{Deserialize, Serialize};
use std::sync::LazyLock;
use url::Url;
pub static CLIENT: LazyLock<ApiClient> = LazyLock::new(|| {
ApiClient::new(Client::new(), None)
});
pub static CLIENT: LazyLock<ApiClient> = LazyLock::new(|| ApiClient::new(Client::new(), None));
#[derive(Clone)]
pub struct ApiClient {
@ -276,13 +273,13 @@ where
#[cfg(feature = "ssr")]
{
use crate::common::Auth;
use crate::common::{Auth, AUTH_COOKIE};
use leptos::use_context;
use reqwest::header::HeaderName;
let auth = use_context::<Auth>();
if let Some(Auth(Some(auth))) = auth {
req = req.header(HeaderName::from_static("auth"), auth);
req = req.header(HeaderName::from_static(AUTH_COOKIE), auth);
}
}
let res = req.send().await?;

View file

@ -1,6 +1,7 @@
use crate::{
common::LocalUserView,
frontend::{
api::CLIENT,
components::nav::Nav,
dark_mode::DarkMode,
pages::{
@ -38,7 +39,6 @@ use leptos::{
};
use leptos_meta::{provide_meta_context, *};
use leptos_router::{Route, Router, Routes};
use crate::frontend::api::CLIENT;
// https://book.leptos.dev/15_global_state.html
#[derive(Clone)]
@ -47,7 +47,6 @@ pub struct GlobalState {
}
impl GlobalState {
pub fn update_my_profile() {
create_local_resource(
move || (),
@ -74,9 +73,7 @@ impl GlobalState {
#[component]
pub fn App() -> impl IntoView {
provide_meta_context();
let global_state = GlobalState {
my_profile: None,
};
let global_state = GlobalState { my_profile: None };
// Load user profile in case we are already logged in
GlobalState::update_my_profile();
provide_context(create_rw_signal(global_state));

View file

@ -1,12 +1,13 @@
use crate::{
common::{validation::can_edit_article, ArticleView, GetInstance},
frontend::{
api::CLIENT,
app::GlobalState,
article_link,
article_title,
components::instance_follow_button::InstanceFollowButton,
},
};use crate::frontend::api::CLIENT;
};
use leptos::*;
use leptos_router::*;

View file

@ -1,5 +1,6 @@
use crate::frontend::api::CLIENT;
use leptos::{component, *};
use url::Url;use crate::frontend::api::CLIENT;
use url::Url;
#[component]
pub fn ConnectView<T: Clone + 'static, R: 'static>(res: Resource<T, R>) -> impl IntoView {

View file

@ -1,7 +1,7 @@
use crate::{
common::{newtypes::InstanceId, DbInstance, FollowInstance},
frontend::app::GlobalState,
};use crate::frontend::api::CLIENT;
frontend::{api::CLIENT, app::GlobalState},
};
use leptos::{component, *};
#[component]
@ -11,10 +11,7 @@ pub fn InstanceFollowButton(instance: DbInstance) -> impl IntoView {
let instance_id = *instance_id;
async move {
let form = FollowInstance { id: instance_id };
CLIENT
.follow_instance(form)
.await
.unwrap();
CLIENT.follow_instance(form).await.unwrap();
GlobalState::update_my_profile();
}
});

View file

@ -1,6 +1,6 @@
use crate::frontend::{app::GlobalState, dark_mode::DarkMode};
use crate::frontend::{api::CLIENT, app::GlobalState, dark_mode::DarkMode};
use leptos::{component, use_context, view, IntoView, RwSignal, SignalWith, *};
use leptos_router::*;use crate::frontend::api::CLIENT;
use leptos_router::*;
#[component]
pub fn Nav() -> impl IntoView {
@ -21,12 +21,7 @@ pub fn Nav() -> impl IntoView {
);
let notification_count = create_resource(
|| (),
move |_| async move {
CLIENT
.notifications_count()
.await
.unwrap_or_default()
},
move |_| async move { CLIENT.notifications_count().await.unwrap_or_default() },
);
let (search_query, set_search_query) = create_signal(String::new());

View file

@ -1,13 +1,14 @@
use crate::{
common::{newtypes::ArticleId, ForkArticleForm, ProtectArticleForm},
frontend::{
api::CLIENT,
app::GlobalState,
article_link,
components::article_nav::{ActiveTab, ArticleNav},
pages::article_resource,
DbArticle,
},
};use crate::frontend::api::CLIENT;
};
use leptos::*;
use leptos_router::Redirect;

View file

@ -1,7 +1,7 @@
use crate::{
common::CreateArticleForm,
frontend::components::editor::EditorView,
};use crate::frontend::api::CLIENT;
frontend::{api::CLIENT, components::editor::EditorView},
};
use html::Textarea;
use leptos::*;
use leptos_router::Redirect;

View file

@ -1,13 +1,14 @@
use crate::{
common::{newtypes::ConflictId, ApiConflict, ArticleView, EditArticleForm, Notification},
frontend::{
api::CLIENT,
components::{
article_nav::{ActiveTab, ArticleNav},
editor::EditorView,
},
pages::article_resource,
},
};use crate::frontend::api::CLIENT;
};
use html::Textarea;
use leptos::*;
use leptos_router::use_params_map;
@ -89,9 +90,7 @@ pub fn EditArticle() -> impl IntoView {
resolve_conflict_id,
};
set_wait_for_response.update(|w| *w = true);
let res = CLIENT
.edit_article_with_conflict(&form)
.await;
let res = CLIENT.edit_article_with_conflict(&form).await;
set_wait_for_response.update(|w| *w = false);
match res {
Ok(Some(conflict)) => {

View file

@ -1,7 +1,7 @@
use crate::{
common::ListArticlesForm,
frontend::{article_link, article_title, components::connect::ConnectView},
};use crate::frontend::api::CLIENT;
frontend::{api::CLIENT, article_link, article_title, components::connect::ConnectView},
};
use html::Input;
use leptos::*;

View file

@ -1,11 +1,12 @@
use crate::{
common::{utils::http_protocol_str, DbInstance, ListArticlesForm},
frontend::{
api::CLIENT,
article_link,
article_title,
components::instance_follow_button::InstanceFollowButton,
},
};use crate::frontend::api::CLIENT;
};
use leptos::*;
use leptos_router::use_params_map;
use url::Url;
@ -16,10 +17,7 @@ pub fn InstanceDetails() -> impl IntoView {
let hostname = move || params.get().get("hostname").cloned().unwrap();
let instance_profile = create_resource(hostname, move |hostname| async move {
let url = Url::parse(&format!("{}://{hostname}", http_protocol_str())).unwrap();
CLIENT
.resolve_instance(url)
.await
.unwrap()
CLIENT.resolve_instance(url).await.unwrap()
});
view! {

View file

@ -1,5 +1,5 @@
use crate::frontend::components::connect::ConnectView;
use leptos::*;use crate::frontend::api::CLIENT;
use crate::frontend::{api::CLIENT, components::connect::ConnectView};
use leptos::*;
#[component]
pub fn ListInstances() -> impl IntoView {

View file

@ -1,8 +1,8 @@
use crate::{
common::LoginUserForm,
frontend::{app::GlobalState, components::credentials::*},
frontend::{api::CLIENT, app::GlobalState, components::credentials::*},
};
use leptos::*;use crate::frontend::api::CLIENT;
use leptos::*;
use leptos_router::Redirect;
#[component]

View file

@ -1,6 +1,9 @@
use crate::common::{ArticleView, GetArticleForm, MAIN_PAGE_NAME};
use crate::{
common::{ArticleView, GetArticleForm, MAIN_PAGE_NAME},
frontend::api::CLIENT,
};
use leptos::{create_resource, Resource, SignalGet};
use leptos_router::use_params_map;use crate::frontend::api::CLIENT;
use leptos_router::use_params_map;
pub(crate) mod article;
pub(crate) mod diff;

View file

@ -8,12 +8,7 @@ use leptos::*;
pub fn Notifications() -> impl IntoView {
let notifications = create_local_resource(
move || {},
|_| async move {
CLIENT
.notifications_list()
.await
.unwrap()
},
|_| async move { CLIENT.notifications_list().await.unwrap() },
);
view! {

View file

@ -1,8 +1,8 @@
use crate::{
common::{LocalUserView, RegisterUserForm},
frontend::{app::GlobalState, components::credentials::*, error::MyResult},
frontend::{api::CLIENT, app::GlobalState, components::credentials::*, error::MyResult},
};
use leptos::{logging::log, *};use crate::frontend::api::CLIENT;
use leptos::{logging::log, *};
#[component]
pub fn Register() -> impl IntoView {
@ -17,8 +17,7 @@ pub fn Register() -> impl IntoView {
log!("Try to register new account for {}", credentials.username);
async move {
set_wait_for_response.update(|w| *w = true);
let result: MyResult<LocalUserView> =
CLIENT.register(credentials).await;
let result: MyResult<LocalUserView> = CLIENT.register(credentials).await;
set_wait_for_response.update(|w| *w = false);
match result {
Ok(res) => {

View file

@ -1,11 +1,11 @@
use crate::{
common::{DbArticle, DbInstance, SearchArticleForm},
frontend::{article_link, article_title},
frontend::{api::CLIENT, article_link, article_title},
};
use leptos::*;
use leptos_router::use_query_map;
use serde::{Deserialize, Serialize};
use url::Url;use crate::frontend::api::CLIENT;
use url::Url;
#[derive(Default, Clone, Deserialize, Serialize, Debug)]
struct SearchResults {

View file

@ -1,9 +1,9 @@
use crate::{
common::{DbPerson, GetUserForm},
frontend::user_title,
frontend::{api::CLIENT, user_title},
};
use leptos::*;
use leptos_router::use_params_map;use crate::frontend::api::CLIENT;
use leptos_router::use_params_map;
#[component]
pub fn UserProfile() -> impl IntoView {