serial number and cert hash route
All checks were successful
Build and Test / account (push) Successful in 3m16s

This commit is contained in:
red binder 2026-07-16 02:06:51 +02:00
commit 9bf06fffd6
12 changed files with 122 additions and 59 deletions

View file

@ -1,11 +1,7 @@
use rocket::serde::json::Json;
use rocket::{post, FromForm, State};
use rocket::form::Form;
use rocket::futures::TryFutureExt;
use rocket::http::Status;
use crate::account::account::Auth;
use crate::json_api::oauth::generate_token::TokenRequest;
use crate::nnid::people::{build_oauth_profile, GetOwnOAuthProfileData};
use crate::Pool;
#[derive(FromForm)]
@ -21,7 +17,7 @@ pub async fn ban_user(pool: &State<Pool>, auth: Auth<true>, request: Form<AdminR
log::info!("banning user {:?} from moderator {:?}", request.username, auth.username);
let row = sqlx::query!(
let _row = sqlx::query!(
"UPDATE users SET account_level = $1 WHERE username = $2",
-1,
request.username

View file

@ -0,0 +1,40 @@
use rocket::serde::json::Json;
use rocket::{get, State};
use rocket::http::Status;
use crate::account::account::Auth;
use serde::Serialize;
use crate::Pool;
#[derive(Serialize)]
pub struct ConsoleData {
pub serial: String,
pub cert_hash: String,
}
#[get("/api/v2/users/@me/console")]
pub async fn get_own_console(pool: &State<Pool>, auth: Auth<true>) -> Result<Json<ConsoleData>, (Status, &'static str)> {
let hash = auth.1.ok_or((Status::BadRequest, "token needs cert hash"))?;
let row = sqlx::query!(
"select serial from certificates where hash = $1",
&hash[..]
)
.fetch_one(pool.inner())
.await
.map_err(|e| {
log::error!("failed to execute query: {:?}", e);
(Status::InternalServerError, "error in database query")
})?;
let hex_hash = hex::encode(hash);
let serial = row.serial.ok_or((Status::BadRequest, "we had no serial"))?;
Ok(
Json (
ConsoleData {
serial,
cert_hash: hex_hash
}
)
)
}

View file

@ -1,3 +1,4 @@
pub mod profile;
pub mod mii;
pub mod delete;
pub mod delete;
pub mod console;

View file

@ -119,6 +119,7 @@ async fn launch() -> _ {
json_api::oauth::authorize::authorize_page,
json_api::oauth::authorize::authorize_submit,
json_api::admin::bans::ban_user,
json_api::users::console::get_own_console,
nnid::people::thing,
// graphql::graphiql,
// graphql::playground,

View file

@ -6,7 +6,6 @@ use crate::nnid::oauth::generate_token::token_type::NEX_TOKEN;
use crate::xml::Xml;
use log::{info, warn};
use nex_account::grpc::Pid;
use reqwest::header::SERVER;
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome, Request};
use rocket::{State, async_trait, get};
@ -74,18 +73,20 @@ pub async fn store_or_check_serial(pool: &Pool, serial: &str, cert_hash: [u8; 32
};
let Some(stored_serial) = res.serial else {
query!(
let _row = query!(
"update certificates set serial = $1 where hash = $2",
serial,
&cert_hash[..]
);
).execute(pool)
.await
.ok();
return true;
};
serial == stored_serial
}
struct Serial(String);
pub struct Serial(pub String);
#[async_trait]
impl<'r> FromRequest<'r> for Serial {