| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use subtle::ConstantTimeEq; |
| 8 | use topcoat::{ |
| 9 | context::{Cx, app_context}, |
| 10 | router::{header::AUTHORIZATION, headers}, |
| 11 | }; |
| 12 | |
| 13 | use crate::application::AppConfig; |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | pub fn operator_authorized(cx: &Cx) -> bool { |
| 20 | let Some(expected) = app_context::<AppConfig>(cx) |
| 21 | .operator_token |
| 22 | .as_ref() |
| 23 | .map(crate::application::Secret::expose) |
| 24 | else { |
| 25 | return false; |
| 26 | }; |
| 27 | let Some(presented) = bearer(cx) else { |
| 28 | return false; |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | presented.as_bytes().ct_eq(expected.as_bytes()).into() |
| 33 | } |
| 34 | |
| 35 | fn bearer(cx: &Cx) -> Option<String> { |
| 36 | let header = headers(cx).get(AUTHORIZATION)?.to_str().ok()?; |
| 37 | let token = header.strip_prefix("Bearer ")?.trim(); |
| 38 | (!token.is_empty()).then(|| token.to_owned()) |
| 39 | } |