forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.3 MiB·0d793e7·Tahinli·4 hours ago·359 commits
| 1 | use base64::prelude::{BASE64_STANDARD, Engine}; |
| 2 | use forge_core::{User, token::hash_token}; |
| 3 | use http::{HeaderMap, header::AUTHORIZATION}; |
| 4 | |
| 5 | use crate::access::AccessError; |
| 6 | use crate::state::AppState; |
| 7 | |
| 8 | const TOKEN_ONLY_USERNAMES: [&str; 2] = ["x-access-token", "oauth2"]; |
| 9 | |
| 10 | pub fn parse_basic_auth(headers: &HeaderMap) -> Option<(String, String)> { |
| 11 | let value = headers.get(AUTHORIZATION)?.to_str().ok()?; |
| 12 | let (scheme, encoded) = value.split_once(' ')?; |
| 13 | if !scheme.eq_ignore_ascii_case("basic") || encoded.is_empty() { |
| 14 | return None; |
| 15 | } |
| 16 | let decoded = BASE64_STANDARD.decode(encoded).ok()?; |
| 17 | let text = String::from_utf8(decoded).ok()?; |
| 18 | let (username, password) = text.split_once(':')?; |
| 19 | Some((username.to_owned(), password.to_owned())) |
| 20 | } |
| 21 | |
| 22 | pub async fn user_from_basic( |
| 23 | state: &AppState, |
| 24 | headers: &HeaderMap, |
| 25 | ) -> Result<Option<User>, AccessError> { |
| 26 | if !headers.contains_key(AUTHORIZATION) { |
| 27 | return Ok(None); |
| 28 | } |
| 29 | let Some((username, token)) = parse_basic_auth(headers) else { |
| 30 | return Err(AccessError::Unauthorized); |
| 31 | }; |
| 32 | |
| 33 | let token_hash = hash_token(&token); |
| 34 | let access = state |
| 35 | .store |
| 36 | .token_by_hash(&token_hash) |
| 37 | .await |
| 38 | .map_err(internal)? |
| 39 | .ok_or(AccessError::Unauthorized)?; |
| 40 | let user = state |
| 41 | .store |
| 42 | .user_by_id(&access.user_id) |
| 43 | .await |
| 44 | .map_err(internal)? |
| 45 | .filter(|user| !user.is_org()) |
| 46 | .ok_or(AccessError::Unauthorized)?; |
| 47 | |
| 48 | let username_ok = username.eq_ignore_ascii_case(&user.username) |
| 49 | || TOKEN_ONLY_USERNAMES |
| 50 | .iter() |
| 51 | .any(|name| username.eq_ignore_ascii_case(name)); |
| 52 | if !username_ok { |
| 53 | return Err(AccessError::Unauthorized); |
| 54 | } |
| 55 | if !access |
| 56 | .scopes |
| 57 | .iter() |
| 58 | .any(|scope| scope == crate::api::REPO_SCOPE) |
| 59 | { |
| 60 | return Err(AccessError::Unauthorized); |
| 61 | } |
| 62 | |
| 63 | state |
| 64 | .store |
| 65 | .touch_token(&access.id) |
| 66 | .await |
| 67 | .map_err(internal)?; |
| 68 | crate::live::bump_account(state, &user.id); |
| 69 | Ok(Some(user)) |
| 70 | } |
| 71 | |
| 72 | fn internal(e: forge_core::Error) -> AccessError { |
| 73 | AccessError::Internal(e.to_string()) |
| 74 | } |
| 75 | |
| 76 | #[cfg(test)] |
| 77 | mod tests { |
| 78 | use super::*; |
| 79 | use http::HeaderValue; |
| 80 | |
| 81 | fn headers_with(value: &str) -> HeaderMap { |
| 82 | let mut headers = HeaderMap::new(); |
| 83 | headers.insert( |
| 84 | AUTHORIZATION, |
| 85 | HeaderValue::from_str(value).expect("valid header"), |
| 86 | ); |
| 87 | headers |
| 88 | } |
| 89 | |
| 90 | fn basic(plain: &str) -> String { |
| 91 | format!("Basic {}", BASE64_STANDARD.encode(plain)) |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn parse_basic_auth_cases() { |
| 96 | let t = BASE64_STANDARD.encode("tahinli:t"); |
| 97 | type Case<'a> = (&'a str, String, Option<(&'a str, &'a str)>); |
| 98 | let cases: Vec<Case<'_>> = vec![ |
| 99 | ( |
| 100 | "user and token", |
| 101 | basic("tahinli:forge_abc"), |
| 102 | Some(("tahinli", "forge_abc")), |
| 103 | ), |
| 104 | ( |
| 105 | "scheme is case insensitive", |
| 106 | format!("bAsIc {t}"), |
| 107 | Some(("tahinli", "t")), |
| 108 | ), |
| 109 | ( |
| 110 | "password may contain colons", |
| 111 | basic("tahinli:a:b:c"), |
| 112 | Some(("tahinli", "a:b:c")), |
| 113 | ), |
| 114 | ( |
| 115 | "empty password is parsed", |
| 116 | basic("tahinli:"), |
| 117 | Some(("tahinli", "")), |
| 118 | ), |
| 119 | ("wrong scheme", format!("Bearer {t}"), None), |
| 120 | ("missing colon", basic("tahinlitoken"), None), |
| 121 | ("bad base64", "Basic !!!not-base64!!!".to_owned(), None), |
| 122 | ("extra space", format!("Basic {t}"), None), |
| 123 | ("empty value", "Basic ".to_owned(), None), |
| 124 | ( |
| 125 | "non utf8 credentials", |
| 126 | format!( |
| 127 | "Basic {}", |
| 128 | BASE64_STANDARD.encode([0xff_u8, 0xfe, b':', b'x']) |
| 129 | ), |
| 130 | None, |
| 131 | ), |
| 132 | ]; |
| 133 | for (name, value, want) in cases { |
| 134 | let got = parse_basic_auth(&headers_with(&value)); |
| 135 | let want = want.map(|(user, pass)| (user.to_owned(), pass.to_owned())); |
| 136 | assert_eq!(got, want, "case {name}"); |
| 137 | } |
| 138 | assert_eq!(parse_basic_auth(&HeaderMap::new()), None, "case no header"); |
| 139 | } |
| 140 | } |