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