1 module vibeauth.router.request;
2 
3 import vibeauth.users;
4 
5 import vibe.http.router;
6 import vibe.data.json;
7 import vibe.inet.url;
8 
9 import std..string;
10 
11 const struct RequestUserData {
12   private {
13     const string[string] data;
14   }
15 
16   this(HTTPServerRequest req) const {
17     string[string] data;
18 
19     if(req.json.type == Json.Type.object) {
20       foreach(string key, value; req.json) {
21         data[key] = value.to!string;
22       }
23     }
24 
25     foreach(string key, value; req.query) {
26       value = value.strip;
27 
28       if(value.length > 0) {
29         data[key] = value;
30       }
31     }
32 
33     foreach(string key, value; req.form) {
34       value = value.strip;
35 
36       if(value.length > 0) {
37         data[key] = value;
38       }
39     }
40 
41     this.data = data;
42   }
43 
44   private string get(string field)() {
45     return field in data ? data[field] : "";
46   }
47 
48   Json toJson() {
49     Json response = data.serializeToJson;
50 
51     if("error" !in response) {
52       response["error"] = "";
53     }
54 
55     if("name" !in response) {
56       response["name"] = "";
57     }
58 
59     if("username" !in response) {
60       response["username"] = "";
61     }
62 
63     if("email" !in response) {
64       response["email"] = "";
65     }
66 
67     return response;
68   }
69 
70   string[] getMissingFields(string[] fields) const {
71     string[] missingFields;
72 
73     foreach(field; fields) {
74       if(field !in data) {
75         missingFields ~= field;
76       }
77     }
78 
79     return missingFields;
80   }
81 
82   string name() {
83     return get!"name";
84   }
85 
86   string username() {
87     return get!"username";
88   }
89 
90   string email() {
91     return get!"email";
92   }
93 
94   string response() {
95     return get!"response";
96   }
97 
98   string password() {
99     return get!"password";
100   }
101 
102   string passwordConfirm() {
103     return get!"passwordConfirm";
104   }
105 
106   string error() {
107     return get!"error";
108   }
109 
110   string message() {
111     return get!"message";
112   }
113 
114   string token() {
115     return get!"token";
116   }
117 
118   void validateUser() {
119     auto missingFields = getMissingFields(["name", "username", "email", "password", "response"]);
120 
121     if(missingFields.length == 1) {
122       throw new Exception("`" ~ missingFields[0] ~ "` is missing");
123     }
124 
125     if(missingFields.length > 1) {
126       throw new Exception("`" ~ missingFields.join(",") ~ "` is missing");
127     }
128 
129     if(password == "") {
130       throw new Exception("The `password` is empty");
131     }
132 
133     if(password.length < 10) {
134       throw new Exception("The `password` should have at least 10 chars");
135     }
136   }
137 }
138 
139 User getUser(HTTPServerRequest req, UserCollection collection) {
140   string token = req.cookies.get("auth-token");
141 
142   User user;
143 
144   if(token !is null) {
145     try {
146       user = collection.byToken(token);
147     } catch(Exception) {
148       return null;
149     }
150   }
151 
152   return user;
153 }