oxedyne/fe2o3/fe2o3_steel/src/srv/member/mod.rs
3.3 KiB, 6 runs
created by r1870400018:72142, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! What a site keeps for its signed-in members, as opposed to its operators (`srv::admin`). |
| 2 | //! |
| 3 | //! `session` and `invite` share one secret form: 32 random bytes the member holds, of which the |
| 4 | //! store keeps only the SHA3-256 hash, so a read of the store yields nothing that can be presented. |
| 5 | |
| 6 | pub mod invite; |
| 7 | pub mod session; |
| 8 | |
| 9 | use oxedyne_fe2o3_core::{ |
| 10 | prelude::*, |
| 11 | rand::Rand, |
| 12 | }; |
| 13 | use oxedyne_fe2o3_hash::hash::HashScheme; |
| 14 | use oxedyne_fe2o3_iop_hash::api::Hasher; |
| 15 | use oxedyne_fe2o3_text::base2x; |
| 16 | |
| 17 | use std::{ |
| 18 | fmt, |
| 19 | sync::{ |
| 20 | atomic::{ |
| 21 | AtomicU64, |
| 22 | Ordering, |
| 23 | }, |
| 24 | Arc, |
| 25 | }, |
| 26 | time::{ |
| 27 | SystemTime, |
| 28 | UNIX_EPOCH, |
| 29 | }, |
| 30 | }; |
| 31 | |
| 32 | pub const TOKEN_LEN: usize = 32; // bytes |
| 33 | |
| 34 | type Hash32 = [u8; TOKEN_LEN]; |
| 35 | |
| 36 | /// The secret a member holds, in a cookie for a session or in a link for an invite. It never |
| 37 | /// prints: neither `Debug` nor any formatting shows its bytes, and only `to_hex` gives them out. |
| 38 | pub struct Token(Hash32); |
| 39 | |
| 40 | impl Token { |
| 41 | fn generate() -> Self { |
| 42 | let mut b = [0u8; TOKEN_LEN]; |
| 43 | Rand::fill_u8(&mut b); |
| 44 | Self(b) |
| 45 | } |
| 46 | |
| 47 | /// An app's pre-existing secret, taken as the token, for a session `Sessions::adopt` moves |
| 48 | /// across from a store that held it raw. The bytes are used as they are, so the same secret |
| 49 | /// always names the same session. |
| 50 | pub fn from_secret(b: [u8; TOKEN_LEN]) -> Self { |
| 51 | Self(b) |
| 52 | } |
| 53 | |
| 54 | pub fn to_hex(&self) -> String { |
| 55 | hex(&self.0) |
| 56 | } |
| 57 | |
| 58 | pub fn from_hex(s: &str) -> Outcome<Self> { |
| 59 | if s.len() != TOKEN_LEN * 2 { |
| 60 | return Err(err!( |
| 61 | "A member token must be {} hex characters, this one has {}.", |
| 62 | TOKEN_LEN * 2, s.len(); |
| 63 | Invalid, Input)); |
| 64 | } |
| 65 | let v = res!(base2x::HEX.from_str(&s.to_ascii_uppercase())); // either case |
| 66 | let b: Hash32 = res!(v.try_into().map_err(|_| err!( |
| 67 | "A member token did not decode to {} bytes.", TOKEN_LEN; |
| 68 | Invalid, Input))); |
| 69 | Ok(Self(b)) |
| 70 | } |
| 71 | |
| 72 | fn hash(&self) -> Outcome<Hash32> { |
| 73 | let h = HashScheme::new_sha3_256().hash(&[&self.0], []); |
| 74 | let v = h.as_hashform().as_vec(); |
| 75 | let b: Hash32 = res!(v.try_into().map_err(|_| err!( |
| 76 | "A SHA3-256 hash was not {} bytes.", TOKEN_LEN; |
| 77 | Unexpected, Size))); |
| 78 | Ok(b) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl fmt::Debug for Token { |
| 83 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 84 | write!(f, "Token(..)") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Where a member store reads the time. A test steps a `Manual` clock to cross an expiry. |
| 89 | #[derive(Clone, Debug)] |
| 90 | pub enum Clock { |
| 91 | System, |
| 92 | Manual(Arc<AtomicU64>), // Unix seconds |
| 93 | } |
| 94 | |
| 95 | impl Clock { |
| 96 | pub fn now(&self) -> Outcome<u64> { |
| 97 | match self { |
| 98 | Self::System => Ok(res!(SystemTime::now().duration_since(UNIX_EPOCH), |
| 99 | System, Invalid).as_secs()), |
| 100 | Self::Manual(t) => Ok(t.load(Ordering::SeqCst)), |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn hex(b: &[u8]) -> String { |
| 106 | base2x::HEX.to_string(b) |
| 107 | } |
| 108 | |
| 109 | fn hash_of_key(s: &str) -> Option<Hash32> { |
| 110 | if s.len() != TOKEN_LEN * 2 { |
| 111 | return None; |
| 112 | } |
| 113 | match base2x::HEX.from_str(&s.to_ascii_uppercase()) { |
| 114 | Ok(v) => v.try_into().ok(), |
| 115 | Err(_) => None, |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | fn short(hash: &Hash32) -> String { |
| 120 | let mut s = hex(hash); |
| 121 | s.truncate(8); |
| 122 | s |
| 123 | } |