Skip to content

Commit

Permalink
feat(backend): allow setting insecure cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed May 13, 2023
1 parent 61d2297 commit 7333483
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ builder.
| `video_games.igdb.image_url` | The url for getting images from IGDB. |
| `video_games.igdb.image_size` | The image sizes to fetch from IGDB. |
| `web.cors_origins` | An array of URLs for CORS. |
| `web.insecure_cookie` | Setting this to `true` will make auth cookies insecure. [More information](https://github.com/IgnisDa/ryot/issues/23#) |

## 🤓 Developer notes

Expand Down
12 changes: 11 additions & 1 deletion apps/backend/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,19 @@ impl IsFeatureEnabled for VideoGameConfig {
#[derive(Deserialize, Debug, Clone, Serialize, Default)]
pub struct SchedulerConfig {}

#[derive(Deserialize, Debug, Clone, Serialize, Default)]
#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct WebConfig {
pub cors_origins: Vec<String>,
pub insecure_cookie: bool,
}

impl Default for WebConfig {
fn default() -> Self {
Self {
cors_origins: vec![],
insecure_cookie: false,
}
}
}

#[derive(Deserialize, Debug, Clone, Serialize, Default)]
Expand Down
39 changes: 24 additions & 15 deletions apps/backend/src/users/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use uuid::Uuid;

use crate::{
background::UserCreatedJob,
config::AppConfig,
entities::{
audio_book, book, movie,
prelude::{AudioBook, Book, Metadata, Movie, Seen, Show, Summary, Token, User, VideoGame},
Expand Down Expand Up @@ -105,8 +106,13 @@ struct UpdateUserInput {
password: Option<String>,
}

fn create_cookie(ctx: &Context<'_>, api_key: &str, expires: bool) -> Result<()> {
let mut cookie = Cookie::build(COOKIE_NAME, api_key.to_string()).secure(true);
fn create_cookie(
ctx: &Context<'_>,
api_key: &str,
expires: bool,
insecure_cookie: bool,
) -> Result<()> {
let mut cookie = Cookie::build(COOKIE_NAME, api_key.to_string()).secure(!insecure_cookie);
if expires {
cookie = cookie.expires(OffsetDateTime::now_utc());
} else {
Expand Down Expand Up @@ -208,15 +214,17 @@ impl UsersMutation {
.data_unchecked::<UsersService>()
.login_user(&input.username, &input.password)
.await?;
let cookie_insecure = gql_ctx.data_unchecked::<AppConfig>().web.insecure_cookie;
if let LoginResult::Ok(LoginResponse { api_key }) = api_key {
create_cookie(gql_ctx, &api_key.to_string(), false)?;
create_cookie(gql_ctx, &api_key.to_string(), false, cookie_insecure)?;
};
Ok(api_key)
}

/// Logout a user from the server, deleting their login token
async fn logout_user(&self, gql_ctx: &Context<'_>) -> Result<bool> {
create_cookie(gql_ctx, "", true)?;
let cookie_insecure = gql_ctx.data_unchecked::<AppConfig>().web.insecure_cookie;
create_cookie(gql_ctx, "", true, cookie_insecure)?;
let user_id = user_auth_token_from_ctx(gql_ctx)?;
gql_ctx
.data_unchecked::<UsersService>()
Expand Down Expand Up @@ -330,6 +338,17 @@ impl UsersService {
for (seen, metadata) in seen_items.iter() {
let meta = metadata.to_owned().unwrap();
match meta.lot {
MetadataLot::AudioBook => {
let item = meta
.find_related(AudioBook)
.one(&self.db)
.await
.unwrap()
.unwrap();
if let Some(r) = item.runtime {
audio_books_total.push(r);
}
}
MetadataLot::Book => {
let item = meta
.find_related(Book)
Expand All @@ -341,6 +360,7 @@ impl UsersService {
books_total.push(pg);
}
}
MetadataLot::Podcast => todo!(),
MetadataLot::Movie => {
let item = meta
.find_related(Movie)
Expand Down Expand Up @@ -376,17 +396,6 @@ impl UsersService {
}
}
}
MetadataLot::AudioBook => {
let item = meta
.find_related(AudioBook)
.one(&self.db)
.await
.unwrap()
.unwrap();
if let Some(r) = item.runtime {
audio_books_total.push(r);
}
}
MetadataLot::VideoGame => {
// nothing to calculate
continue;
Expand Down

0 comments on commit 7333483

Please sign in to comment.