jade_testing/
auth.rs

1//! Authorization related stuffs
2
3use crate::Jam;
4use service::{OpaqueHash, ServiceId, service::ServiceAccount};
5
6/// Authorization related stuffs
7#[derive(Default)]
8pub struct Auth {
9    /// The authorization token
10    pub token: Vec<u8>,
11
12    /// The authorization host
13    pub host: ServiceId,
14
15    /// The auth code hash
16    pub code_hash: OpaqueHash,
17
18    /// The authorizer config
19    pub config: Vec<u8>,
20}
21
22impl Auth {
23    /// Set the authorization token
24    pub fn with_token(mut self, token: Vec<u8>) -> Self {
25        self.token = token;
26        self
27    }
28
29    /// Set the authorizer
30    pub fn with_authorizer(mut self, service: ServiceId, code: OpaqueHash) -> Self {
31        self.host = service;
32        self.code_hash = code;
33        self
34    }
35
36    /// Set the authorizer config
37    pub fn with_config(mut self, config: Vec<u8>) -> Self {
38        self.config = config;
39        self
40    }
41}
42
43impl Jam {
44    /// Set the authorization
45    pub fn with_auth(mut self, service: ServiceId, code: Vec<u8>) -> Self {
46        let mut auth = ServiceAccount::default();
47        auth.info.balance = 1000;
48        auth.info.creation = self.chain.best.slot;
49
50        // register the service account
51        self.add_account(service, auth);
52        let hash = self.add_preimage(service, code);
53
54        // set the code hash
55        if let Some(account) = self.chain.accounts.get_mut(&service) {
56            account.info.code = hash;
57        }
58
59        self.auth.code_hash = hash;
60        self.auth.host = service;
61        self
62    }
63
64    /// Set the authorization token
65    pub fn with_auth_token(mut self, token: Vec<u8>) -> Self {
66        self.auth.token = token;
67        self
68    }
69
70    /// Set the authorizer config
71    pub fn with_auth_config(mut self, config: Vec<u8>) -> Self {
72        self.auth.config = config;
73        self
74    }
75
76    /// Set the authorization
77    pub fn with_authorizer(mut self, auth: Auth) -> Self {
78        self.auth = auth;
79        self
80    }
81}