Restore EzDent application login over LAN
This commit is contained in:
1 parent
0aad4f27eb
commit
96fb58fa4e
4 files changed
+75
-10
No files matched your search
@@ -20,6 +20,7 @@ final class WebConsoleAPI {
|
||||
private let passwordDigest = SHA256.hash(data: Data(
|
||||
(ProcessInfo.processInfo.environment["EZSERVER_CONSOLE_PASSWORD"] ?? "0000").utf8
|
||||
))
|
||||
private let signingKey = SymmetricKey(size: .bits256)
|
||||
private var sessions: [String: Date] = [:]
|
||||
private var events: [[String: Any]] = []
|
||||
private var failures = 0
|
||||
@@ -29,6 +30,7 @@ final class WebConsoleAPI {
|
||||
guard path.count >= 2 else { return false }
|
||||
switch (path[0], path[1], method) {
|
||||
case ("auth", "apploginr2", "POST"),
|
||||
("auth", "applogin", "POST"),
|
||||
("e2ds", "useraccountsbynamer2", "GET"),
|
||||
("e2ds", "useraccountsfornames", "GET"),
|
||||
("analytics", "eventsforsearch", "POST"),
|
||||
@@ -50,8 +52,18 @@ final class WebConsoleAPI {
|
||||
}
|
||||
}
|
||||
|
||||
func allowsNetworkAccess(_ path: [String], method: String) -> Bool {
|
||||
switch (path, method) {
|
||||
case (["auth", "apploginr2"], "POST"), (["auth", "applogin"], "POST"),
|
||||
(["e2ds", "useraccountsbynamer2", "name", Self.admin.lowercased()], "GET"):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func reply(path: [String], method: String, body: Data, authorization: String?, store: ServerStore) -> Reply {
|
||||
if path == ["auth", "apploginr2"] {
|
||||
if path == ["auth", "apploginr2"] || path == ["auth", "applogin"] {
|
||||
return login(body)
|
||||
}
|
||||
let now = Date()
|
||||
@@ -150,9 +162,11 @@ final class WebConsoleAPI {
|
||||
}
|
||||
guard let request = try? JSONDecoder().decode(LoginRequest.self, from: body),
|
||||
let login = Self.decrypt(request.data),
|
||||
login.auth.enc == "raw", login.auth.usr == Self.admin,
|
||||
login.auth.decoded(login.auth.usr) == Self.admin,
|
||||
login.auth.exp >= now.timeIntervalSince1970,
|
||||
zip(SHA256.hash(data: Data(login.auth.sec.utf8)), passwordDigest)
|
||||
login.auth.exp < Double(Int32.max),
|
||||
let password = login.auth.decoded(login.auth.sec),
|
||||
zip(SHA256.hash(data: Data(password.utf8)), passwordDigest)
|
||||
.reduce(UInt8(0), { $0 | ($1.0 ^ $1.1) }) == 0 else {
|
||||
failures += 1
|
||||
if failures >= 8 {
|
||||
@@ -168,12 +182,36 @@ final class WebConsoleAPI {
|
||||
if sessions.count >= 32, let oldest = sessions.min(by: { $0.value < $1.value }) {
|
||||
sessions.removeValue(forKey: oldest.key)
|
||||
}
|
||||
let token = UUID().uuidString + UUID().uuidString
|
||||
sessions[token] = now.addingTimeInterval(600)
|
||||
let expiry = Int(login.auth.exp)
|
||||
guard let token = try? makeToken(login, issuedAt: Int(now.timeIntervalSince1970), expiry: expiry) else {
|
||||
return Reply(status: "503 Service Unavailable", errorCode: -1005)
|
||||
}
|
||||
sessions[token] = Date(timeIntervalSince1970: Double(expiry))
|
||||
event(type: "LOGIN", message: "Logged in to EzServer Web Console.")
|
||||
return Reply(["token": token])
|
||||
}
|
||||
|
||||
private func makeToken(_ login: Login, issuedAt: Int, expiry: Int) throws -> String {
|
||||
func base64URL(_ bytes: Data) -> String {
|
||||
bytes.base64EncodedString().replacingOccurrences(of: "+", with: "-")
|
||||
.replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "")
|
||||
}
|
||||
let header = try JSONSerialization.data(withJSONObject: ["typ": "JWT", "alg": "HS256"],
|
||||
options: .sortedKeys)
|
||||
let payload = try JSONSerialization.data(withJSONObject: [
|
||||
"enc": login.auth.enc,
|
||||
"iss": login.auth.usr,
|
||||
"aud": "http://www.ewoosoft.com",
|
||||
"iat": issuedAt,
|
||||
"exp": expiry,
|
||||
"clientName": login.client?.name ?? "Legacy",
|
||||
"clientVersion": login.client?.ver ?? "0.0.0",
|
||||
], options: .sortedKeys)
|
||||
let message = base64URL(header) + "." + base64URL(payload)
|
||||
let signature = HMAC<SHA256>.authenticationCode(for: Data(message.utf8), using: signingKey)
|
||||
return message + "." + base64URL(Data(signature))
|
||||
}
|
||||
|
||||
private func event(type: String, message: String) {
|
||||
events.append(["id": UUID().uuidString, "eventType": String(type.prefix(100)),
|
||||
"appName": "EzServer Web Console", "userName": Self.admin,
|
||||
@@ -205,8 +243,34 @@ final class WebConsoleAPI {
|
||||
let usr: String
|
||||
let sec: String
|
||||
let exp: Double
|
||||
|
||||
func decoded(_ value: String) -> String? {
|
||||
switch enc {
|
||||
case "raw":
|
||||
return value
|
||||
case "b64":
|
||||
return Data(base64Encoded: value).flatMap { String(data: $0, encoding: .utf8) }
|
||||
case "hex":
|
||||
let digits = Array(value.utf8)
|
||||
guard digits.count.isMultiple(of: 2) else { return nil }
|
||||
var bytes: [UInt8] = []
|
||||
for index in stride(from: 0, to: digits.count, by: 2) {
|
||||
guard let byte = UInt8(String(decoding: digits[index...index + 1], as: UTF8.self), radix: 16)
|
||||
else { return nil }
|
||||
bytes.append(byte)
|
||||
}
|
||||
return String(bytes: bytes, encoding: .utf8)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
struct Client: Decodable {
|
||||
let name: String
|
||||
let ver: String
|
||||
}
|
||||
let auth: Auth
|
||||
let client: Client?
|
||||
}
|
||||
|
||||
private static func decrypt(_ encoded: String) -> Login? {
|
||||
|
||||
Reference in new issue
Block a user