Skip to content

Commit

Permalink
fix(ui): fix tempporary redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 committed Nov 26, 2023
1 parent 057c3e5 commit 584415d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
17 changes: 9 additions & 8 deletions openai/src/serve/route/ui/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use axum_extra::extract::CookieJar;
use base64::Engine;
use serde::{Deserialize, Serialize};

use crate::error;
use crate::token::TokenProfile;
use crate::{
auth::API_AUTH_SESSION_COOKIE_KEY,
serve::{error::ResponseError, route::ui::LOGIN_INDEX, route::ui::SESSION_ID},
token::model::Token,
};

use super::LOGOUT_INDEX;

#[derive(Serialize, Deserialize)]
pub(super) struct Session {
pub access_token: String,
Expand Down Expand Up @@ -107,14 +110,12 @@ where

fn extract_session(cookie_value: &str) -> Result<Session, ResponseError> {
Session::from_str(cookie_value)
.map_err(|_| ResponseError::Unauthorized(anyhow::anyhow!("invalid session")))
.and_then(|session| match check_token(&session.access_token) {
.map_err(|_| ResponseError::TempporaryRedirect(LOGIN_INDEX))
.and_then(|session| match crate::token::check(&session.access_token) {
Ok(_) => Ok(session),
Err(err) => Err(err),
Err(err) => {
error!("Session token is invalid: {}", err);
Err(ResponseError::TempporaryRedirect(LOGOUT_INDEX))
}
})
}

fn check_token(token: &str) -> Result<(), ResponseError> {
let _ = crate::token::check(token).map_err(ResponseError::Unauthorized)?;
Ok(())
}
1 change: 1 addition & 0 deletions openai/src/serve/route/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use super::get_static_resource;

const DEFAULT_INDEX: &str = "/";
const LOGIN_INDEX: &str = "/auth/login";
const LOGOUT_INDEX: &str = "/auth/logout";
const SESSION_ID: &str = "ninja_session";
const PUID_ID: &str = "_puid";
const BUILD_ID: &str = "eFlZtDCQUjuHAccnRY3au";
Expand Down

0 comments on commit 584415d

Please sign in to comment.