1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use bitcoin_hashes::sha256::Hash as Sha256Hash;
use bitcoin_hashes::Hash;
use secp256k1::ecdh::SharedSecret;
use secp256k1::{PublicKey, SecretKey};

/// A non-priviledged authentication token.
/// This token cannot be used for HMAC operations, so is suitable
/// for an untrusted proxy.
pub struct Auth {
    /// Client pubkey
    pub client_id: PublicKey,
    /// SHA256 of ECDH of client and server keys
    pub token: Vec<u8>,
}

impl Auth {
    pub fn auth_token(&self) -> Vec<u8> {
        self.token.clone()
    }
}

/// A priviledged authentication token.
/// This allows authentication to the server and also performing HMAC operations.
/// For non-privileged access, use [`Auth`].
#[derive(Clone)]
pub struct PrivAuth {
    /// Client pubkey
    pub client_id: PublicKey,
    /// ECDH of client and server keys
    pub shared_secret: Vec<u8>,
}

impl PrivAuth {
    pub fn new_for_client(client_key: &SecretKey, server_id: &PublicKey) -> Self {
        let secp = secp256k1::Secp256k1::new();
        let shared_secret = SharedSecret::new(server_id, client_key).secret_bytes().to_vec();
        let client_id = PublicKey::from_secret_key(&secp, client_key);
        Self { client_id, shared_secret }
    }

    pub fn new_for_server(server_key: &SecretKey, client_id: &PublicKey) -> Self {
        let shared_secret = SharedSecret::new(client_id, server_key).secret_bytes().to_vec();
        Self { client_id: client_id.clone(), shared_secret }
    }

    /// Unpriviledged authentication token
    pub fn auth(&self) -> Auth {
        let token = self.auth_token();
        Auth { client_id: self.client_id.clone(), token }
    }

    /// SHA256 of shared_secret
    pub fn auth_token(&self) -> Vec<u8> {
        Sha256Hash::hash(&self.shared_secret).to_vec()
    }
}