refactor(webapi,types)!: make challenge type extensive

We may allow more challenge types other than PoW in the future, eg.
captcha. So make the relevent types more generic.

Now the challenge is returned in JSON response as a individual top-level
field `register_challenge` instead of in HTTP headers.
This commit is contained in:
oxalica 2024-10-01 05:26:00 -04:00
parent 364e517b7d
commit bc6e6c2056
11 changed files with 206 additions and 130 deletions

View file

@ -2,7 +2,7 @@
use std::hint::black_box;
use std::time::Instant;
use blah_types::msg::{ChatPayload, UserRegisterPayload};
use blah_types::msg::{ChatPayload, UserRegisterChallengeResponse, UserRegisterPayload};
use blah_types::{get_timestamp, Id, PubKey, SignExt, Signee, UserKey};
use criterion::{criterion_group, criterion_main, Criterion};
use ed25519_dalek::SigningKey;
@ -24,7 +24,7 @@ fn bench_register_pow(c: &mut Criterion) {
id_key: id_key.clone(),
server_url: "http://some.example.com".parse().unwrap(),
id_url: "http://another.example.com".parse().unwrap(),
challenge_nonce: rng.gen(),
challenge: Some(UserRegisterChallengeResponse::Pow { nonce: rng.gen() }),
};
let mut signee = Signee {
nonce: 0,

View file

@ -58,7 +58,17 @@ pub struct UserRegisterPayload {
pub server_url: Url,
pub id_url: IdUrl,
pub id_key: PubKey,
pub challenge_nonce: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub challenge: Option<UserRegisterChallengeResponse>,
}
/// The server-specific challenge data for registration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UserRegisterChallengeResponse {
/// Proof of work challenge containing the same nonce from server challenge request.
/// The whole msg signee hash should have enough prefix zero bits.
Pow { nonce: u32 },
}
// FIXME: `deny_unknown_fields` breaks this.

View file

@ -6,9 +6,6 @@ use url::Url;
use crate::msg::{Id, MemberPermission, RoomAttrs, SignedChatMsgWithId};
use crate::PubKey;
pub const X_BLAH_NONCE: &str = "x-blah-nonce";
pub const X_BLAH_DIFFICULTY: &str = "x-blah-difficulty";
/// Metadata about the version and capabilities of a Chat Server.
///
/// It should be relatively stable and do not change very often.
@ -37,6 +34,18 @@ pub struct ServerCapabilities {
pub allow_public_register: bool,
}
/// Registration challenge information.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UserRegisterChallenge {
/// Proof-of-work (PoW) challenge.
Pow { nonce: u32, difficulty: u8 },
/// A catch-all unknown challenge type.
#[serde(other, skip_serializing)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomMetadata {
/// Room id.