1 use {
2 crate::signup::GithubEmail,
3 chrono::{DateTime, Utc},
4 serde::{Deserialize, Serialize},
5 serde_repr::{Deserialize_repr, Serialize_repr},
6 std::fmt::{Display, Formatter},
7 tsync::tsync,
8 };
9
10 // smbcloud Users.
11 #[derive(Debug, Serialize, Deserialize)]
12 #[tsync]
13 pub struct User {
14 pub id: i32,
15 pub email: String,
16 pub created_at: DateTime<Utc>,
17 pub updated_at: DateTime<Utc>,
18 }
19
20 impl Display for User {
21 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22 write!(f, "User: id: {}, email: {}", self.id, self.email)
23 }
24 }
25
26 #[derive(Debug, Serialize, Deserialize)]
27 pub struct Status {
28 pub code: Option<i32>,
29 pub message: String,
30 }
31
32 #[derive(Debug, Serialize, Deserialize)]
33 #[tsync]
34 pub struct Data {
35 pub id: i32,
36 pub email: String,
37 pub created_at: String,
38 }
39
40 /// This is smbCloud authorization model
41 /// that is currently tightly coupled with GitHub OAuth model.
42 #[derive(Debug, Serialize, Deserialize)]
43 pub struct SmbAuthorization {
44 pub message: String,
45 pub user: Option<User>,
46 pub user_email: Option<GithubEmail>,
47 pub user_info: Option<GithubInfo>,
48 pub error_code: Option<ErrorCode>,
49 }
50
51 /// TODO: Move to ErrorResponse
52 #[derive(Debug, Serialize_repr, Deserialize_repr, PartialEq)]
53 #[repr(u32)]
54 #[tsync]
55 pub enum ErrorCode {
56 EmailNotFound = 1000,
57 EmailUnverified = 1001,
58 EmailConfirmationFailed = 1002,
59 PasswordNotSet = 1003,
60 GithubNotLinked = 1004,
61 EmailAlreadyExist = 1005,
62 InvalidPassword = 1006,
63 HostedMailAccountUnverified = 1007,
64 }
65
66 impl Display for ErrorCode {
67 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
68 match self {
69 ErrorCode::EmailNotFound => write!(f, "Email not found."),
70 ErrorCode::EmailUnverified => write!(f, "Email not verified."),
71 ErrorCode::EmailConfirmationFailed => write!(f, "Email confirmation failed."),
72 ErrorCode::PasswordNotSet => write!(f, "Password not set."),
73 ErrorCode::GithubNotLinked => write!(f, "Github not connected."),
74 ErrorCode::EmailAlreadyExist => write!(f, "Email already exists."),
75 ErrorCode::InvalidPassword => write!(f, "Invalid password."),
76 ErrorCode::HostedMailAccountUnverified => {
77 write!(f, "Hosted mail account not fully activated yet.")
78 }
79 }
80 }
81 }
82
83 impl Copy for ErrorCode {}
84
85 impl Clone for ErrorCode {
86 fn clone(&self) -> Self {
87 *self
88 }
89 }
90
91 #[derive(Debug, Serialize, Deserialize)]
92 pub struct GithubInfo {
93 pub id: i64,
94 pub login: String,
95 pub name: String,
96 pub avatar_url: String,
97 pub html_url: String,
98 pub email: Option<String>,
99 pub created_at: String,
100 pub updated_at: String,
101 }
102
103 #[cfg(test)]
104 mod tests {
105 use super::*;
106 use serde_json::json;
107 #[test]
108 fn test_smb_authorization() {
109 let smb_authorization = SmbAuthorization {
110 message: "test".to_owned(),
111 user: None,
112 user_email: None,
113 user_info: None,
114 error_code: Some(ErrorCode::EmailUnverified),
115 };
116 let json = json!({
117 "message": "test",
118 "user": null,
119 "user_email": null,
120 "user_info": null,
121 "error_code": 1001,
122 });
123 assert_eq!(serde_json::to_value(smb_authorization).unwrap(), json);
124 }
125 }