| df71c789 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use super::settings::{csrf, require, SCOPES}; |
| 60f11b71 Andy McCall ↶ |
6 |
use super::{filters, Error, Page, Shell}; |
| df71c789 Andy McCall ↶ |
7 |
use crate::api::device::normalize_user_code; |
| 8 |
use crate::auth::Viewer; |
| 9 |
use crate::db::{self, accounts}; |
| aa0bfbd1 Andy McCall ↶ |
10 |
use crate::throttle::{self, Key}; |
| df71c789 Andy McCall ↶ |
11 |
use crate::Shared; |
| 12 |
use askama::Template; |
| 13 |
use axum::extract::{Query, State}; |
| 14 |
use serde::Deserialize; |
| 15 |
|
| 16 |
#[derive(Template)] |
| 17 |
#[template(path = "device.html")] |
| 18 |
pub struct DevicePage { |
| 19 |
shell: Shell, |
| 20 |
code: String, |
| 21 |
|
| 22 |
pending: Option<(accounts::DeviceCode, Vec<(&'static str, &'static str)>)>, |
| 4da8b828 Andy McCall ↶ |
23 |
|
| 24 |
|
| 25 |
done: Option<Done>, |
| df71c789 Andy McCall ↶ |
26 |
error: Option<String>, |
| 27 |
} |
| 28 |
|
| 4da8b828 Andy McCall ↶ |
29 |
pub struct Done { |
| 30 |
pub approved: bool, |
| 31 |
|
| 32 |
pub client: String, |
| 33 |
pub username: String, |
| 34 |
} |
| 35 |
|
| df71c789 Andy McCall ↶ |
36 |
|
| 37 |
async fn pending(app: &Shared, code: &str) -> Result<Option<accounts::DeviceCode>, sqlx::Error> { |
| 38 |
Ok(accounts::device_by_user_code(&app.db, code).await?.filter(|d| d.account_id.is_none() && !d.denied && d.expires_ms >= db::now_ms())) |
| 39 |
} |
| 40 |
|
| 4da8b828 Andy McCall ↶ |
41 |
fn page(shell: Shell, code: String, d: Option<accounts::DeviceCode>, done: Option<Done>, error: Option<String>) -> Page<DevicePage> { |
| df71c789 Andy McCall ↶ |
42 |
let pending = d.map(|d| { |
| 43 |
let asked: Vec<(&'static str, &'static str)> = SCOPES.iter().filter(|(k, _)| d.scope_list().contains(k)).copied().collect(); |
| 44 |
(d, asked) |
| 45 |
}); |
| 46 |
Page(DevicePage { shell, code, pending, done, error }) |
| 47 |
} |
| 48 |
|
| 49 |
#[derive(Deserialize)] |
| 50 |
pub struct CodeQuery { |
| 51 |
#[serde(default)] |
| 52 |
code: String, |
| 53 |
} |
| 54 |
|
| 55 |
pub async fn form(State(app): State<Shared>, viewer: Viewer, Query(q): Query<CodeQuery>) -> Result<Page<DevicePage>, Error> { |
| 56 |
let shell = Shell::new(&app, "/device", &viewer); |
| 57 |
require(&viewer, &shell)?; |
| 58 |
let code = normalize_user_code(&q.code); |
| 59 |
if code.is_empty() { |
| 60 |
return Ok(page(shell, code, None, None, None)); |
| 61 |
} |
| 62 |
let d = pending(&app, &code).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 63 |
let error = d.is_none().then(|| "No app is waiting for that code: it may have expired. Ask the app for a new one.".to_string()); |
| 64 |
Ok(page(shell, code, d, None, error)) |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Deserialize)] |
| 68 |
pub struct DeviceForm { |
| 69 |
#[serde(rename = "_csrf")] |
| 70 |
csrf_token: String, |
| 71 |
code: String, |
| 72 |
#[serde(default)] |
| 73 |
action: String, |
| 74 |
#[serde(default)] |
| 75 |
scopes: Vec<String>, |
| 76 |
} |
| 77 |
|
| 78 |
pub async fn answer( |
| 79 |
State(app): State<Shared>, |
| 80 |
viewer: Viewer, |
| aa0bfbd1 Andy McCall ↶ |
81 |
headers: axum::http::HeaderMap, |
| 82 |
extensions: axum::http::Extensions, |
| df71c789 Andy McCall ↶ |
83 |
axum_extra::extract::Form(f): axum_extra::extract::Form<DeviceForm>, |
| 84 |
) -> Result<Page<DevicePage>, Error> { |
| 85 |
let shell = Shell::new(&app, "/device", &viewer); |
| 86 |
let me = require(&viewer, &shell)?; |
| 87 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 88 |
let code = normalize_user_code(&f.code); |
| aa0bfbd1 Andy McCall ↶ |
89 |
|
| 90 |
let addr = crate::api::client_addr(&app, &headers, &extensions); |
| 91 |
let keys = [Key::account("device", &me.account.username), Key::address("device", &addr)]; |
| 92 |
let now = db::now_ms(); |
| 93 |
if let Some(wait) = app.throttle.lock().unwrap().wait(&keys, now) { |
| 94 |
return Ok(page(shell, code, None, None, Some(throttle::message(wait)))); |
| 95 |
} |
| df71c789 Andy McCall ↶ |
96 |
let d = pending(&app, &code).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 97 |
let Some(d) = d else { |
| aa0bfbd1 Andy McCall ↶ |
98 |
let started = app.throttle.lock().unwrap().failed(&keys, now); |
| 99 |
if let Some(wait) = started { |
| 100 |
accounts::audit(&app.db, Some(&me.account), "device.throttled", &addr, &format!("for {}s", wait / 1000)).await.ok(); |
| 101 |
} |
| df71c789 Andy McCall ↶ |
102 |
let e = "No app is waiting for that code: it may have expired. Ask the app for a new one."; |
| 103 |
return Ok(page(shell, code, None, None, Some(e.into()))); |
| 104 |
}; |
| 105 |
match f.action.as_str() { |
| 106 |
"approve" => { |
| 107 |
|
| 108 |
let asked = d.scope_list(); |
| 109 |
let chosen: Vec<&str> = f.scopes.iter().map(String::as_str).filter(|s| asked.contains(s)).collect(); |
| 110 |
if chosen.is_empty() { |
| 111 |
return Ok(page(shell, code, Some(d), None, Some("Allow at least one of what it asks for, or deny it.".into()))); |
| 112 |
} |
| 113 |
let ok = accounts::approve_device(&app.db, d.id, me.account.id, &chosen.join(",")) |
| 114 |
.await |
| 115 |
.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 116 |
if !ok { |
| 117 |
return Ok(page(shell, code, None, None, Some("That code was answered or expired meanwhile.".into()))); |
| 118 |
} |
| 119 |
accounts::audit(&app.db, Some(&me.account), "device.approved", &d.client, &chosen.join(",")).await.ok(); |
| 4da8b828 Andy McCall ↶ |
120 |
let done = Done { approved: true, client: d.client.clone(), username: me.account.username.clone() }; |
| df71c789 Andy McCall ↶ |
121 |
Ok(page(shell, String::new(), None, Some(done), None)) |
| 122 |
} |
| 123 |
"deny" => { |
| 124 |
accounts::deny_device(&app.db, d.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 125 |
accounts::audit(&app.db, Some(&me.account), "device.denied", &d.client, "").await.ok(); |
| 4da8b828 Andy McCall ↶ |
126 |
Ok(page( |
| 127 |
shell, |
| 128 |
String::new(), |
| 129 |
None, |
| 130 |
Some(Done { approved: false, client: d.client.clone(), username: me.account.username.clone() }), |
| 131 |
None, |
| 132 |
)) |
| df71c789 Andy McCall ↶ |
133 |
} |
| 134 |
|
| 135 |
_ => Ok(page(shell, code, Some(d), None, None)), |
| 136 |
} |
| 137 |
} |