Skip to content

Commit

Permalink
Check registration flow
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed Apr 11, 2024
1 parent bee9c7c commit 253d164
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) trait DBConnection {
fn set_invoice_state(&self, invoice: Invoice, s: i32) -> anyhow::Result<()>;
fn get_user_by_name(&self, name: String) -> anyhow::Result<Option<AppUser>>;
fn get_user_by_id(&self, id: i32) -> anyhow::Result<Option<AppUser>>;
fn get_user_by_pubkey(&self, pubkey: String) -> anyhow::Result<Option<AppUser>>;
fn get_user_and_increment_counter(&self, name: &str) -> anyhow::Result<Option<AppUser>>;
fn insert_new_zap(&self, new_zap: Zap) -> anyhow::Result<Zap>;
fn get_zap_by_id(&self, id: i32) -> anyhow::Result<Option<Zap>>;
Expand Down Expand Up @@ -71,6 +72,11 @@ impl DBConnection for PostgresConnection {
AppUser::get_by_name(conn, name)
}

fn get_user_by_pubkey(&self, pubkey: String) -> anyhow::Result<Option<AppUser>> {
let conn = &mut self.db.get()?;
AppUser::get_by_pubkey(conn, pubkey)
}

fn get_user_by_id(&self, id: i32) -> anyhow::Result<Option<AppUser>> {
let conn = &mut self.db.get()?;
AppUser::get_by_id(conn, id)
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use crate::{
invoice::handle_pending_invoices,
mint::{setup_multimint, MultiMintWrapperTrait},
routes::{
check_pubkey, check_username, health_check, lnurl_callback_route, lnurl_verify_route,
register_route, root, validate_cors, well_known_lnurlp_route, well_known_nip5_route,
check_pubkey, check_registration_info, check_username, health_check, lnurl_callback_route,
lnurl_verify_route, register_route, root, validate_cors, well_known_lnurlp_route,
well_known_nip5_route,
},
};

Expand Down Expand Up @@ -168,7 +169,8 @@ async fn main() -> anyhow::Result<()> {
.route("/", get(root))
.route("/health-check", get(health_check))
.route("/v1/check-username/:username", get(check_username))
.route("/v1/check-pubkey/:pubkey", get(check_pubkey))
.route("/v1/check-pubkey/:pubkey", get(check_pubkey)) // DEPRECATED for check-registration
.route("/v1/check-registration", post(check_registration_info))
.route("/v1/register", post(register_route))
.route("/.well-known/nostr.json", get(well_known_nip5_route))
.route(
Expand Down
6 changes: 5 additions & 1 deletion src/register.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use crate::{
models::app_user::NewAppUser,
models::app_user::{AppUser, NewAppUser},
routes::{RegisterRequest, RegisterResponse},
State,
};
Expand Down Expand Up @@ -35,6 +35,10 @@ pub fn check_registered_pubkey(state: &State, pubkey: String) -> anyhow::Result<
state.db.check_registered_pubkey(pubkey)
}

pub fn get_user_by_pubkey(state: &State, pubkey: String) -> anyhow::Result<Option<AppUser>> {
state.db.get_user_by_pubkey(pubkey)
}

pub fn generate_random_name(state: &State) -> anyhow::Result<String> {
loop {
let new_name = Generator::with_naming(names::Name::Numbered)
Expand Down
60 changes: 58 additions & 2 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
lnurlp::{lnurl_callback, verify, well_known_lnurlp},
nostr::well_known_nip5,
register::{check_available, check_registered_pubkey, register},
register::{check_available, check_registered_pubkey, get_user_by_pubkey, register},
State, ALLOWED_LOCALHOST, ALLOWED_ORIGINS, ALLOWED_SUBDOMAIN, API_VERSION,
};
use axum::extract::{Path, Query};
Expand All @@ -10,15 +10,18 @@ use axum::http::StatusCode;
use axum::response::{IntoResponse, Redirect, Response};
use axum::Extension;
use axum::{Json, TypedHeader};
use fedimint_core::Amount;
use fedimint_core::{config::FederationId, Amount};
use fedimint_ln_common::lightning_invoice::Bolt11Invoice;
use log::{error, info};
use nostr::{Event, Kind};
use serde::{de, Deserialize, Deserializer, Serialize};
use serde_json::{json, Value};
use std::{collections::HashMap, fmt::Display, str::FromStr};
use tbs::AggregatePublicKey;
use url::Url;

const REGISTRATION_CHECK_EVENT_KIND: Kind = Kind::Custom(93_186);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LnUrlErrorResponse {
pub status: LnurlStatus,
Expand Down Expand Up @@ -70,6 +73,59 @@ pub async fn check_pubkey(
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct RegistrationInfo {
pub name: Option<String>,
pub federation_id: Option<FederationId>,
}

pub async fn check_registration_info(
origin: Option<TypedHeader<Origin>>,
Extension(state): Extension<State>,
Json(event): Json<Event>,
) -> Result<Json<RegistrationInfo>, (StatusCode, String)> {
validate_cors(origin)?;

let pubkey = event.author();
info!("check_registration_info: {}", pubkey);

if event.verify().is_err() && event.kind() != REGISTRATION_CHECK_EVENT_KIND {
error!("error in check_registration_info: bad event");
return Err((StatusCode::BAD_REQUEST, "Bad event".to_string()));
}

// make sure it was made in the last 30 seconds
let created_at = event.created_at();
let now = nostr::Timestamp::now();
if created_at < now - 120_i64 && created_at > now + 120_i64 {
error!("error in check_registration_info: event time not in range");
return Err((
StatusCode::BAD_REQUEST,
"Event time not in range".to_string(),
));
}

match get_user_by_pubkey(&state, pubkey.to_string()) {
Ok(Some(u)) => {
info!("check_pubkey finished: {}", pubkey);

Ok(Json(RegistrationInfo {
name: Some(u.name),
federation_id: Some(u.federation_id),
}))
}
Ok(None) => {
info!("check_pubkey not found: {}", pubkey);

Ok(Json(RegistrationInfo {
name: None,
federation_id: None,
}))
}
Err(e) => Err(handle_anyhow_error("check_pubkey", e)),
}
}

#[derive(Deserialize, Clone)]
pub struct RegisterRequest {
pub name: Option<String>,
Expand Down

0 comments on commit 253d164

Please sign in to comment.