rust
Elnu 1 year ago
parent 94bd2da27b
commit a282d06bc5

@ -7,4 +7,4 @@ pub mod user {
pub const USER_NAME_COOKIE: &str = "user_name"; pub const USER_NAME_COOKIE: &str = "user_name";
pub const USER_DISCRIMINATOR_COOKIE: &str = "user_discriminator"; pub const USER_DISCRIMINATOR_COOKIE: &str = "user_discriminator";
pub const USER_AVATAR_COOKIE: &str = "user_avatar"; pub const USER_AVATAR_COOKIE: &str = "user_avatar";
} }

@ -25,9 +25,11 @@ async fn rocket() -> _ {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let http = Http::new(&token); let http = Http::new(&token);
let config = rocket::Config::figment() let config = rocket::Config::figment().merge(("port", 1313)).merge((
.merge(("port", 1313)) "secret_key",
.merge(("secret_key", env::var("SECRET").expect("Must have SECRET defined in .env for private cookie persistence"))); env::var("SECRET")
.expect("Must have SECRET defined in .env for private cookie persistence"),
));
rocket::custom(config) rocket::custom(config)
.manage(http) .manage(http)
.mount( .mount(

@ -31,7 +31,8 @@ impl Challenge {
let arena = Arena::new(); let arena = Arena::new();
let root = parse_document( let root = parse_document(
&arena, &arena,
&fs::read_to_string(format!("content/challenges/{number}.md")).expect("Couldn't find challenge file"), &fs::read_to_string(format!("content/challenges/{number}.md"))
.expect("Couldn't find challenge file"),
&options, &options,
); );
if let Some(node) = root.children().next() { if let Some(node) = root.children().next() {
@ -48,7 +49,7 @@ impl Challenge {
} }
} else { } else {
panic!("Empty document!") panic!("Empty document!")
} }
} }
} }

@ -2,4 +2,4 @@ mod user;
pub use user::User; pub use user::User;
mod challenge; mod challenge;
pub use challenge::Challenge; pub use challenge::Challenge;

@ -6,7 +6,7 @@ mod tests;
use chrono::Utc; use chrono::Utc;
use derive_more::From; use derive_more::From;
use reqwest::StatusCode; use reqwest::StatusCode;
use rocket::http::{CookieJar, Cookie}; use rocket::http::{Cookie, CookieJar};
use serial::*; use serial::*;
use serde::Deserialize; use serde::Deserialize;
@ -113,4 +113,4 @@ pub enum GetUserError {
fn parse_cookie_value<T: std::str::FromStr>(cookies: &CookieJar<'_>, name: &str) -> Option<T> { fn parse_cookie_value<T: std::str::FromStr>(cookies: &CookieJar<'_>, name: &str) -> Option<T> {
cookies.get_private(name)?.value().parse().ok() cookies.get_private(name)?.value().parse().ok()
} }

@ -1,4 +1,4 @@
use serde::{Serialize, Serializer, ser::SerializeStruct}; use serde::{ser::SerializeStruct, Serialize, Serializer};
use super::User; use super::User;
@ -31,4 +31,4 @@ impl Serialize for User {
state.serialize_field("username", &self.username())?; state.serialize_field("username", &self.username())?;
state.end() state.end()
} }
} }

@ -18,4 +18,4 @@ fn test_legacy_username() {
fn test_new_username() { fn test_new_username() {
let user = test_user("test", 0); let user = test_user("test", 0);
assert_eq!(user.username(), "test"); assert_eq!(user.username(), "test");
} }

@ -1 +1 @@
pub use crate::utils::Kyujitai; pub use crate::utils::Kyujitai;

@ -1,7 +1,7 @@
use rocket::http::CookieJar; use rocket::http::CookieJar;
use rocket_dyn_templates::{Template, context}; use rocket_dyn_templates::{context, Template};
use crate::models::{User, Challenge}; use crate::models::{Challenge, User};
#[get("/<challenge>")] #[get("/<challenge>")]
pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template { pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template {
@ -19,4 +19,4 @@ pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template
content: Challenge::get(challenge), content: Challenge::get(challenge),
}, },
) )
} }

