sentc_crypto_common/
user.rs

1use alloc::string::String;
2use alloc::vec::Vec;
3
4use serde::{Deserialize, Serialize};
5use serde_json::{from_str, to_string};
6
7use crate::group::{CreateData, GroupHmacData, GroupInviteReqList, GroupKeyServerOutput, GroupKeysForNewMemberServerInput, GroupNewMemberLightInput};
8use crate::{DeviceId, EncryptionKeyPairId, GroupId, SignKeyPairId, SymKeyId, UserId};
9
10#[derive(Serialize, Deserialize)]
11pub struct CaptchaCreateOutput
12{
13	pub captcha_id: String,
14	pub png: String, //base64 encoded
15}
16
17#[derive(Serialize, Deserialize)]
18pub struct CaptchaInput
19{
20	pub captcha_solution: String,
21	pub captcha_id: String,
22}
23
24#[derive(Serialize, Deserialize)]
25pub struct MasterKey
26{
27	pub master_key_alg: String,
28	pub encrypted_master_key: String, //base64 encoded master key
29	pub encrypted_master_key_alg: String,
30}
31
32#[derive(Serialize, Deserialize)]
33pub struct KeyDerivedData
34{
35	pub derived_alg: String,
36	pub client_random_value: String, //don't use the enum for out, we will get the enum form the derived alg on the server (because the rand value is only used on the server)
37	pub hashed_authentication_key: String,
38
39	//pub/pri encrypt decrypt
40	pub public_key: String,
41	pub encrypted_private_key: String,
42	pub keypair_encrypt_alg: String,
43
44	//sign/verify
45	pub verify_key: String,
46	pub encrypted_sign_key: String,
47	pub keypair_sign_alg: String,
48}
49
50#[derive(Serialize, Deserialize)]
51pub struct UserIdentifierAvailableServerInput
52{
53	pub user_identifier: String,
54}
55
56impl UserIdentifierAvailableServerInput
57{
58	pub fn from_string(v: &str) -> serde_json::Result<Self>
59	{
60		from_str::<Self>(v)
61	}
62
63	pub fn to_string(&self) -> serde_json::Result<String>
64	{
65		to_string(self)
66	}
67}
68
69#[derive(Serialize, Deserialize)]
70pub struct UserIdentifierAvailableServerOutput
71{
72	pub user_identifier: String,
73	pub available: bool,
74}
75
76impl UserIdentifierAvailableServerOutput
77{
78	pub fn from_string(v: &str) -> serde_json::Result<Self>
79	{
80		from_str::<Self>(v)
81	}
82
83	pub fn to_string(&self) -> serde_json::Result<String>
84	{
85		to_string(self)
86	}
87}
88
89#[derive(Serialize, Deserialize)]
90pub struct UserDeviceRegisterInput
91{
92	pub master_key: MasterKey,
93	pub derived: KeyDerivedData,
94	pub device_identifier: String, //with this the user is called for login. can be username or an id, or an email
95}
96
97#[derive(Serialize, Deserialize)]
98pub struct UserDeviceRegisterOutput
99{
100	pub device_id: DeviceId, //device id is also the public key id (keypair_encrypt_id)
101	pub token: String,
102	pub device_identifier: String,
103	pub public_key_string: String,
104	pub keypair_encrypt_alg: String,
105}
106
107#[derive(Serialize, Deserialize)]
108pub struct UserDeviceDoneRegisterInputLight
109{
110	pub user_group: GroupNewMemberLightInput,
111	pub token: String,
112}
113
114#[derive(Serialize, Deserialize)]
115pub struct UserDeviceDoneRegisterInput
116{
117	pub user_keys: GroupKeysForNewMemberServerInput,
118	pub token: String,
119}
120
121/**
122# Register Data for the server api
123
124send this after register to the server
125*/
126#[derive(Serialize, Deserialize)]
127pub struct RegisterData
128{
129	pub device: UserDeviceRegisterInput, //the first device of the user
130	pub group: CreateData,               //the user group data
131}
132
133impl RegisterData
134{
135	pub fn from_string(v: &str) -> serde_json::Result<Self>
136	{
137		from_str::<Self>(v)
138	}
139
140	pub fn to_string(&self) -> serde_json::Result<String>
141	{
142		to_string(self)
143	}
144}
145
146#[derive(Serialize, Deserialize)]
147pub struct RegisterServerOutput
148{
149	pub user_id: UserId,
150	pub device_id: DeviceId,
151	pub device_identifier: String,
152}
153
154impl RegisterServerOutput
155{
156	pub fn from_string(v: &str) -> serde_json::Result<Self>
157	{
158		from_str::<Self>(v)
159	}
160
161	pub fn to_string(&self) -> serde_json::Result<String>
162	{
163		to_string(self)
164	}
165}
166
167#[derive(Serialize, Deserialize)]
168pub struct ChangePasswordData
169{
170	pub new_derived_alg: String,
171	pub new_client_random_value: String,
172	pub new_hashed_authentication_key: String,
173	pub new_encrypted_master_key: String,
174	pub new_encrypted_master_key_alg: String,
175	pub old_auth_key: String,
176}
177
178impl ChangePasswordData
179{
180	pub fn from_string(v: &str) -> serde_json::Result<Self>
181	{
182		from_str::<Self>(v)
183	}
184
185	pub fn to_string(&self) -> serde_json::Result<String>
186	{
187		to_string(self)
188	}
189}
190
191#[derive(Serialize, Deserialize)]
192pub struct ResetPasswordData
193{
194	pub client_random_value: String, //don't use the enum for out, we will get the enum form the derived alg on the server (because the rand value is only used on the server)
195	pub hashed_authentication_key: String,
196	pub master_key: MasterKey,
197	pub derived_alg: String,
198	pub encrypted_private_key: String,
199	pub encrypted_sign_key: String,
200}
201
202impl ResetPasswordData
203{
204	pub fn to_string(&self) -> serde_json::Result<String>
205	{
206		to_string(self)
207	}
208
209	pub fn from_string(v: &str) -> serde_json::Result<Self>
210	{
211		from_str::<Self>(v)
212	}
213}
214
215#[derive(Serialize, Deserialize)]
216pub struct UserPublicKeyData
217{
218	pub public_key_pem: String,
219	pub public_key_alg: String,
220	pub public_key_id: EncryptionKeyPairId,
221	pub public_key_sig: Option<String>,
222	pub public_key_sig_key_id: Option<String>,
223}
224
225impl UserPublicKeyData
226{
227	pub fn from_string(v: &str) -> serde_json::Result<Self>
228	{
229		from_str::<Self>(v)
230	}
231
232	pub fn to_string(&self) -> serde_json::Result<String>
233	{
234		to_string(self)
235	}
236}
237
238#[derive(Serialize, Deserialize)]
239pub struct UserVerifyKeyData
240{
241	pub verify_key_pem: String,
242	pub verify_key_alg: String,
243	pub verify_key_id: SignKeyPairId,
244}
245
246impl UserVerifyKeyData
247{
248	pub fn from_string(v: &str) -> serde_json::Result<Self>
249	{
250		from_str::<Self>(v)
251	}
252
253	pub fn to_string(&self) -> serde_json::Result<String>
254	{
255		to_string(self)
256	}
257}
258
259#[derive(Serialize, Deserialize)]
260pub struct UserPublicKeyDataServerOutput
261{
262	pub public_key_id: EncryptionKeyPairId,
263	pub public_key: String,
264	pub public_key_alg: String,
265	pub public_key_sig: Option<String>,
266	pub public_key_sig_key_id: Option<String>,
267}
268
269impl UserPublicKeyDataServerOutput
270{
271	pub fn from_string(v: &str) -> serde_json::Result<Self>
272	{
273		from_str::<Self>(v)
274	}
275
276	pub fn to_string(&self) -> serde_json::Result<String>
277	{
278		to_string(self)
279	}
280}
281
282#[derive(Serialize, Deserialize)]
283pub struct UserVerifyKeyDataServerOutput
284{
285	pub verify_key_id: EncryptionKeyPairId,
286	pub verify_key: String,
287	pub verify_key_alg: String,
288}
289
290impl UserVerifyKeyDataServerOutput
291{
292	pub fn from_string(v: &str) -> serde_json::Result<Self>
293	{
294		from_str::<Self>(v)
295	}
296
297	pub fn to_string(&self) -> serde_json::Result<String>
298	{
299		to_string(self)
300	}
301}
302
303#[derive(Serialize, Deserialize)]
304pub struct PrepareLoginServerInput
305{
306	pub user_identifier: String,
307}
308
309impl PrepareLoginServerInput
310{
311	pub fn to_string(&self) -> serde_json::Result<String>
312	{
313		to_string(self)
314	}
315
316	pub fn from_string(v: &str) -> serde_json::Result<Self>
317	{
318		from_str::<Self>(v)
319	}
320}
321
322#[derive(Serialize, Deserialize)]
323pub struct PrepareLoginSaltServerOutput
324{
325	pub salt_string: String,
326	pub derived_encryption_key_alg: String,
327}
328
329impl PrepareLoginSaltServerOutput
330{
331	pub fn to_string(&self) -> serde_json::Result<String>
332	{
333		to_string(self)
334	}
335
336	pub fn from_string(v: &str) -> serde_json::Result<Self>
337	{
338		from_str::<Self>(v)
339	}
340}
341
342#[derive(Serialize, Deserialize)]
343pub struct DoneLoginServerInput
344{
345	pub auth_key: String,
346	pub device_identifier: String,
347}
348
349impl DoneLoginServerInput
350{
351	pub fn to_string(&self) -> serde_json::Result<String>
352	{
353		to_string(self)
354	}
355
356	pub fn from_string(v: &str) -> serde_json::Result<Self>
357	{
358		from_str::<Self>(v)
359	}
360}
361
362#[derive(Serialize, Deserialize, Clone)]
363pub struct DoneLoginServerOutput
364{
365	pub device_keys: DoneLoginServerKeysOutput,
366	pub challenge: String,
367}
368
369#[allow(clippy::large_enum_variant)]
370#[derive(Serialize, Deserialize)]
371pub enum DoneLoginServerReturn
372{
373	Otp,
374	Direct(DoneLoginServerOutput),
375}
376
377//as base64 encoded string from the server
378#[derive(Serialize, Deserialize, Clone)]
379pub struct DoneLoginServerKeysOutput
380{
381	pub encrypted_master_key: String,
382	pub encrypted_private_key: String,
383	pub public_key_string: String,
384	pub keypair_encrypt_alg: String,
385	pub encrypted_sign_key: String,
386	pub verify_key_string: String,
387	pub keypair_sign_alg: String,
388	pub keypair_encrypt_id: EncryptionKeyPairId,
389	pub keypair_sign_id: SignKeyPairId,
390	pub user_id: UserId,
391	pub device_id: DeviceId,
392	pub user_group_id: GroupId,
393}
394
395impl DoneLoginServerKeysOutput
396{
397	pub fn to_string(&self) -> serde_json::Result<String>
398	{
399		to_string(self)
400	}
401
402	pub fn from_string(v: &str) -> serde_json::Result<Self>
403	{
404		from_str::<Self>(v)
405	}
406}
407
408#[derive(Serialize, Deserialize)]
409pub struct VerifyLoginInput
410{
411	pub auth_key: String,
412	pub device_identifier: String,
413	pub challenge: String,
414}
415
416#[derive(Serialize, Deserialize)]
417pub struct VerifyLoginOutput
418{
419	pub user_keys: Vec<GroupKeyServerOutput>,
420	pub hmac_keys: Vec<GroupHmacData>,
421	pub jwt: String,
422	pub refresh_token: String,
423}
424
425#[derive(Serialize, Deserialize)]
426pub struct VerifyLoginLightOutput
427{
428	pub jwt: String,
429	pub refresh_token: String,
430}
431
432#[derive(Serialize, Deserialize)]
433pub struct UserForcedAction
434{
435	pub user_identifier: String,
436}
437
438#[derive(Serialize, Deserialize)]
439pub struct VerifyLoginForcedEntity
440{
441	pub device_id: DeviceId,
442	pub user_id: UserId,
443	pub user_group_id: GroupId,
444
445	pub encrypted_master_key: String,
446	pub encrypted_private_key: String,
447	pub public_key_string: String,
448	pub keypair_encrypt_alg: String,
449	pub encrypted_sign_key: String,
450	pub verify_key_string: String,
451	pub keypair_sign_alg: String,
452}
453
454#[derive(Serialize, Deserialize)]
455pub struct LoginForcedOutput
456{
457	pub device_keys: VerifyLoginForcedEntity,
458	pub verify: VerifyLoginOutput,
459}
460
461#[derive(Serialize, Deserialize)]
462pub struct LoginForcedLightOutput
463{
464	pub device_keys: VerifyLoginForcedEntity,
465	pub verify: VerifyLoginLightOutput,
466}
467
468/**
469For refresh jwt
470*/
471#[derive(Serialize, Deserialize)]
472pub struct DoneLoginLightServerOutput
473{
474	pub user_id: UserId,
475	pub jwt: String,
476	pub device_id: DeviceId,
477}
478
479#[derive(Serialize, Deserialize)]
480pub struct UserInitServerOutput
481{
482	pub jwt: String,
483	pub invites: Vec<GroupInviteReqList>,
484}
485
486#[derive(Serialize, Deserialize)]
487pub struct JwtRefreshInput
488{
489	pub refresh_token: String,
490}
491
492impl JwtRefreshInput
493{
494	pub fn to_string(&self) -> serde_json::Result<String>
495	{
496		to_string(self)
497	}
498
499	pub fn from_string(v: &str) -> serde_json::Result<Self>
500	{
501		from_str::<Self>(v)
502	}
503}
504
505#[derive(Serialize, Deserialize)]
506pub struct PrepareLoginForKeyUpdateServerOutput
507{
508	pub client_random_value: String, //instead of prepare login (where the server creates the salt), for key update the client creates the salt for the old keys
509	pub derived_encryption_key_alg: String,
510	pub key_id: SymKeyId,
511}
512
513impl PrepareLoginForKeyUpdateServerOutput
514{
515	pub fn to_string(&self) -> serde_json::Result<String>
516	{
517		to_string(self)
518	}
519
520	pub fn from_string(v: &str) -> serde_json::Result<Self>
521	{
522		from_str::<Self>(v)
523	}
524}
525
526#[derive(Serialize, Deserialize)]
527pub struct UserUpdateServerInput
528{
529	pub user_identifier: String,
530}
531
532impl UserUpdateServerInput
533{
534	pub fn to_string(&self) -> serde_json::Result<String>
535	{
536		to_string(self)
537	}
538
539	pub fn from_string(v: &str) -> serde_json::Result<Self>
540	{
541		from_str::<Self>(v)
542	}
543}
544
545#[derive(Serialize, Deserialize)]
546pub struct UserDeviceList
547{
548	pub device_id: String,
549	pub time: u128,
550	pub device_identifier: String,
551}
552
553#[derive(Serialize, Deserialize)]
554pub struct UserJwtInfo
555{
556	pub id: UserId,
557	pub device_id: DeviceId,
558}
559
560#[derive(Serialize, Deserialize)]
561pub struct Claims
562{
563	//jwt defaults
564	pub aud: UserId,   //the user id
565	pub sub: DeviceId, //the device id
566	pub exp: usize,
567	pub iat: usize,
568	pub fresh: bool, //was this token from refresh jwt or from login
569}
570
571#[derive(Serialize, Deserialize)]
572pub struct OtpRegister
573{
574	pub secret: String, //base32 endowed secret
575	pub alg: String,
576	pub recover: Vec<String>,
577}
578
579#[derive(Serialize, Deserialize)]
580pub struct OtpInput
581{
582	pub token: String,
583	pub auth_key: String,
584	pub device_identifier: String,
585}
586
587#[derive(Serialize, Deserialize)]
588pub struct OtpRecoveryKeysOutput
589{
590	pub keys: Vec<String>,
591}