| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::{caller, ApiError, Caller}; |
| 6 | | use crate::auth::{phone, totp}; |
| 7 | | use crate::db::{self, accounts, approvals}; |
| 8 | | use crate::Shared; |
| 9 | | use axum::extract::{Path, State}; |
| 10 | | use axum::http::{HeaderMap, StatusCode}; |
| 11 | | use axum::Json; |
| 12 | | use hub_api::{ApprovalAnswer, Authenticator, NewAuthenticator, PendingApproval}; |
| 13 | |
|
| 14 | |
|
| 15 | 10× | fn phone_of(c: &Caller) -> Result<(&accounts::Account, i64), ApiError> { |
| 16 | 10× | let account = c.require("user:read")?; |
| 17 | 10× | if c.agent { |
| 18 | 0× | return Err(ApiError(StatusCode::FORBIDDEN, "an agent cannot be a second factor".into())); |
| 19 | 10× | } |
| 20 | 10× | let Some(id) = c.token_id else { |
| 21 | 0× | return Err(ApiError(StatusCode::UNAUTHORIZED, "a phone uses the token it signed in with".into())); |
| 22 | | }; |
| 23 | 10× | Ok((account, id)) |
| 24 | 10× | } |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | 3× | pub async fn add( |
| 30 | 3× | State(app): State<Shared>, |
| 31 | 3× | headers: HeaderMap, |
| 32 | 3× | Json(r): Json<NewAuthenticator>, |
| 33 | 3× | ) -> Result<(StatusCode, Json<Authenticator>), ApiError> { |
| 34 | 3× | let c = caller(&app, &headers).await?; |
| 35 | 3× | let (account, id) = phone_of(&c)?; |
| 36 | 3× | let Some(secret) = account.totp_secret.as_deref() else { |
| 37 | 1× | return Err(ApiError( |
| 38 | 1× | StatusCode::CONFLICT, |
| 39 | 1× | "set up an authenticator app first (Settings › Security on the hub): it stays the way in if this phone is lost".into(), |
| 40 | 1× | )); |
| 41 | | }; |
| 42 | 2× | if !totp::verify(secret, r.code.trim(), (db::now_ms() / 1000) as u64) { |
| 43 | 1× | accounts::audit(&app.db, Some(account), "2fa.phone_add_failed", &c.token, "wrong code").await.ok(); |
| 44 | 1× | return Err(ApiError(StatusCode::FORBIDDEN, "that code is not right".into())); |
| 45 | 1× | } |
| 46 | 1× | let Some(point) = phone::public_point(&r.public_key) else { |
| 47 | 0× | return Err(ApiError(StatusCode::BAD_REQUEST, "public_key is a P-256 key: hex of its SubjectPublicKeyInfo".into())); |
| 48 | | }; |
| 49 | 1× | approvals::add_authenticator(&app.db, id, account.id, &c.token, &point).await?; |
| 50 | 1× | accounts::audit(&app.db, Some(account), "2fa.phone_added", &c.token, "").await.ok(); |
| 51 | 1× | let a = approvals::authenticator(&app.db, id).await?.expect("just added"); |
| 52 | 1× | Ok((StatusCode::CREATED, Json(Authenticator { id, name: a.name, created_ms: a.created_ms, last_used_ms: a.last_used_ms, this: true }))) |
| 53 | 3× | } |
| 54 | |
|
| 55 | |
|
| 56 | 1× | pub async fn list(State(app): State<Shared>, headers: HeaderMap) -> Result<Json<Vec<Authenticator>>, ApiError> { |
| 57 | 1× | let c = caller(&app, &headers).await?; |
| 58 | 1× | let account = c.require("user:read")?; |
| 59 | 1× | let this = c.token_id; |
| 60 | | Ok(Json( |
| 61 | 1× | approvals::authenticators(&app.db, account.id) |
| 62 | 1× | .await? |
| 63 | 1× | .into_iter() |
| 64 | 1× | .map(|a| Authenticator { |
| 65 | 0× | this: Some(a.token_id) == this, |
| 66 | 0× | id: a.token_id, |
| 67 | 0× | name: a.name, |
| 68 | 0× | created_ms: a.created_ms, |
| 69 | 0× | last_used_ms: a.last_used_ms, |
| 70 | 0× | }) |
| 71 | 1× | .collect(), |
| 72 | | )) |
| 73 | 1× | } |
| 74 | |
|
| 75 | |
|
| 76 | 0× | pub async fn remove(State(app): State<Shared>, headers: HeaderMap) -> Result<StatusCode, ApiError> { |
| 77 | 0× | let c = caller(&app, &headers).await?; |
| 78 | 0× | let (account, id) = phone_of(&c)?; |
| 79 | 0× | if approvals::remove_authenticator(&app.db, account.id, id).await? { |
| 80 | 0× | accounts::audit(&app.db, Some(account), "2fa.phone_removed", &c.token, "").await.ok(); |
| 81 | 0× | } |
| 82 | 0× | Ok(StatusCode::NO_CONTENT) |
| 83 | 0× | } |
| 84 | |
|
| 85 | |
|
| 86 | 3× | pub async fn pending(State(app): State<Shared>, headers: HeaderMap) -> Result<Json<Vec<PendingApproval>>, ApiError> { |
| 87 | 3× | let c = caller(&app, &headers).await?; |
| 88 | 3× | let (account, id) = phone_of(&c)?; |
| 89 | 3× | if approvals::authenticator(&app.db, id).await?.is_none() { |
| 90 | 0× | return Ok(Json(Vec::new())); |
| 91 | 3× | } |
| 92 | | Ok(Json( |
| 93 | 3× | approvals::pending(&app.db, account.id, db::now_ms()) |
| 94 | 3× | .await? |
| 95 | 3× | .into_iter() |
| 96 | 3× | .map(|a| PendingApproval { |
| 97 | 9× | choices: a.choices.split(',').filter_map(|n| n.parse().ok()).collect(), |
| 98 | 3× | id: a.id, |
| 99 | 3× | kind: a.kind, |
| 100 | 3× | what: a.what, |
| 101 | 3× | created_ms: a.created_ms, |
| 102 | 3× | expires_ms: a.expires_ms, |
| 103 | 3× | }) |
| 104 | 3× | .collect(), |
| 105 | | )) |
| 106 | 3× | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | 4× | pub async fn answer( |
| 112 | 4× | State(app): State<Shared>, |
| 113 | 4× | headers: HeaderMap, |
| 114 | 4× | Path(approval): Path<String>, |
| 115 | 4× | Json(r): Json<ApprovalAnswer>, |
| 116 | 4× | ) -> Result<Json<serde_json::Value>, ApiError> { |
| 117 | 4× | let c = caller(&app, &headers).await?; |
| 118 | 4× | let (account, id) = phone_of(&c)?; |
| 119 | 4× | let Some(key) = approvals::authenticator(&app.db, id).await?.filter(|a| a.account_id == account.id) else { |
| 120 | 0× | return Err(ApiError(StatusCode::FORBIDDEN, "this phone is not a second factor of the account".into())); |
| 121 | | }; |
| 122 | 4× | let now = db::now_ms(); |
| 123 | 4× | let Some(a) = approvals::get(&app.db, &approval).await?.filter(|a| a.account_id == account.id) else { |
| 124 | 0× | return Err(ApiError(StatusCode::NOT_FOUND, "no such approval".into())); |
| 125 | | }; |
| 126 | 4× | if a.state != "pending" || a.expires_ms <= now { |
| 127 | 0× | return Err(ApiError(StatusCode::CONFLICT, "that approval was answered already, or ran out of time".into())); |
| 128 | 4× | } |
| 129 | 4× | let state = if !r.approve { |
| 130 | 0× | "denied" |
| 131 | | } else { |
| 132 | 4× | let Ok(sig) = hex::decode(r.signature.trim()) else { |
| 133 | 0× | return Err(ApiError(StatusCode::BAD_REQUEST, "signature is hex".into())); |
| 134 | | }; |
| 135 | 4× | if !phone::verify(&key.public_key, &phone::message(&a.id, r.number, true), &sig) { |
| 136 | 1× | accounts::audit(&app.db, Some(account), "2fa.bad_signature", &c.token, &a.kind).await.ok(); |
| 137 | 1× | return Err(ApiError(StatusCode::FORBIDDEN, "the signature is not this phone's".into())); |
| 138 | 3× | } |
| 139 | 3× | if r.number == a.number { |
| 140 | 2× | "approved" |
| 141 | | } else { |
| 142 | 1× | "denied" |
| 143 | | } |
| 144 | | }; |
| 145 | 3× | if !approvals::decide(&app.db, &a.id, state, id, now).await? { |
| 146 | 0× | return Err(ApiError(StatusCode::CONFLICT, "that approval was answered already, or ran out of time".into())); |
| 147 | 3× | } |
| 148 | 3× | if state == "approved" { |
| 149 | 2× | if let Some(session) = a.session_id.filter(|_| a.kind == "stepup") { |
| 150 | 1× | db::sessions::mark_second_factor(&app.db, session, now).await?; |
| 151 | 1× | } |
| 152 | 1× | } |
| 153 | 3× | let detail = if r.approve && state == "denied" { format!("{}: wrong number", a.kind) } else { a.kind.clone() }; |
| 154 | 3× | accounts::audit(&app.db, Some(account), &format!("2fa.{state}"), &c.token, &detail).await.ok(); |
| 155 | 3× | Ok(Json(serde_json::json!({ "state": state }))) |
| 156 | 4× | } |