(
create_session_dto: CreateSessionDTO,
ctx: Arc<AppContext>,
)
| 16 | const TOKEN_LIFETIME: u64 = 3600; // 1 hour |
| 17 | |
| 18 | pub(crate) async fn create_session( |
| 19 | create_session_dto: CreateSessionDTO, |
| 20 | ctx: Arc<AppContext>, |
| 21 | ) -> Result<Session, AuthError> { |
| 22 | let user = ctx |
| 23 | .ain_env |
| 24 | .users_map |
| 25 | .get_user(&create_session_dto.username) |
| 26 | .ok_or(AuthError::WrongCredentials)?; |
| 27 | let password_hash = SingleSHA256Hash::from_str(&create_session_dto.password).unwrap(); |
| 28 | let password_double_hash = password_hash.hash_again(); |
| 29 | // check if passwords match, in constant time to prevents timing attacks |
| 30 | if !password_double_hash.verify_eq(&user.password_hash) { |
| 31 | return Err(AuthError::WrongCredentials)?; |
| 32 | } |
| 33 | |
| 34 | let (access_token, timestamp) = crypto::create_session( |
| 35 | &create_session_dto.username, |
| 36 | &ctx.ain_env.admin_key, |
| 37 | &password_hash, |
| 38 | ); |
| 39 | |
| 40 | let created_at = timestamp; |
| 41 | let expires_at = timestamp + TOKEN_LIFETIME; |
| 42 | |
| 43 | ctx.ain_env.active_sessions.insert( |
| 44 | access_token.clone(), |
| 45 | SessionDetails { |
| 46 | created_at, |
| 47 | expires_at, |
| 48 | user, |
| 49 | }, |
| 50 | ); |
| 51 | |
| 52 | Ok(Session { |
| 53 | access_token, |
| 54 | created_at, |
| 55 | expires_at, |
| 56 | }) |
| 57 | } |
nothing calls this directly
no test coverage detected