Add missing gree endpoints

This commit is contained in:
Ethan O'Brien
2026-07-26 23:22:23 -05:00
parent 352ff2dbc5
commit 2056511bd1
2 changed files with 51 additions and 3 deletions

View File

@@ -60,13 +60,28 @@ fn vacuum_database() {
DATABASE.lock_and_exec("VACUUM", params!()); DATABASE.lock_and_exec("VACUUM", params!());
} }
pub fn get_user_cert(uuid: &str) -> Option<(i64, String)> {
let user_id = DATABASE.lock_and_select("SELECT user_id FROM users WHERE uuid=?1;", params!(uuid)).ok()?.parse::<i64>().ok()?;
let cert = DATABASE.lock_and_select("SELECT cert FROM users WHERE uuid=?1;", params!(uuid)).ok()?;
Some((user_id, cert))
}
pub fn is_registered(uuid: &str) -> bool { pub fn is_registered(uuid: &str) -> bool {
match DATABASE.lock_and_select("SELECT cert FROM users WHERE uuid=?1;", params!(uuid)) { match get_user_cert(uuid) {
Ok(cert) => cert != "none", Some((_, cert)) => pem::parse(&cert).is_ok(),
Err(_) => false None => false
} }
} }
pub fn verify_fingerprint(uuid: &str, fingerprint: &str, cert: &str) -> bool {
let Ok(signature) = general_purpose::STANDARD.decode(fingerprint) else {
return false;
};
verify_signature(&signature, uuid.as_bytes(), cert)
}
fn verify_signature(signature: &[u8], message: &[u8], public_key: &str) -> bool { fn verify_signature(signature: &[u8], message: &[u8], public_key: &str) -> bool {
let Ok(pem) = pem::parse(public_key) else { let Ok(pem) = pem::parse(public_key) else {
return false; return false;

View File

@@ -16,6 +16,7 @@ use crate::database::gree::*;
const APP_ID: &str = "232610769078541"; const APP_ID: &str = "232610769078541";
const SRC_APP_ID: &str = "100900301"; const SRC_APP_ID: &str = "100900301";
const HMAC_SECRET_HEX: &str = "6438663638653238346566646636306262616563326432323563306366643432"; const HMAC_SECRET_HEX: &str = "6438663638653238346566646636306262616563326432323563306366643432";
const ERROR_INVALID_SIGNATURE: i32 = 4;
const ERROR_MIGRATED_DEVICE: i32 = 20; const ERROR_MIGRATED_DEVICE: i32 = 20;
struct RequireGreeAuth; struct RequireGreeAuth;
@@ -52,6 +53,8 @@ pub fn routes(cfg: &mut web::ServiceConfig) {
.service( .service(
web::scope("/migration") web::scope("/migration")
.route("", web::post().to(migration)) .route("", web::post().to(migration))
.route("/auth/initialize", web::post().to(initialize))
.route("/user/token", web::post().to(migration_user_token))
.route("/code", web::get().to(migration_code)) .route("/code", web::get().to(migration_code))
.route("/code/verify", web::post().to(migration_verify)) .route("/code/verify", web::post().to(migration_verify))
.route("/password/register", web::post().to(migration_password_register)) .route("/password/register", web::post().to(migration_password_register))
@@ -234,6 +237,36 @@ async fn migration(req: HttpRequest, body: String) -> impl Responder {
send(req, resp) send(req, resp)
} }
async fn migration_user_token(req: HttpRequest, body: String) -> impl Responder {
let body = jzon::parse(&body).unwrap_or(object!{});
let uuid = body["uuid"].to_string();
let resp = if let Some((user_id, cert)) = get_user_cert(&uuid) {
if verify_fingerprint(&uuid, &body["fingerprint"].to_string(), &cert) {
update_cert(user_id, &body["token"].to_string());
object!{
result: "OK"
}
} else {
println!("Rejecting device token update for {}: bad fingerprint", uuid);
object!{
result: "NG",
code: ERROR_INVALID_SIGNATURE,
message: "Invalid fingerprint"
}
}
} else {
println!("Rejecting device token update for unregistered device {}", uuid);
object!{
result: "NG",
code: ERROR_MIGRATED_DEVICE,
message: "Device is not registered"
}
};
send(req, resp)
}
async fn balance(req: HttpRequest) -> impl Responder { async fn balance(req: HttpRequest) -> impl Responder {
let uid = get_uid(&req); let uid = get_uid(&req);