Add login functionality
This commit is contained in:
@@ -3,6 +3,6 @@ import Vapor
|
||||
|
||||
struct Config: Codable {
|
||||
let title: String
|
||||
let showSourceBtn: Bool
|
||||
let passwordHash: String
|
||||
let services: [String:[[String:String]]]
|
||||
}
|
||||
@@ -8,6 +8,9 @@ public func configure(_ app: Application) throws {
|
||||
// Serve files from /Public
|
||||
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
||||
|
||||
app.middleware.use(app.sessions.middleware)
|
||||
app.sessions.configuration.cookieName = "statusboard_session"
|
||||
|
||||
// Configure Leaf
|
||||
LeafOption.caching = app.environment.isRelease ? .default : .bypass
|
||||
LeafRenderer.Option.timeout = 200.0
|
||||
|
||||
7
Sources/App/context.swift
Normal file
7
Sources/App/context.swift
Normal file
@@ -0,0 +1,7 @@
|
||||
import Foundation
|
||||
import Vapor
|
||||
|
||||
struct LContext: Codable {
|
||||
let config: Config
|
||||
let loggedIn: Bool
|
||||
}
|
||||
6
Sources/App/login.swift
Normal file
6
Sources/App/login.swift
Normal file
@@ -0,0 +1,6 @@
|
||||
import Foundation
|
||||
import Vapor
|
||||
|
||||
struct Login: Codable {
|
||||
let password: String?
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import Vapor
|
||||
import Foundation
|
||||
import Crypto
|
||||
import Leaf
|
||||
|
||||
func routes(_ app: Application) throws {
|
||||
@@ -7,7 +8,9 @@ func routes(_ app: Application) throws {
|
||||
app.get { req -> EventLoopFuture<View> in
|
||||
let fileData = try String(contentsOfFile: "\(app.directory.resourcesDirectory)/config.json").data(using: .utf8)
|
||||
let config: Config = try! JSONDecoder().decode(Config.self, from: fileData!)
|
||||
return req.view.render("home", ["config": config])
|
||||
let loginStatus = req.session.data["loggedIn"] ?? "false"
|
||||
let loginBool = loginStatus == "true" ? true : false
|
||||
return req.view.render("home", LContext(config: config, loggedIn: loginBool))
|
||||
}
|
||||
|
||||
app.get("status", ":url") { req -> EventLoopFuture<[String: String]> in
|
||||
@@ -18,4 +21,34 @@ func routes(_ app: Application) throws {
|
||||
["down": String(json.isitdown), "code": String(json.response_code)]
|
||||
}
|
||||
}
|
||||
|
||||
app.get("login") { req -> EventLoopFuture<View> in
|
||||
let fileData = try String(contentsOfFile: "\(app.directory.resourcesDirectory)/config.json").data(using: .utf8)
|
||||
let config: Config = try! JSONDecoder().decode(Config.self, from: fileData!)
|
||||
return req.view.render("login", LContext(config: config, loggedIn: false))
|
||||
}
|
||||
|
||||
app.post("login") { req -> Response in
|
||||
let data = try req.content.decode(Login.self)
|
||||
let fileData = try String(contentsOfFile: "\(app.directory.resourcesDirectory)/config.json").data(using: .utf8)
|
||||
let config: Config = try! JSONDecoder().decode(Config.self, from: fileData!)
|
||||
let loginPassData = data.password?.data(using: .utf8)
|
||||
let loginPassHash = SHA256.hash(data: loginPassData ?? "".data(using: .utf8)!)
|
||||
let stringHash = loginPassHash.map { String(format: "%02hhx", $0) }.joined()
|
||||
print("Recv: \(stringHash)")
|
||||
print("Conf: \(config.passwordHash)")
|
||||
if stringHash == config.passwordHash {
|
||||
req.session.data["loggedIn"] = "true"
|
||||
return try req.redirect(to: "/")
|
||||
} else {
|
||||
throw Abort(.unauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
app.get("logout") { req -> Response in
|
||||
req.session.destroy()
|
||||
return try req.redirect(to: "/")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user