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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#![allow(unused_imports, dead_code)]
use serde::*;
use ate::prelude::*;

use crate::model::Advert;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LoginRequest
{
    pub email: String,
    pub secret: EncryptKey,
    pub code: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LoginResponse
{
    pub user_key: PrimaryKey,
    pub nominal_read: ate::crypto::Hash,
    pub nominal_write: PublicSignKey,
    pub sudo_read: ate::crypto::Hash,
    pub sudo_write: PublicSignKey,
    pub authority: AteSession
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum LoginFailed
{
    UserNotFound,
    WrongPassword,
    WrongCode,
    AccountLocked,
    Unverified,
    NoMasterKey,
}

impl std::fmt::Display
for LoginFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            LoginFailed::UserNotFound => {
                write!(f, "The user could not be found")
            },
            LoginFailed::WrongPassword => {
                write!(f, "The account password is incorrect")
            },
            LoginFailed::WrongCode => {
                write!(f, "The authenticator code is incorrect")
            },
            LoginFailed::AccountLocked => {
                write!(f, "The account is currently locked")
            },
            LoginFailed::Unverified => {
                write!(f, "The account has not yet been verified")
            },
            LoginFailed::NoMasterKey => {
                write!(f, "Authentication server has not been properly initialized")
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GatherRequest
{
    pub session: AteSession,
    pub group: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GatherResponse
{
    pub group_name: String,
    pub gid: u32,
    pub group_key: PrimaryKey,
    pub authority: AteSession
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum GatherFailed
{
    GroupNotFound,
    NoAccess,
    NoMasterKey,
}

impl std::fmt::Display
for GatherFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            GatherFailed::GroupNotFound => {
                write!(f, "The group could not be found")
            },
            GatherFailed::NoAccess => {
                write!(f, "No access available to this group")
            },
            GatherFailed::NoMasterKey => {
                write!(f, "Authentication server has not been properly initialized")
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CreateUserRequest
{
    pub auth: String,
    pub email: String,
    pub secret: EncryptKey,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CreateUserResponse
{
    pub key: PrimaryKey,
    pub qr_code: String,
    pub qr_secret: String,
    pub authority: AteSession
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum CreateUserFailed
{
    AlreadyExists,
    NoMoreRoom,
    NoMasterKey,
}

impl std::fmt::Display
for CreateUserFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            CreateUserFailed::AlreadyExists => {
                write!(f, "The user already exists")
            },
            CreateUserFailed::NoMoreRoom => {
                write!(f, "There is no more room for this user - try another name")
            },
            CreateUserFailed::NoMasterKey => {
                write!(f, "Authentication server has not been properly initialized")
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QueryRequest
{
    pub email: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QueryResponse
{
    pub advert: Advert,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum QueryFailed
{
    NotFound,
    Banned,
    Suspended,
}

impl std::fmt::Display
for QueryFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            QueryFailed::NotFound => {
                write!(f, "The account does not exist")
            },
            QueryFailed::Banned => {
                write!(f, "The account has been banned")
            },
            QueryFailed::Suspended => {
                write!(f, "The account has been suspended")
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CreateGroupRequest
{
    pub group: String,
    pub identity: String,
    pub nominal_read_key: PublicEncryptKey,
    pub sudo_read_key: PublicEncryptKey,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GroupUserAddRequest
{
    pub group: String,
    pub session: AteSession,
    pub who_key: PublicEncryptKey,
    pub who_name: String,
    pub purpose: AteRolePurpose
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GroupUserRemoveRequest
{
    pub group: String,
    pub session: AteSession,
    pub who: AteHash,
    pub purpose: AteRolePurpose
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GroupDetailsRequest
{
    pub group: String,
    pub session: Option<AteSession>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CreateGroupResponse
{
    pub key: PrimaryKey,
    pub session: AteSession,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GroupUserAddResponse
{
    pub key: PrimaryKey,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GroupUserRemoveResponse
{
    pub key: PrimaryKey,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GroupDetailsRoleResponse
{
    pub purpose: AteRolePurpose,
    pub name: String,
    pub read: AteHash,
    pub private_read: PublicEncryptKey,
    pub write: PublicSignKey,
    pub hidden: bool,
    pub members: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GroupDetailsResponse
{
    pub key: PrimaryKey,
    pub name: String,
    pub roles: Vec<GroupDetailsRoleResponse>,
    pub gid: u32,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum CreateGroupFailed
{
    AlreadyExists,
    NoMoreRoom,
    NoMasterKey,
    InvalidGroupName
}

impl std::fmt::Display
for CreateGroupFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            CreateGroupFailed::AlreadyExists => {
                write!(f, "The group already exists")
            },
            CreateGroupFailed::NoMoreRoom => {
                write!(f, "No more space for this group, try a different name")
            },
            CreateGroupFailed::NoMasterKey => {
                write!(f, "Authentication server has not been properly initialized")
            },
            CreateGroupFailed::InvalidGroupName => {
                write!(f, "The specified group name is not supported")
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum GroupDetailsFailed
{
    GroupNotFound,
    NoMasterKey,
    NoAccess,
}

impl std::fmt::Display
for GroupDetailsFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            GroupDetailsFailed::GroupNotFound => {
                write!(f, "The group does not exist")
            },
            GroupDetailsFailed::NoMasterKey => {
                write!(f, "Authentication server has not been properly initialized")
            },
            GroupDetailsFailed::NoAccess => {
                write!(f, "Access to this group is denied")
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum GroupUserAddFailed
{
    GroupNotFound,
    NoMasterKey,
    NoAccess,
    UnknownIdentity
}

impl std::fmt::Display
for GroupUserAddFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            GroupUserAddFailed::GroupNotFound => {
                write!(f, "The group does not exist")
            },
            GroupUserAddFailed::NoAccess => {
                write!(f, "The referrer does not have access to this group")
            },
            GroupUserAddFailed::NoMasterKey => {
                write!(f, "Authentication server has not been properly initialized")
            },
            GroupUserAddFailed::UnknownIdentity => {
                write!(f, "Ther referrers identity is not known")
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum GroupUserRemoveFailed
{
    GroupNotFound,
    RoleNotFound,
    NothingToRemove,
    NoMasterKey,
    NoAccess
}

impl std::fmt::Display
for GroupUserRemoveFailed {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            GroupUserRemoveFailed::GroupNotFound => {
                write!(f, "The group does not exist")
            },
            GroupUserRemoveFailed::RoleNotFound => {
                write!(f, "The group role does not exist")
            },
            GroupUserRemoveFailed::NothingToRemove => {
                write!(f, "The user is not a member of the group")
            },
            GroupUserRemoveFailed::NoAccess => {
                write!(f, "The referrer does not have access to this group")
            },
            GroupUserRemoveFailed::NoMasterKey => {
                write!(f, "Authentication server has not been properly initialized")
            },
        }
    }
}