MCPcopy Create free account
hub / github.com/cosdata/cosdata / create_session

Function create_session

src/api/auth/service.rs:18–57  ·  view source on GitHub ↗
(
    create_session_dto: CreateSessionDTO,
    ctx: Arc<AppContext>,
)

Source from the content-addressed store, hash-verified

16const TOKEN_LIFETIME: u64 = 3600; // 1 hour
17
18pub(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}

Callers

nothing calls this directly

Calls 5

get_userMethod · 0.80
hash_againMethod · 0.80
verify_eqMethod · 0.80
cloneMethod · 0.80
insertMethod · 0.45

Tested by

no test coverage detected