forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching0 forksRustAGPL-3.010.2 MiB197fa34Tahinli1 month ago68 commits
| 1 | //! Our own small JSON API under `/api/v1`. Authentication is by personal access token only: |
| 2 | //! `Authorization: Bearer forge_...` or `Authorization: token forge_...`, or, for Gitea clients, Basic auth |
| 3 | //! with the token as the password or `?access_token=` on the Gitea routes. Session cookies are never |
| 4 | //! read here, so a browser cannot drive these routes and CSRF protection does not apply. Errors are |
| 5 | //! `{"error": "..."}` with a matching status code. Anonymous callers get 401 whenever they cannot |
| 6 | //! use a repo, the same as git, so missing and hidden repos look identical to them. |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | use IpAddr; |
| 15 | use PathBuf; |
| 16 | use Instant; |
| 17 | |
| 18 | use ; |
| 19 | use browse; |
| 20 | use HeaderMap; |
| 21 | use ; |
| 22 | use ; |
| 23 | use StatusCode; |
| 24 | use Response; |
| 25 | |
| 26 | use crateAppState; |
| 27 | use crate; |
| 28 | |
| 29 | /// The scope a token needs to use this API. It is the only scope the token form offers. |
| 30 | pub const REPO_SCOPE: &str = "repo"; |
| 31 | |
| 32 | /// A failed step. The response is boxed, as in git_http, to keep `Result` small. |
| 33 | pub type Refusal = ; |
| 34 | |
| 35 | /// Builds a JSON response with the given status. |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | /// A JSON error: `{"error": message}`. |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | /// 401 with a Bearer challenge. |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | /// A 500 that does not reveal the cause; the cause is logged. |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | /// Pulls the token out of `Bearer <token>`, `token <token>` or `Basic base64(user:<token>)`. The |
| 71 | /// scheme is matched case-insensitively. A Basic user name is ignored, since the token alone |
| 72 | /// names the user. Returns `None` for a missing header and `Some(None)` for any other shape. |
| 73 | pub |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | /// Resolves the caller from the `Authorization` header. |
| 92 | /// |
| 93 | /// - No header: `Ok(None)`, an anonymous caller. |
| 94 | /// - A header that is not a forge token, an unknown or expired token, or a token whose user is |
| 95 | /// gone: 401, and the failure counts against the client IP. The budget is the git one, so |
| 96 | /// guesses cannot be split between git and the API. |
| 97 | /// - A token without the `repo` scope: 403, which does not count as a failed login. |
| 98 | /// - Otherwise the token's `last_used_at` is touched and the user is returned. |
| 99 | pub async |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | /// Maps a refused action. Anonymous callers always get 401, whether the repo is hidden, missing, |
| 153 | /// or merely read-only to them. Signed-in callers get 404 for hidden repos and 403 otherwise. |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | /// Loads `owner/name` and checks that `actor` may perform `action` on it. |
| 163 | pub async |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | /// On-disk path of a repo's bare directory. |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | /// True when `sha` names a commit in the repo. Runs the git read off the async runtime. |
| 200 | pub async |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | /// Lowercases a full commit id. `None` unless it is 40 or 64 hex characters. |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | /// Reads the request body, refusing anything over `limit` bytes. |
| 215 | pub async |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 |