-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAccount.swift
More file actions
267 lines (231 loc) · 6.94 KB
/
Account.swift
File metadata and controls
267 lines (231 loc) · 6.94 KB
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
//
// Account.swift
// Perfect-OAuth2-Server
//
// Created by Jonathan Guthrie on 2017-02-06.
//
//
import StORM
import PostgresStORM
import SwiftRandom
import PerfectSMTP
import PerfectCrypto
public class Account: PostgresStORM {
public var id = ""
public var username = ""
public var password = ""
public var salt = ""
public var email = ""
public var usertype: AccountType = .provisional
public var source = "local" // local, facebook, etc
public var remoteid = "" // if oauth then the sourceid is stored here
public var passvalidation = ""
public var passreset = ""
public var detail = [String:Any]()
let _r = URandom()
public static func setup(_ str: String = "") {
do {
let obj = Account()
try obj.setup(str)
// Account migrations:
// 1.3.1->1.4
let _ = try? obj.sql("ALTER TABLE account ADD COLUMN source text;", params: [])
let _ = try? obj.sql("ALTER TABLE account ADD COLUMN remoteid text;", params: [])
let _ = try? obj.sql("ALTER TABLE account ADD COLUMN passreset text;", params: [])
} catch {
// nothing
}
}
override public func to(_ this: StORMRow) {
id = this.data["id"] as? String ?? ""
username = this.data["username"] as? String ?? ""
password = this.data["password"] as? String ?? ""
email = this.data["email"] as? String ?? ""
usertype = AccountType.from((this.data["usertype"] as? String)!)
source = this.data["source"] as? String ?? "local"
remoteid = this.data["remoteid"] as? String ?? ""
passvalidation = this.data["passvalidation"] as? String ?? ""
passreset = this.data["passreset"] as? String ?? ""
if let detailObj = this.data["detail"] {
self.detail = detailObj as? [String:Any] ?? [String:Any]()
}
}
public func rows() -> [Account] {
var rows = [Account]()
for i in 0..<self.results.rows.count {
let row = Account()
row.to(self.results.rows[i])
rows.append(row)
}
return rows
}
public override init() {
super.init()
}
public init(
_ i: String = "",
_ u: String,
_ p: String = "",
_ e: String,
_ ut: AccountType = .provisional,
_ s: String = "local",
_ rid: String = ""
) {
super.init()
id = i
username = u
password = p
email = e
usertype = ut
passvalidation = _r.secureToken
passreset = _r.secureToken
source = s
remoteid = rid
}
public init(validation: String) {
super.init()
try? find(["passvalidation": validation])
}
public init(reset: String) {
super.init()
try? find(["passreset": reset])
}
public func makeID() {
id = _r.secureToken
}
public func makePassword(_ p1: String) {
if let random = ([UInt8](randomCount: 16)).encode(.hex),
let salt = String(validatingUTF8: random),
let shadow = p1.encrypt(.aes_128_cbc, password: p1, salt: salt) {
password = shadow
self.salt = salt
}
}
public func isUnique() throws {
// checks for email address already existing
let this = Account()
// let thisUsername = Account()
do {
try this.find(["email":email])
if this.results.cursorData.totalRecords > 0 {
// print("failing unique test")
throw OAuth2ServerError.invalidEmail
}
} catch {
// print(error)
throw OAuth2ServerError.invalidEmail
}
}
// Register User
public static func register(_ u: String, _ e: String, _ ut: AccountType = .provisional, baseURL: String) -> OAuth2ServerError {
let r = URandom()
let acc = Account(r.secureToken, u, "", e, ut)
do {
try acc.isUnique()
// print("passed unique test")
try acc.create()
} catch {
print(error)
return .registerError
}
var h = "<p>Welcome to your new account</p>"
h += "<p>To get started with your new account, please <a href=\"\(baseURL)/verifyAccount/\(acc.passvalidation)\">click here</a></p>"
h += "<p>If the link does not work copy and paste the following link into your browser:<br>\(baseURL)/verifyAccount/\(acc.passvalidation)</p>"
var t = "Welcome to your new account\n"
t += "To get started with your new account, please click here: \(baseURL)/verifyAccount/\(acc.passvalidation)"
Utility.sendMail(name: u, address: e, subject: "Welcome to your account", html: h, text: t)
return .noError
}
/// Reset Password
/// - Parameter e: email address
/// - Parameter baseURL: base url to create the reset pass url
public static func resetPassword(_ e: String, baseURL: String) -> OAuth2ServerError {
let r = URandom()
let acc = Account()
do {
try acc.find(["email": e])
acc.passreset = r.secureToken
acc.email = e
try acc.save()
} catch {
print(error)
return .invalidEmail
}
var h = "<p>Forgotten password reset</p>"
h += "<p>You requested a new password <a href=\"\(baseURL)/verifyPassReset/\(acc.passreset)\">click here</a></p>"
h += "<p>If the link does not work copy and paste the following link into your browser:<br>\(baseURL)/resetPassword/\(acc.passreset)</p>"
var t = "Forgotten password reset\n"
t += "You requested a new password, please click here: \(baseURL)/verifyPassReset/\(acc.passreset)"
Utility.sendMail(name: "", address: e, subject: "Password reset request", html: h, text: t)
return .noError
}
// Register User
public static func login(_ u: String, _ p: String) throws -> Account {
let acc = Account()
let criteria = ["username":u]
do {
try acc.find(criteria)
guard let pwd = acc.password.decrypt(.aes_128_cbc, password: p, salt: acc.salt),
pwd == p, acc.usertype != .provisional else {
throw OAuth2ServerError.loginError
}
return acc
} catch {
print(error)
throw OAuth2ServerError.loginError
}
}
public static func listUsers() -> [[String: Any]] {
var users = [[String: Any]]()
let t = Account()
let cursor = StORMCursor(limit: 9999999,offset: 0)
try? t.select(
columns: [],
whereclause: "true",
params: [],
orderby: ["username"],
cursor: cursor
)
for row in t.rows() {
var r = [String: Any]()
r["id"] = row.id
r["username"] = row.username
r["email"] = row.email
r["usertype"] = row.usertype
r["detail"] = row.detail
r["source"] = row.source
r["remoteid"] = row.remoteid
users.append(r)
}
return users
}
public func isAdmin() -> Bool {
switch usertype {
case .admin, .admin1, .admin2, .admin3:
return true
default:
return false
}
}
}
public enum AccountType {
case provisional, standard, inactive, admin, admin1, admin2, admin3
public static func from(_ str: String) -> AccountType {
switch str {
case "admin":
return .admin
case "admin1":
return .admin1
case "admin2":
return .admin2
case "admin3":
return .admin3
case "standard":
return .standard
case "inactive":
return .inactive
default:
return .provisional
}
}
}