smart_account_auth/messages/
sessions.rs

1use saa_schema::saa_type;
2use saa_common::{Binary, CredentialId, Expiration, Vec, String, vec, hashes::sha256};
3use crate::{credential::CredentialRecord, msgs::{Action, AllowedActions, DerivableMsg}};
4
5
6type GranteeInfo = CredentialRecord;
7
8
9#[saa_type]
10pub struct SessionInfo  {
11    pub grantee     :       GranteeInfo,
12    pub granter     :       Option<CredentialId>,
13    pub expiration  :       Option<Expiration>,
14}
15
16
17
18#[saa_type]
19pub struct Session {
20    pub granter     : CredentialId,
21    pub grantee     : GranteeInfo,
22    pub actions     : AllowedActions, 
23    pub expiration  : Expiration,
24    #[cfg(feature = "replay")]
25    pub nonce       : u64,
26}
27
28
29
30
31impl Session {
32
33    pub fn key(&self) -> CredentialId {
34        let (id, info) = &self.grantee;
35        
36        let actions  = match self.actions {
37            AllowedActions::All {  } => vec![],
38            AllowedActions::Include(ref actions) => {
39                actions.iter().map(|a| a.to_string())
40                    .collect::<Vec<String>>()
41                    .join(",")
42                    .as_bytes()
43                    .to_vec()
44            }
45        };
46
47        Binary::from(
48            sha256(
49            &[
50                    self.granter.as_bytes(),
51                    id.as_bytes(),
52                    info.name.to_string().as_bytes(),
53                    actions.as_slice()
54                ]
55                .concat()
56            )
57        ).to_base64()
58    }
59
60    pub fn can_do_action(&self, act: &Action) -> bool {
61        self.actions.can_do_action(act)
62    }
63    
64    pub fn can_do_msg<M : DerivableMsg>(&self, message: &M) -> bool {
65        self.actions.can_do_msg(message)
66    }
67}
68
69