14223e7feat: claim, sign in, and sign out through the browser1mo | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use std::time::SystemTime; |
| 8 | |
| 9 | use sqlx::SqlitePool; |
| 10 | use topcoat::{ |
| 11 | Result, |
| 12 | context::{Cx, app_context}, |
285f5fdfeat: create and view repositories through the browser24d | 13 | router::{HeaderName, HeaderValue, error::internal_server_error, header::LOCATION}, |
14223e7feat: claim, sign in, and sign out through the browser1mo | 14 | session, |
| 15 | }; |
| 16 | |
| 17 | use crate::{ |
285f5fdfeat: create and view repositories through the browser24d | 18 | application::{AppConfig, Identity, describe_identity, is_claimed, resolve_actor}, |
14223e7feat: claim, sign in, and sign out through the browser1mo | 19 | domain::{Actor, SessionTokenHash}, |
285f5fdfeat: create and view repositories through the browser24d | 20 | infrastructure::{ |
| 21 | git::DiskGitStorage, |
| 22 | repository::{ |
| 23 | SqliteMembershipRepo, SqliteOrgRepo, SqliteRepoRepo, SqliteSessionRepo, SqliteUserRepo, |
| 24 | }, |
a69e380feat: public profile page at /{handle}1mo | 25 | }, |
14223e7feat: claim, sign in, and sign out through the browser1mo | 26 | }; |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | pub struct SetupState(pub crate::domain::SetupToken); |
| 32 | |
| 33 | pub fn pool(cx: &Cx) -> &SqlitePool { |
| 34 | app_context::<SqlitePool>(cx) |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
a69e380feat: public profile page at /{handle}1mo | 39 | pub fn server_error<E>(error: E) -> topcoat::Error |
14223e7feat: claim, sign in, and sign out through the browser1mo | 40 | where |
| 41 | E: std::error::Error + Send + Sync + 'static, |
| 42 | { |
| 43 | eprintln!("steid: {error}"); |
| 44 | internal_server_error(error).into() |
| 45 | } |
| 46 | |
a69e380feat: public profile page at /{handle}1mo | 47 | pub fn orgs(cx: &Cx) -> SqliteOrgRepo { |
| 48 | SqliteOrgRepo::new(pool(cx).clone()) |
| 49 | } |
| 50 | |
| 51 | pub fn memberships(cx: &Cx) -> SqliteMembershipRepo { |
| 52 | SqliteMembershipRepo::new(pool(cx).clone()) |
| 53 | } |
| 54 | |
285f5fdfeat: create and view repositories through the browser24d | 55 | pub fn repos(cx: &Cx) -> SqliteRepoRepo { |
| 56 | SqliteRepoRepo::new(pool(cx).clone()) |
| 57 | } |
| 58 | |
| 59 | |
| 60 | pub fn storage(cx: &Cx) -> DiskGitStorage { |
| 61 | DiskGitStorage::new(app_context::<AppConfig>(cx).data_dir.clone()) |
| 62 | } |
| 63 | |
14223e7feat: claim, sign in, and sign out through the browser1mo | 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub async fn current_actor(cx: &Cx) -> Result<Actor> { |
| 69 | let presented = session::token_hash(cx) |
| 70 | .await? |
| 71 | .map(|hash| SessionTokenHash::from_trusted(hex(&hash))); |
| 72 | |
| 73 | let sessions = SqliteSessionRepo::new(pool(cx).clone()); |
| 74 | |
| 75 | resolve_actor(presented.as_ref(), SystemTime::now(), &sessions) |
| 76 | .await |
| 77 | .map_err(server_error) |
| 78 | } |
| 79 | |
40ab5c7feat: /api/me and a shared identity read model1mo | 80 | |
| 81 | |
| 82 | |
| 83 | pub async fn identity(cx: &Cx) -> Result<Option<Identity>> { |
| 84 | let actor = current_actor(cx).await?; |
| 85 | let pool = pool(cx).clone(); |
| 86 | |
| 87 | describe_identity( |
| 88 | &actor, |
| 89 | &SqliteUserRepo::new(pool.clone()), |
| 90 | &SqliteOrgRepo::new(pool), |
| 91 | ) |
| 92 | .await |
| 93 | .map_err(server_error) |
| 94 | } |
| 95 | |
14223e7feat: claim, sign in, and sign out through the browser1mo | 96 | |
| 97 | pub async fn claimed(cx: &Cx) -> Result<bool> { |
| 98 | let users = SqliteUserRepo::new(pool(cx).clone()); |
| 99 | |
| 100 | is_claimed(&users).await.map_err(server_error) |
| 101 | } |
| 102 | |
| 103 | |
| 104 | pub fn setup_token(cx: &Cx) -> Option<&crate::domain::SetupToken> { |
| 105 | topcoat::context::try_app_context::<SetupState>(cx).map(|state| &state.0) |
| 106 | } |
| 107 | |
285f5fdfeat: create and view repositories through the browser24d | 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | pub fn location(uri: &str) -> Result<(HeaderName, HeaderValue)> { |
| 123 | Ok((LOCATION, HeaderValue::try_from(uri).map_err(server_error)?)) |
| 124 | } |
| 125 | |
14223e7feat: claim, sign in, and sign out through the browser1mo | 126 | |
| 127 | pub fn hex(hash: &session::TokenHash) -> String { |
| 128 | hash.iter().map(|byte| format!("{byte:02x}")).collect() |
| 129 | } |