|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use axum::extract::{FromRequestParts, State}; |
| 4 | +use axum::http::request::Parts; |
| 5 | +use axum::http::StatusCode; |
| 6 | +use axum::response::Response; |
| 7 | +use axum::routing::{get, post}; |
| 8 | +use axum::{async_trait, Json, Router}; |
| 9 | +use cdk::error::{ErrorCode, ErrorResponse}; |
| 10 | +use cdk::nuts::nutxx1::MintAuthRequest; |
| 11 | +use cdk::nuts::{AuthToken, BlindAuthToken, KeysResponse, KeysetResponse, MintBolt11Response}; |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | + |
| 14 | +use crate::{get_keyset_pubkeys, into_response, MintState}; |
| 15 | + |
| 16 | +const CLEAR_AUTH_KEY: &str = "Clear-auth"; |
| 17 | +const BLIND_AUTH_KEY: &str = "Blind-auth"; |
| 18 | + |
| 19 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 20 | +pub enum AuthHeader { |
| 21 | + /// Clear Auth token |
| 22 | + Clear(String), |
| 23 | + /// Blind Auth token |
| 24 | + Blind(BlindAuthToken), |
| 25 | + /// No auth |
| 26 | + None, |
| 27 | +} |
| 28 | + |
| 29 | +impl From<AuthHeader> for Option<AuthToken> { |
| 30 | + fn from(value: AuthHeader) -> Option<AuthToken> { |
| 31 | + match value { |
| 32 | + AuthHeader::Clear(token) => Some(AuthToken::ClearAuth(token)), |
| 33 | + AuthHeader::Blind(token) => Some(AuthToken::BlindAuth(token)), |
| 34 | + AuthHeader::None => None, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[async_trait] |
| 40 | +impl<S> FromRequestParts<S> for AuthHeader |
| 41 | +where |
| 42 | + S: Send + Sync, |
| 43 | +{ |
| 44 | + type Rejection = (StatusCode, String); |
| 45 | + |
| 46 | + async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { |
| 47 | + // Check for Blind-auth header |
| 48 | + if let Some(bat) = parts.headers.get(BLIND_AUTH_KEY) { |
| 49 | + let token = bat |
| 50 | + .to_str() |
| 51 | + .map_err(|_| { |
| 52 | + ( |
| 53 | + StatusCode::BAD_REQUEST, |
| 54 | + "Invalid Blind-auth header value".to_string(), |
| 55 | + ) |
| 56 | + })? |
| 57 | + .to_string(); |
| 58 | + |
| 59 | + let token = BlindAuthToken::from_str(&token).map_err(|_| { |
| 60 | + ( |
| 61 | + StatusCode::BAD_REQUEST, |
| 62 | + "Invalid Blind-auth header value".to_string(), |
| 63 | + ) |
| 64 | + })?; |
| 65 | + |
| 66 | + return Ok(AuthHeader::Blind(token)); |
| 67 | + } |
| 68 | + |
| 69 | + // Check for Clear-auth header |
| 70 | + if let Some(cat) = parts.headers.get(CLEAR_AUTH_KEY) { |
| 71 | + let token = cat |
| 72 | + .to_str() |
| 73 | + .map_err(|_| { |
| 74 | + ( |
| 75 | + StatusCode::BAD_REQUEST, |
| 76 | + "Invalid Clear-auth header value".to_string(), |
| 77 | + ) |
| 78 | + })? |
| 79 | + .to_string(); |
| 80 | + return Ok(AuthHeader::Clear(token)); |
| 81 | + } |
| 82 | + |
| 83 | + // No authentication headers found - this is now valid |
| 84 | + Ok(AuthHeader::None) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg_attr(feature = "swagger", utoipa::path( |
| 89 | + get, |
| 90 | + context_path = "/v1/auth/blind", |
| 91 | + path = "/keysets", |
| 92 | + responses( |
| 93 | + (status = 200, description = "Successful response", body = KeysetResponse, content_type = "application/json"), |
| 94 | + (status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json") |
| 95 | + ) |
| 96 | +))] |
| 97 | +/// Get all active keyset IDs of the mint |
| 98 | +/// |
| 99 | +/// This endpoint returns a list of keysets that the mint currently supports and will accept tokens from. |
| 100 | +pub async fn get_auth_keysets( |
| 101 | + State(state): State<MintState>, |
| 102 | +) -> Result<Json<KeysetResponse>, Response> { |
| 103 | + let keysets = state.mint.auth_keysets().await.map_err(|err| { |
| 104 | + tracing::error!("Could not get keysets: {}", err); |
| 105 | + into_response(err) |
| 106 | + })?; |
| 107 | + |
| 108 | + Ok(Json(keysets)) |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg_attr(feature = "swagger", utoipa::path( |
| 112 | + get, |
| 113 | + context_path = "/v1/auth/blind", |
| 114 | + path = "/keys", |
| 115 | + responses( |
| 116 | + (status = 200, description = "Successful response", body = KeysResponse, content_type = "application/json") |
| 117 | + ) |
| 118 | +))] |
| 119 | +/// Get the public keys of the newest blind auth mint keyset |
| 120 | +/// |
| 121 | +/// This endpoint returns a dictionary of all supported token values of the mint and their associated public key. |
| 122 | +pub async fn get_blind_auth_keys( |
| 123 | + State(state): State<MintState>, |
| 124 | +) -> Result<Json<KeysResponse>, Response> { |
| 125 | + let pubkeys = state.mint.auth_pubkeys().await.map_err(|err| { |
| 126 | + tracing::error!("Could not get keys: {}", err); |
| 127 | + into_response(err) |
| 128 | + })?; |
| 129 | + |
| 130 | + Ok(Json(pubkeys)) |
| 131 | +} |
| 132 | + |
| 133 | +/// Mint tokens by paying a BOLT11 Lightning invoice. |
| 134 | +/// |
| 135 | +/// Requests the minting of tokens belonging to a paid payment request. |
| 136 | +/// |
| 137 | +/// Call this endpoint after `POST /v1/mint/quote`. |
| 138 | +#[cfg_attr(feature = "swagger", utoipa::path( |
| 139 | + post, |
| 140 | + context_path = "/v1/auth", |
| 141 | + path = "/blind/mint", |
| 142 | + request_body(content = MintAuthRequest, description = "Request params", content_type = "application/json"), |
| 143 | + responses( |
| 144 | + (status = 200, description = "Successful response", body = MintBolt11Response, content_type = "application/json"), |
| 145 | + (status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json") |
| 146 | + ) |
| 147 | +))] |
| 148 | +pub async fn post_mint_auth( |
| 149 | + auth: AuthHeader, |
| 150 | + State(state): State<MintState>, |
| 151 | + Json(payload): Json<MintAuthRequest>, |
| 152 | +) -> Result<Json<MintBolt11Response>, Response> { |
| 153 | + let auth_token = match auth { |
| 154 | + AuthHeader::Clear(cat) => AuthToken::ClearAuth(cat), |
| 155 | + _ => { |
| 156 | + return Err(into_response(ErrorResponse::new( |
| 157 | + ErrorCode::from_code(0), |
| 158 | + None, |
| 159 | + None, |
| 160 | + ))) |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + let res = state |
| 165 | + .mint |
| 166 | + .mint_blind_auth(auth_token, payload) |
| 167 | + .await |
| 168 | + .map_err(|err| { |
| 169 | + tracing::error!("Could not process blind auth mint: {}", err); |
| 170 | + into_response(err) |
| 171 | + })?; |
| 172 | + |
| 173 | + Ok(Json(res)) |
| 174 | +} |
| 175 | + |
| 176 | +pub fn create_auth_router(state: MintState) -> Router<MintState> { |
| 177 | + Router::new() |
| 178 | + .nest( |
| 179 | + "/auth/blind", |
| 180 | + Router::new() |
| 181 | + .route("/keys", get(get_blind_auth_keys)) |
| 182 | + .route("/keysets", get(get_auth_keysets)) |
| 183 | + .route("/keys/:keyset_id", get(get_keyset_pubkeys)) |
| 184 | + .route("/mint", post(post_mint_auth)), |
| 185 | + ) |
| 186 | + .with_state(state) |
| 187 | +} |
0 commit comments