auth::create_new_session_key => impl not tested

auth::login now generates a 500 on db insertion failure
auth::login returning single json value instead of full struct
This commit is contained in:
shockrah 2020-05-19 20:13:56 -07:00
parent 87fc6700a4
commit 895d7246f0
2 changed files with 30 additions and 7 deletions

View File

@ -13,6 +13,8 @@ use rocket::response::{self, Responder, Response};
use rocket::request::{Form, Request}; use rocket::request::{Form, Request};
use rocket_contrib::json::{Json, JsonValue}; use rocket_contrib::json::{Json, JsonValue};
use diesel::{self, prelude::*}; use diesel::{self, prelude::*};
use chrono::{Duration, Utc};
use std::{error, fmt}; use std::{error, fmt};
#[allow(dead_code)] // added because these fields are read through rocket, not directly; and rls keeps complainin #[allow(dead_code)] // added because these fields are read through rocket, not directly; and rls keeps complainin
@ -117,9 +119,24 @@ fn blind_remove_session(conn: &MysqlConnection, sesh_secret: &str) {
.execute(conn); .execute(conn);
} }
fn create_new_session_key() -> String { fn create_new_session_key(conn: &MysqlConnection) -> Option<String> {
let key_raw = utils::new_key(); use crate::models::InsertableSession;
utils::encode_param(&key_raw)
let new_session = InsertableSession {
secret: utils::new_key(),
expires: (Utc::now() + Duration::hours(1)).timestamp() as u64
};
// insert the new key into our db
let db_result = diesel::insert_into(schema::sessions::table)
.values(&new_session)
.execute(conn);
// finally return the key assuming everything went well
match db_result {
Ok(_val) => Some(new_session.secret),
Err(_e) => None
}
} }
#[post("/login", data = "<api_key>")] #[post("/login", data = "<api_key>")]
@ -131,8 +148,14 @@ pub fn login(conn: DBConn, api_key: Form<AuthKey>) -> AuthResult<JsonValue, Auth
if confirm_user_api_access(&conn.0, api_key.id, &api_key.secret) { if confirm_user_api_access(&conn.0, api_key.id, &api_key.secret) {
blind_remove_session(&conn.0, &api_key.secret); blind_remove_session(&conn.0, &api_key.secret);
let key = create_new_session_key(); let key = create_new_session_key(&conn.0);
Ok(json!({"key": key})) match key {
Some(data) => Ok(json!({"key": data})),
None => Err(AuthErr {
msg: "Could not create session",
status: 500
})
}
} }
else { else {
Err(AuthErr { Err(AuthErr {
@ -178,6 +201,7 @@ mod auth_tests {
Err(e) => panic!("`.env` could not be loaded: {:?}", e) Err(e) => panic!("`.env` could not be loaded: {:?}", e)
} }
} }
#[test] #[test]
fn feed_n_leave() { fn feed_n_leave() {
// Create an invite in our db manually // Create an invite in our db manually

View File

@ -18,8 +18,7 @@ table! {
} }
table! { table! {
sessions (id) { sessions (secret) {
id -> Unsigned<Bigint>,
secret -> Varchar, secret -> Varchar,
expires -> Unsigned<Bigint>, expires -> Unsigned<Bigint>,
} }