@ -10,4 +10,4 @@ pub fn login() -> Redirect {
client_id = env::var("CLIENT_ID").unwrap(), client_id = env::var("CLIENT_ID").unwrap(),
redirect_uri = format!("{}success", env::var("DOMAIN").unwrap()), redirect_uri = format!("{}success", env::var("DOMAIN").unwrap()),
)) ))
} }

@ -2,7 +2,7 @@ use std::{collections::HashMap, env};
use rocket::{http::CookieJar, response::Redirect}; use rocket::{http::CookieJar, response::Redirect};
use crate::{utils::Referer, cookies::token::TOKEN_COOKIE, models::User}; use crate::{cookies::token::TOKEN_COOKIE, models::User, utils::Referer};
#[get("/logout")] #[get("/logout")]
pub fn logout(cookies: &CookieJar<'_>, referer: Referer) -> Redirect { pub fn logout(cookies: &CookieJar<'_>, referer: Referer) -> Redirect {
@ -33,4 +33,4 @@ pub fn logout(cookies: &CookieJar<'_>, referer: Referer) -> Redirect {
User::purge(cookies); User::purge(cookies);
let redirect_url = referer.0.unwrap_or("/".to_owned()); let redirect_url = referer.0.unwrap_or("/".to_owned());
Redirect::to(redirect_url) Redirect::to(redirect_url)
} }

@ -20,4 +20,4 @@ pub use _success::success;
#[path = "testing.rs"] #[path = "testing.rs"]
mod _testing; mod _testing;
pub use _testing::testing; pub use _testing::testing;

@ -1,5 +1,9 @@
use chrono::{Utc, Duration}; use chrono::{Duration, Utc};
use rocket::{form::Form, http::{CookieJar, Cookie}, response::Redirect}; use rocket::{
form::Form,
http::{Cookie, CookieJar},
response::Redirect,
};
use crate::{cookies::token::TOKEN_EXPIRE_COOKIE, models::User}; use crate::{cookies::token::TOKEN_EXPIRE_COOKIE, models::User};
@ -24,4 +28,4 @@ pub async fn post_login(login: Form<Login<'_>>, cookies: &CookieJar<'_>) -> Redi
)); ));
} }
Redirect::to("/") Redirect::to("/")
} }

@ -21,4 +21,4 @@ pub fn success() -> RawHtml<&'static str> {
} }
</script>", </script>",
) )
} }

@ -8,13 +8,21 @@ use crate::models::User;
#[get("/testing")] #[get("/testing")]
pub async fn testing(cookies: &CookieJar<'_>, http: &State<Http>) -> String { pub async fn testing(cookies: &CookieJar<'_>, http: &State<Http>) -> String {
// Get logged in user's join date in 字ちゃん server // Get logged in user's join date in 字ちゃん server
format!("{:?}", http.get_guild(814700630958276649).await format!(
.expect("Failed to get testing guild") "{:?}",
.member(http.deref(), User::get(cookies).await http.get_guild(814700630958276649)
.expect("Failed to get logged in user data") .await
.expect("No logged in user") .expect("Failed to get testing guild")
.id .member(
).await http.deref(),
.expect("Failed to fetch user in server") User::get(cookies)
.joined_at) .await
} .expect("Failed to get logged in user data")
.expect("No logged in user")
.id
)
.await
.expect("Failed to fetch user in server")
.joined_at
)
}

@ -1,6 +1,9 @@
use std::convert::Infallible; use std::convert::Infallible;
use rocket::{request::{FromRequest, self}, Request}; use rocket::{
request::{self, FromRequest},
Request,
};
pub struct Referer(pub Option<String>); pub struct Referer(pub Option<String>);
@ -12,4 +15,4 @@ impl<'r> FromRequest<'r> for Referer {
let referer = req.headers().get_one("Referer"); let referer = req.headers().get_one("Referer");
request::Outcome::Success(Referer(referer.map(|referer| referer.to_owned()))) request::Outcome::Success(Referer(referer.map(|referer| referer.to_owned())))
} }
} }

Loading…
Cancel
Save