1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
mod entity;
mod repo;
mod tag;
mod tree;
mod user;
use std::borrow::Borrow;
pub use entity::*;
pub use repo::*;
pub use tag::*;
pub use tree::*;
pub use user::*;
use drawbridge_type::{Meta, RepositoryContext, TagContext, TreeContext, UserContext, UserRecord};
use async_std::io;
use camino::{Utf8Path, Utf8PathBuf};
use cap_async_std::fs_utf8::Dir;
use futures::{try_join, TryFutureExt};
use openidconnect::SubjectIdentifier;
#[derive(Debug)]
pub struct Store {
root: Dir,
oidc: String,
}
async fn upsert_dir(root: &Dir, path: impl AsRef<Utf8Path>) -> io::Result<()> {
let path = path.as_ref();
if !root.is_dir(path).await {
root.create_dir(path)
} else {
Ok(())
}
}
impl Store {
pub async fn new(root: Dir, oidc: String) -> io::Result<Self> {
try_join!(upsert_dir(&root, "oidc"), upsert_dir(&root, "users"))?;
upsert_dir(&root, format!("oidc/{oidc}")).await?;
Ok(Self { root, oidc })
}
pub fn user(&self, UserContext { name }: &UserContext) -> User<'_, Utf8PathBuf> {
Entity::new(&self.root)
.child(format!("users/{name}"))
.into()
}
pub async fn create_user(
&self,
cx: &UserContext,
meta: Meta,
rec: &UserRecord,
) -> Result<User<'_>, CreateError<anyhow::Error>> {
let user = self.user(cx);
user.create_dir("").await?;
try_join!(
user.create_json(meta, rec),
user.create_dir("repos"),
user.symlink(format!("oidc/{}/{}", self.oidc, rec.subject))
.map_err(|e| match e {
SymlinkError::AlreadyExists => CreateError::Occupied,
SymlinkError::Internal(e) => CreateError::Internal(e),
})
)?;
Ok(user)
}
pub async fn user_by_subject(
&self,
subj: impl Borrow<SubjectIdentifier>,
) -> Result<(UserContext, User<'_>), GetError<anyhow::Error>> {
let (name, user) = Entity::new(&self.root)
.read_link(format!("oidc/{}/{}", self.oidc, subj.borrow().as_str()))
.await?;
let name = name.parse().map_err(|e: anyhow::Error| {
GetError::Internal(e.context("failed to parse user name"))
})?;
Ok((UserContext { name }, user.into()))
}
pub fn repository<'a>(
&'a self,
RepositoryContext { owner, name }: &'a RepositoryContext,
) -> Repository<'_> {
self.user(owner).repository(name)
}
pub fn tag<'a>(&'a self, TagContext { repository, name }: &'a TagContext) -> Tag<'_> {
self.repository(repository).tag(name)
}
pub fn tree<'a>(&'a self, TreeContext { tag, path }: &'a TreeContext) -> Node<'_> {
self.tag(tag).node(path)
}
}