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