Skip to content

Commit

Permalink
use header constants
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed May 3, 2024
1 parent b97122a commit b1a1f9e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/src/features/toolbar/hooks/useGithubAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function useGithubAuth(): [
AuthenticatedUser | null,
() => Promise<void>
] {
const [sessionId, setSessionId] = useCookie('session');
const [sessionId, setSessionId] = useCookie('fp_session');
const [githubUser, setGithubUser] = useState<AuthenticatedUser | null>(null);
const { githubCode, saveGithubCode, clearGithubCode } = useLocalSession();
const [searchParams, setSearchParams] = useSearchParams();
Expand Down
17 changes: 9 additions & 8 deletions src/middleware/cors.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::env;

use dotenvy::dotenv;
use regex::Regex;
use reqwest::header::ACCESS_CONTROL_ALLOW_ORIGIN;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::hyper::header;
use rocket::http::{Header, HeaderMap};
use rocket::{Request, Response};
use std::env;

// Build an open cors module so this server can be used accross many locations on the web.
pub struct Cors;
Expand Down Expand Up @@ -46,24 +46,25 @@ impl Fairing for Cors {
// Build an Access-Control-Allow-Origin policy Response header.
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
if let Some(origin) = get_allowed_origin(request.headers()) {
response.set_header(Header::new("Access-Control-Allow-Origin", origin));
response.set_header(Header::new(ACCESS_CONTROL_ALLOW_ORIGIN.as_str(), origin));
}
response.set_header(Header::new(
"Access-Control-Allow-Methods",
header::ACCESS_CONTROL_ALLOW_METHODS.as_str(),
"POST, PATCH, PUT, DELETE, HEAD, OPTIONS, GET",
));
response.set_header(Header::new(
"Access-Control-Allow-Headers",
header::ACCESS_CONTROL_ALLOW_HEADERS.as_str(),
"*, Access-Control-Request-Headers, Content-Type",
));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
response.set_header(Header::new(
header::ACCESS_CONTROL_ALLOW_CREDENTIALS.as_str(),
"true",
));
}
}

#[cfg(test)]
mod tests {
use rocket::http::hyper::header;

use super::*;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/session_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rocket::Request;
use std::time::SystemTime;
use uuid::Uuid;

pub const SESSION_COOKIE_NAME: &str = "session";
pub const SESSION_COOKIE_NAME: &str = "fp_session";

pub struct SessionAuth {
pub user: models::User,
Expand Down
5 changes: 2 additions & 3 deletions src/middleware/token_auth.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::db::api_token::PlainToken;
use crate::db::Database;
use crate::models;
use rocket::http::hyper::header;
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome};
use rocket::Request;

pub const SESSION_COOKIE_NAME: &str = "session";

pub struct TokenAuth {
pub token: models::ApiToken,
}
Expand Down Expand Up @@ -36,7 +35,7 @@ impl<'r> FromRequest<'r> for TokenAuth {
}
};

if let Some(auth_header) = request.headers().get_one("Authorization") {
if let Some(auth_header) = request.headers().get_one(header::AUTHORIZATION.as_str()) {
if auth_header.starts_with("Bearer ") {
let token = auth_header.trim_start_matches("Bearer ");
if let Ok(token) = db.get_token(PlainToken::from(token.to_string())) {
Expand Down

0 comments on commit b1a1f9e

Please sign in to comment.