| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::time::SystemTime; |
| 7 | |
| 8 | use serde::Deserialize; |
| 9 | use topcoat::{ |
| 10 | Result, |
| 11 | context::Cx, |
| 12 | router::{StatusCode, content::Form, error::forbidden, page, path_param}, |
| 13 | view::{attributes, component, view}, |
| 14 | }; |
| 15 | |
| 16 | use crate::{ |
| 17 | application::{Error, TokenSummary, issue_token, list_tokens, revoke_token}, |
| 18 | components::{ |
| 19 | badge::{BadgeVariant, badge}, |
| 20 | button::{ButtonSize, ButtonVariant, button}, |
| 21 | flash::{FlashKind, flash}, |
| 22 | input::input, |
| 23 | label::label, |
| 24 | select::select, |
| 25 | }, |
| 26 | domain::{DomainError, TokenId, TokenLifetime}, |
| 27 | }; |
| 28 | |
| 29 | use super::{ |
| 30 | browse::{ago, timestamp}, |
| 31 | context::{current_actor, location, server_error, tokens}, |
| 32 | layout::narrow, |
| 33 | profile::profile_for, |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | #[path_param] |
| 38 | struct Token(str); |
| 39 | |
| 40 | #[derive(Debug, Deserialize)] |
| 41 | struct IssueForm { |
| 42 | name: String, |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | expires: String, |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | async fn own_settings(cx: &Cx) -> Result<String> { |
| 56 | let profile = profile_for(cx).await?; |
| 57 | |
| 58 | if !profile.viewer_is_owner { |
| 59 | return Err(forbidden().into()); |
| 60 | } |
| 61 | |
| 62 | Ok(profile.handle.to_string()) |
| 63 | } |
| 64 | |
| 65 | async fn listing(cx: &Cx) -> Result<Vec<TokenSummary>> { |
| 66 | list_tokens(¤t_actor(cx).await?, SystemTime::now(), &tokens(cx)) |
| 67 | .await |
| 68 | .map_err(server_error) |
| 69 | } |
| 70 | |
| 71 | #[page("/{handle}/settings/tokens")] |
| 72 | async fn tokens_page(cx: &Cx) -> Result { |
| 73 | let handle = own_settings(cx).await?; |
| 74 | let listed = listing(cx).await?; |
| 75 | |
| 76 | view! { |
| 77 | tokens_view( |
| 78 | handle: handle.as_str(), |
| 79 | tokens: &listed, |
| 80 | issued: "", |
| 81 | error: "", |
| 82 | name: "", |
| 83 | expires: TokenLifetime::default().as_str(), |
| 84 | ) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | #[page(POST "/{handle}/settings/tokens")] |
| 95 | async fn issue(cx: &Cx, Form(submitted): Form<IssueForm>) -> Result { |
| 96 | let handle = own_settings(cx).await?; |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | let outcome = match submitted.expires.parse() { |
| 102 | Ok(lifetime) => { |
| 103 | issue_token( |
| 104 | ¤t_actor(cx).await?, |
| 105 | &submitted.name, |
| 106 | lifetime, |
| 107 | SystemTime::now(), |
| 108 | &tokens(cx), |
| 109 | ) |
| 110 | .await |
| 111 | } |
| 112 | Err(error) => Err(Error::Domain(error)), |
| 113 | }; |
| 114 | |
| 115 | let (issued, error) = match outcome { |
| 116 | Ok(issued) => (issued.secret.reveal().to_owned(), String::new()), |
| 117 | Err(Error::Domain(DomainError::Validation { field, reason })) => { |
| 118 | (String::new(), format!("That {field} is no good: {reason}.")) |
| 119 | } |
| 120 | Err(Error::Domain(DomainError::Forbidden)) => return Err(forbidden().into()), |
| 121 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 122 | }; |
| 123 | |
| 124 | let listed = listing(cx).await?; |
| 125 | |
| 126 | view! { |
| 127 | tokens_view( |
| 128 | handle: handle.as_str(), |
| 129 | tokens: &listed, |
| 130 | issued: issued.as_str(), |
| 131 | error: error.as_str(), |
| 132 | |
| 133 | |
| 134 | name: if issued.is_empty() { submitted.name.as_str() } else { "" }, |
| 135 | |
| 136 | |
| 137 | expires: if issued.is_empty() { |
| 138 | submitted.expires.as_str() |
| 139 | } else { |
| 140 | TokenLifetime::default().as_str() |
| 141 | }, |
| 142 | ) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | #[page(POST "/{handle}/settings/tokens/{token}/revoke")] |
| 148 | async fn revoke(cx: &Cx) -> Result { |
| 149 | let handle = own_settings(cx).await?; |
| 150 | let id = TokenId::from_trusted(path_param::<Token>(cx)); |
| 151 | |
| 152 | match revoke_token(¤t_actor(cx).await?, &id, &tokens(cx)).await { |
| 153 | Ok(()) => {} |
| 154 | |
| 155 | Err(Error::Domain(DomainError::NotFound { .. })) => {} |
| 156 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 157 | } |
| 158 | |
| 159 | view! { |
| 160 | (StatusCode::SEE_OTHER) |
| 161 | (location(&format!("/{handle}/settings/tokens"))?) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | fn expiry(summary: &TokenSummary) -> String { |
| 170 | let Some(expires_at) = summary.expires_at else { |
| 171 | return "Never expires".to_owned(); |
| 172 | }; |
| 173 | |
| 174 | let date = timestamp(expires_at) |
| 175 | .split(' ') |
| 176 | .next() |
| 177 | .unwrap_or_default() |
| 178 | .to_owned(); |
| 179 | |
| 180 | if summary.expired { |
| 181 | format!("Expired {date}") |
| 182 | } else { |
| 183 | format!("Expires {date}") |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | fn last_used(summary: &TokenSummary) -> String { |
| 189 | summary.last_used_at.map_or_else( |
| 190 | || "Never used".to_owned(), |
| 191 | |at| format!("Last used {}", ago(at)), |
| 192 | ) |
| 193 | } |
| 194 | |
| 195 | #[component] |
| 196 | async fn tokens_view( |
| 197 | handle: &str, |
| 198 | tokens: &[TokenSummary], |
| 199 | issued: &str, |
| 200 | error: &str, |
| 201 | name: &str, |
| 202 | expires: &str, |
| 203 | ) -> Result { |
| 204 | view! { |
| 205 | narrow( |
| 206 | <h1 class="text-xl font-semibold tracking-tight">"Access tokens"</h1> |
| 207 | <p class="mt-1 text-sm text-muted-foreground"> |
| 208 | "Use a token as the password when git asks. Your username can be anything." |
| 209 | </p> |
| 210 | |
| 211 | if !issued.is_empty() { |
| 212 | <div class="mt-6 space-y-2"> |
| 213 | flash( |
| 214 | kind: FlashKind::Success, |
| 215 | "Copy this now — it is not shown again." |
| 216 | ) |
| 217 | <pre class="overflow-x-auto rounded-lg border border-border bg-surface px-4 py-3 font-mono text-sm">(issued)</pre> |
| 218 | </div> |
| 219 | } |
| 220 | |
| 221 | if !error.is_empty() { |
| 222 | <div class="mt-6"> |
| 223 | flash(kind: FlashKind::Error, (error)) |
| 224 | </div> |
| 225 | } |
| 226 | |
| 227 | <form method="post" action=(format!("/{handle}/settings/tokens")) class="mt-6 space-y-5"> |
| 228 | <div class="space-y-2"> |
| 229 | label(attrs: attributes! { for="name" }, "Token name") |
| 230 | input(attrs: attributes! { |
| 231 | id="name" |
| 232 | name="name" |
| 233 | type="text" |
| 234 | value=(name) |
| 235 | placeholder="laptop" |
| 236 | required=(true) |
| 237 | }) |
| 238 | <p class="text-xs text-muted-foreground"> |
| 239 | "So you can tell which one to revoke later." |
| 240 | </p> |
| 241 | </div> |
| 242 | |
| 243 | <div class="space-y-2"> |
| 244 | label(attrs: attributes! { for="expires" }, "Expiry") |
| 245 | select( |
| 246 | attrs: attributes! { id="expires" name="expires" class="sm:w-48" }, |
| 247 | for lifetime in TokenLifetime::ALL { |
| 248 | <option |
| 249 | value=(lifetime.as_str()) |
| 250 | selected=(lifetime.as_str() == expires) |
| 251 | >(lifetime.label())</option> |
| 252 | } |
| 253 | ) |
| 254 | <p class="text-xs text-muted-foreground"> |
| 255 | "A token that expires stops working on its own if it is ever " |
| 256 | "leaked or forgotten. Pick "<span class="font-medium">"No expiry"</span> |
| 257 | " only for something nobody will be around to rotate." |
| 258 | </p> |
| 259 | </div> |
| 260 | |
| 261 | button(attrs: attributes! { type="submit" }, "Create token") |
| 262 | </form> |
| 263 | |
| 264 | <section class="mt-10"> |
| 265 | <h2 class="text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 266 | "Your tokens" |
| 267 | </h2> |
| 268 | |
| 269 | if tokens.is_empty() { |
| 270 | <p class="mt-2 rounded-lg border border-border px-4 py-6 text-center text-sm text-muted-foreground"> |
| 271 | "No tokens yet." |
| 272 | </p> |
| 273 | } else { |
| 274 | <ul class="mt-2 divide-y divide-border rounded-lg border border-border"> |
| 275 | for token in tokens { |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | <li class="flex items-center justify-between gap-4 px-4 py-3"> |
| 281 | <div |
| 282 | class=(if token.expired { |
| 283 | "min-w-0 space-y-1 opacity-60" |
| 284 | } else { |
| 285 | "min-w-0 space-y-1" |
| 286 | }) |
| 287 | > |
| 288 | <div class="flex flex-wrap items-baseline gap-2"> |
| 289 | <span class="font-medium">(token.name.as_str())</span> |
| 290 | badge( |
| 291 | variant: BadgeVariant::Outline, |
| 292 | "steid_pat_" (token.prefix.as_str()) "…" |
| 293 | ) |
| 294 | if token.expired { |
| 295 | badge(variant: BadgeVariant::Secondary, "Expired") |
| 296 | } |
| 297 | </div> |
| 298 | <p class="text-xs text-muted-foreground"> |
| 299 | <span title=(token.expires_at.map(timestamp).unwrap_or_default())> |
| 300 | (expiry(token)) |
| 301 | </span> |
| 302 | " · " |
| 303 | <span title=(token.last_used_at.map(timestamp).unwrap_or_default())> |
| 304 | (last_used(token)) |
| 305 | </span> |
| 306 | </p> |
| 307 | </div> |
| 308 | <form |
| 309 | method="post" |
| 310 | action=(format!("/{handle}/settings/tokens/{}/revoke", token.id)) |
| 311 | > |
| 312 | |
| 313 | |
| 314 | |
| 315 | button( |
| 316 | attrs: attributes! { type="submit" }, |
| 317 | variant: ButtonVariant::Ghost, |
| 318 | size: ButtonSize::Sm, |
| 319 | "Revoke" |
| 320 | ) |
| 321 | </form> |
| 322 | </li> |
| 323 | } |
| 324 | </ul> |
| 325 | } |
| 326 | </section> |
| 327 | |
| 328 | <p class="mt-8"> |
| 329 | <a |
| 330 | href=(format!("/{handle}/settings")) |
| 331 | class="text-sm text-muted-foreground hover:text-foreground" |
| 332 | >"Back to settings"</a> |
| 333 | </p> |
| 334 | ) |
| 335 | } |
| 336 | } |