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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Types related to server operations and the protocols they execute.

pub mod authenticate;
pub mod create_storage_key;
pub mod delete_key;
pub mod generate;
pub mod get_user_id;
pub mod import;
pub mod logout;
pub mod register;
pub mod remote_generate;
pub mod remote_sign_bytes;
pub mod retrieve_audit_events;
pub mod retrieve_secret;
pub mod retrieve_server_encrypted_blob;
pub mod retrieve_storage_key;
pub mod store_server_encrypted_blob;

use crate::{types::database::account::AccountName, LockKeeperError};
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter, EnumString};
use tonic::metadata::{Ascii, MetadataValue};
use uuid::Uuid;

use super::Message;

/// Options for actions the Lock Keeper client can take.
/// We explicitly assign discriminant values to this type, as it will be stored
/// in the our database.
#[derive(
    Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Display, EnumIter, EnumString, Hash,
)]
pub enum ClientAction {
    Authenticate = 0,
    CreateStorageKey = 1,
    ExportSecret = 2,
    ExportSigningKey = 3,
    GenerateSecret = 4,
    GetUserId = 5,
    ImportSigningKey = 6,
    Logout = 7,
    Register = 8,
    RemoteGenerateSigningKey = 9,
    RemoteSignBytes = 10,
    RetrieveSecret = 11,
    RetrieveAuditEvents = 12,
    RetrieveSigningKey = 13,
    RetrieveStorageKey = 14,
    RetrieveServerEncryptedBlob = 15,
    StoreServerEncryptedBlob = 16,
    CheckSession = 17,
    DeleteKey = 18,
}

impl TryFrom<i64> for ClientAction {
    type Error = i64;

    fn try_from(v: i64) -> Result<Self, Self::Error> {
        match v {
            x if x == ClientAction::Authenticate as i64 => Ok(ClientAction::Authenticate),
            x if x == ClientAction::CreateStorageKey as i64 => Ok(ClientAction::CreateStorageKey),
            x if x == ClientAction::ExportSecret as i64 => Ok(ClientAction::ExportSecret),
            x if x == ClientAction::ExportSigningKey as i64 => Ok(ClientAction::ExportSigningKey),
            x if x == ClientAction::GenerateSecret as i64 => Ok(ClientAction::GenerateSecret),
            x if x == ClientAction::GetUserId as i64 => Ok(ClientAction::GetUserId),
            x if x == ClientAction::ImportSigningKey as i64 => Ok(ClientAction::ImportSigningKey),
            x if x == ClientAction::Logout as i64 => Ok(ClientAction::Logout),
            x if x == ClientAction::Register as i64 => Ok(ClientAction::Register),
            x if x == ClientAction::RemoteGenerateSigningKey as i64 => {
                Ok(ClientAction::RemoteGenerateSigningKey)
            }
            x if x == ClientAction::RemoteSignBytes as i64 => Ok(ClientAction::RemoteSignBytes),
            x if x == ClientAction::RetrieveServerEncryptedBlob as i64 => {
                Ok(ClientAction::RetrieveServerEncryptedBlob)
            }
            x if x == ClientAction::RetrieveSecret as i64 => Ok(ClientAction::RetrieveSecret),
            x if x == ClientAction::RetrieveAuditEvents as i64 => {
                Ok(ClientAction::RetrieveAuditEvents)
            }
            x if x == ClientAction::RetrieveSigningKey as i64 => {
                Ok(ClientAction::RetrieveSigningKey)
            }
            x if x == ClientAction::RetrieveStorageKey as i64 => {
                Ok(ClientAction::RetrieveStorageKey)
            }
            x if x == ClientAction::StoreServerEncryptedBlob as i64 => {
                Ok(ClientAction::StoreServerEncryptedBlob)
            }
            x if x == ClientAction::CheckSession as i64 => Ok(ClientAction::CheckSession),
            x if x == ClientAction::DeleteKey as i64 => Ok(ClientAction::DeleteKey),
            // Return value of offending integer.
            _ => Err(v),
        }
    }
}

/// Converts a serializable Rust type to and from the RPC [`Message`] type.
pub trait ConvertMessage: Sized + for<'a> Deserialize<'a> + Serialize {
    fn from_message(value: Message) -> Result<Self, LockKeeperError> {
        Ok(serde_json::from_slice(&value.content)?)
    }

    fn to_message(self) -> Result<Message, LockKeeperError> {
        let content = serde_json::to_vec(&self)?;
        Ok(Message { content })
    }
}

// Implements `ConvertMessage` for all types that implement `Serialize` and
// `Deserialize`.
impl<T: for<'a> Deserialize<'a> + Serialize> ConvertMessage for T {}

/// Metadata attached to each request to the server. Note that the request ID is
/// an ID for an entire operation, not each `ClientAction` that the operation is
/// composed of.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RequestMetadata {
    account_name: AccountName,
    action: ClientAction,
    session_id: Option<Uuid>,
    request_id: Uuid,
}

impl RequestMetadata {
    pub fn new(
        account_name: &AccountName,
        action: ClientAction,
        session_id: Option<&Uuid>,
        request_id: Uuid,
    ) -> Self {
        Self {
            account_name: account_name.clone(),
            action,
            session_id: session_id.cloned(),
            request_id,
        }
    }

    pub fn account_name(&self) -> &AccountName {
        &self.account_name
    }

    pub fn action(&self) -> ClientAction {
        self.action
    }

    pub fn request_id(&self) -> Uuid {
        self.request_id
    }

    pub fn session_id(&self) -> Option<&Uuid> {
        self.session_id.as_ref()
    }
}

impl TryFrom<&RequestMetadata> for MetadataValue<Ascii> {
    type Error = LockKeeperError;

    fn try_from(value: &RequestMetadata) -> Result<Self, Self::Error> {
        let bytes = serde_json::to_vec(value)?;
        Ok(MetadataValue::try_from(bytes)?)
    }
}

impl TryFrom<&MetadataValue<Ascii>> for RequestMetadata {
    type Error = LockKeeperError;

    fn try_from(value: &MetadataValue<Ascii>) -> Result<Self, Self::Error> {
        let bytes = value.as_bytes();
        let metadata = serde_json::from_slice(bytes)?;
        Ok(metadata)
    }
}

#[cfg(test)]
mod test {
    use crate::types::operations::ClientAction;
    use strum::IntoEnumIterator;

    /// Ensure our conversion function covers all cases.
    #[test]
    fn client_action_conversion_exhaustive() {
        for action in ClientAction::iter() {
            assert_eq!(action, ClientAction::try_from(action as i64).unwrap());
        }
    }
}