| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::{path::PathBuf, pin::Pin}; |
| 7 | |
| 8 | use tokio::io::AsyncRead; |
| 9 | |
| 10 | use crate::domain::{OrgName, PasswordHash, RepoName}; |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | pub trait PasswordHasher: Send + Sync { |
| 18 | |
| 19 | fn hash(&self, plaintext: &str) -> Result<PasswordHash, PasswordError>; |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | fn verify(&self, plaintext: &str, hash: &PasswordHash) -> Result<bool, PasswordError>; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 31 | pub struct PasswordError(String); |
| 32 | |
| 33 | impl PasswordError { |
| 34 | pub fn new(message: impl Into<String>) -> Self { |
| 35 | Self(message.into()) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl std::fmt::Display for PasswordError { |
| 40 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 41 | write!(f, "password hashing failed: {}", self.0) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl std::error::Error for PasswordError {} |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | pub trait GitStorage: Send + Sync { |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | fn init_bare( |
| 67 | &self, |
| 68 | handle: &OrgName, |
| 69 | name: &RepoName, |
| 70 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | fn remove( |
| 78 | &self, |
| 79 | handle: &OrgName, |
| 80 | name: &RepoName, |
| 81 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | #[derive(Debug)] |
| 92 | pub enum GitStorageError { |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | AlreadyExists, |
| 99 | |
| 100 | Backend(Box<dyn std::error::Error + Send + Sync>), |
| 101 | } |
| 102 | |
| 103 | impl GitStorageError { |
| 104 | pub fn backend(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 105 | Self::Backend(error.into()) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl std::fmt::Display for GitStorageError { |
| 110 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 111 | match self { |
| 112 | Self::AlreadyExists => f.write_str("a repository already exists at that path"), |
| 113 | Self::Backend(error) => write!(f, "git storage failure: {error}"), |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl std::error::Error for GitStorageError { |
| 119 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 120 | match self { |
| 121 | Self::AlreadyExists => None, |
| 122 | Self::Backend(error) => Some(&**error), |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | pub type ByteStream = Pin<Box<dyn AsyncRead + Send>>; |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 140 | pub enum GitMethod { |
| 141 | Get, |
| 142 | Post, |
| 143 | } |
| 144 | |
| 145 | impl GitMethod { |
| 146 | pub fn as_str(self) -> &'static str { |
| 147 | match self { |
| 148 | Self::Get => "GET", |
| 149 | Self::Post => "POST", |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | pub struct GitRequest { |
| 163 | pub method: GitMethod, |
| 164 | |
| 165 | pub path_info: String, |
| 166 | |
| 167 | pub query: String, |
| 168 | pub content_type: Option<String>, |
| 169 | |
| 170 | |
| 171 | |
| 172 | pub content_encoding: Option<String>, |
| 173 | pub content_length: Option<String>, |
| 174 | |
| 175 | |
| 176 | pub git_protocol: Option<String>, |
| 177 | pub body: ByteStream, |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | pub struct GitResponse { |
| 186 | pub status: u16, |
| 187 | pub headers: Vec<(String, String)>, |
| 188 | pub body: ByteStream, |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | impl std::fmt::Debug for GitRequest { |
| 194 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 195 | f.debug_struct("GitRequest") |
| 196 | .field("method", &self.method) |
| 197 | .field("path_info", &self.path_info) |
| 198 | .field("query", &self.query) |
| 199 | .field("content_type", &self.content_type) |
| 200 | .field("content_encoding", &self.content_encoding) |
| 201 | .field("content_length", &self.content_length) |
| 202 | .field("git_protocol", &self.git_protocol) |
| 203 | .finish_non_exhaustive() |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | impl std::fmt::Debug for GitResponse { |
| 208 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 209 | f.debug_struct("GitResponse") |
| 210 | .field("status", &self.status) |
| 211 | .field("headers", &self.headers) |
| 212 | .finish_non_exhaustive() |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | pub trait GitProtocolServer: Send + Sync { |
| 221 | fn serve( |
| 222 | &self, |
| 223 | request: GitRequest, |
| 224 | ) -> impl Future<Output = Result<GitResponse, GitProtocolError>> + Send; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | #[derive(Debug)] |
| 233 | pub struct GitProtocolError(Box<dyn std::error::Error + Send + Sync>); |
| 234 | |
| 235 | impl GitProtocolError { |
| 236 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 237 | Self(error.into()) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | impl std::fmt::Display for GitProtocolError { |
| 242 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 243 | write!(f, "git protocol failure: {}", self.0) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | impl std::error::Error for GitProtocolError { |
| 248 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 249 | Some(&*self.0) |
| 250 | } |
| 251 | } |