forked from CyberShadow/DFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.d
300 lines (257 loc) · 6.93 KB
/
user.d
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
/* Copyright (C) 2011, 2012, 2013 Vladimir Panteleev <vladimir@thecybershadow.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
module user;
import std.string;
import std.exception;
import std.base64;
import ae.sys.data;
import ae.utils.text;
import ae.utils.zlib;
abstract class User
{
abstract string get(string name, string defaultValue);
abstract void set(string name, string value);
abstract string[] save();
abstract void logIn(string username, string password);
abstract void logOut();
abstract void register(string username, string password);
abstract bool isLoggedIn();
string getName() { return null; }
final bool opIn_r(string name)
{
return get(name, null) !is null;
}
final string opIndex(string name)
{
auto result = get(name, null);
enforce(result, "No such user setting: " ~ name);
return result;
}
final string opIndexAssign(string value, string name)
{
set(name, value);
return value;
}
protected:
/// Save misc data to string settings
final void finalize()
{
encodeReadPosts();
}
// ***********************************************************************
private:
Data* readPosts;
bool readPostsDirty;
final:
void needReadPosts()
{
if (!readPosts)
{
auto b64 = get("readposts", null);
if (b64)
{
// Temporary hack to catch Phobos bug
ubyte[] zcode;
try
zcode = Base64.decode(b64);
catch
{
std.file.write("bad-base64.txt", b64);
throw new Exception("Malformed Base64 in read post history cookie. Try clearing your cookies.");
}
readPosts = [uncompress(Data(zcode))].ptr;
}
else
readPosts = new Data();
}
}
void encodeReadPosts()
{
if (readPosts && readPosts.length && readPostsDirty)
{
auto b64 = Base64.encode(cast(ubyte[])compress(*readPosts, 1).contents);
set("readposts", assumeUnique(b64));
}
}
public bool isRead(size_t post)
{
needReadPosts();
auto pos = post/8;
if (pos >= readPosts.length)
return false;
else
return ((cast(ubyte[])readPosts.contents)[pos] & (1 << (post % 8))) != 0;
}
public void setRead(size_t post, bool value)
{
needReadPosts();
auto pos = post/8;
if (pos >= readPosts.length)
{
if (value)
readPosts.length = pos+1;
else
return;
}
ubyte mask = cast(ubyte)(1 << (post % 8));
assert(pos < readPosts.length);
auto pbyte = (cast(ubyte*)readPosts.ptr) + pos;
if (value)
*pbyte = *pbyte | mask;
else
*pbyte = *pbyte & ~mask;
readPostsDirty = true;
}
}
final class GuestUser : User
{
string[string] cookies, newCookies;
this(string cookieHeader)
{
auto segments = cookieHeader.split(";");
foreach (segment; segments)
{
segment = segment.strip();
auto p = segment.indexOf('=');
if (p > 0)
{
string name = segment[0..p];
if (name.startsWith("dfeed_"))
cookies[name[6..$]] = segment[p+1..$];
}
}
}
override string get(string name, string defaultValue)
{
auto pCookie = name in newCookies;
if (pCookie)
return *pCookie;
pCookie = name in cookies;
if (pCookie)
return *pCookie;
return defaultValue;
}
override void set(string name, string value)
{
newCookies[name] = value;
}
override string[] save()
{
finalize();
string[] result;
foreach (name, value; newCookies)
{
if (name in cookies && cookies[name] == value)
continue;
// TODO Expires
result ~= "dfeed_" ~ name ~ "=" ~ value ~ "; Expires=Wed, 09 Jun 2021 10:18:14 GMT; Path=/";
}
return result;
}
static string encryptPassword(string password)
{
// TODO: use bcrypt()
import std.md5, std.file;
return toLower(getDigestString(password ~ readText("data/salt.txt")));
}
override void logIn(string username, string password)
{
foreach (string session; query("SELECT `Session` FROM `Users` WHERE `Username` = ? AND `Password` = ?").iterate(username, encryptPassword(password)))
{
set("session", session);
return;
}
throw new Exception("No such username/password combination");
}
override void register(string username, string password)
{
enforce(username.length, "Please enter a username");
enforce(username.length < 32, "Username too long");
enforce(password.length < 64, "Password too long");
// Create user
auto session = randomString();
query("INSERT INTO `Users` (`Username`, `Password`, `Session`) VALUES (?, ?, ?)").exec(username, encryptPassword(password), session);
// Copy cookies to database
auto user = new RegisteredUser(username);
foreach (name, value; cookies)
user.set(name, value);
user.save();
// Log them in
this.set("session", session);
}
override void logOut() { throw new Exception("Not logged in"); }
override bool isLoggedIn() { return false; }
}
import database;
final class RegisteredUser : User
{
string[string] settings, newSettings;
string username;
this(string username)
{
this.username = username;
}
override string get(string name, string defaultValue)
{
auto pSetting = name in newSettings;
if (pSetting)
return *pSetting;
pSetting = name in settings;
string value;
if (pSetting)
value = *pSetting;
else
{
foreach (string v; query("SELECT `Value` FROM `UserSettings` WHERE `User` = ? AND `Name` = ?").iterate(username, name))
value = v;
settings[name] = value;
}
return value ? value : defaultValue;
}
override void set(string name, string value)
{
newSettings[name] = value;
}
override string[] save()
{
finalize();
foreach (name, value; newSettings)
{
if (name in settings && settings[name] == value)
continue;
query("INSERT OR REPLACE INTO `UserSettings` (`User`, `Name`, `Value`) VALUES (?, ?, ?)").exec(username, name, value);
}
return null;
}
override void logIn(string username, string password) { throw new Exception("Already logged in"); }
override bool isLoggedIn() { return true; }
override void register(string username, string password) { throw new Exception("Already registered"); }
override string getName() { return username; }
override void logOut()
{
query("UPDATE `Users` SET `Session` = ? WHERE `Username` = ?").exec(randomString(), username);
}
}
User getUser(string cookieHeader)
{
auto guest = new GuestUser(cookieHeader);
if ("session" in guest.cookies)
{
foreach (string username; query("SELECT `Username` FROM `Users` WHERE `Session` = ?").iterate(guest.cookies["session"]))
return new RegisteredUser(username);
}
return guest;
}