import Kitura

import LoggerAPI
import HeliumLogger

import Credentials
import CredentialsHTTP

import PerfectCrypto
import Foundation

// MARK: NEVER DO THIS IN PRODUCTION! PRIVATE KEYS SHOULD BE KEPT SECURE!
let privateKey =  "-----BEGIN RSA PRIVATE KEY-----\nMIICWwIBAAKBgQDdlatRjRjogo3WojgGHFHYLugdUWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQsHUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5Do2kQ+X5xK9cipRgEKwIDAQABAoGAD+onAtVye4ic7VR7V50DF9bOnwRwNXrARcDhq9LWNRrRGElESYYTQ6EbatXS3MCyjjX2eMhu/aF5YhXBwkppwxg+EOmXeh+MzL7Zh284OuPbkglAaGhV9bb6/5CpuGb1esyPbYW+Ty2PC0GSZfIXkXs76jXAu9TOBvD0ybc2YlkCQQDywg2R/7t3Q2OE2+yo382CLJdrlSLVROWKwb4tb2PjhY4XAwV8d1vy0RenxTB+K5Mu57uVSTHtrMK0GAtFr833AkEA6avx20OHo61Yela/4k5kQDtjEf1N0LfI+BcWZtxsS3jDM3i1Hp0KSu5rsCPb8acJo5RO26gGVrfAsDcIXKC+bQJAZZ2XIpsitLyPpuiMOvBbzPavd4gY6Z8KWrfYzJoI/Q9FuBo6rKwl4BFoToD7WIUS+hpkagwWiz+6zLoX1dbOZwJACmH5fSSjAkLRi54PKJ8TFUeOP15h9sQzydI8zJU+upvDEKZsZc/UhT/SySDOxQ4G/523Y0sz/OZtSWcol/UMgQJALesy++GdvoIDLfJX5GBQpuFgFenRiRDabxrE9MNUZ2aPFaFp+DyAe+b4nDwuJaW2LURbr8AEZga7oQj0uYxcYw==\n-----END RSA PRIVATE KEY-----\n"

// Setup logger
Log.logger = HeliumLogger(.info)

// Setup basic credentials middleware
let credentials = Credentials()
let users = ["username" : "password"]
let httpBasic = CredentialsHTTPBasic(verifyPassword: { userId, password, callback in
    if let storedPassword = users[userId], storedPassword == password {
        Log.info("\(userId) logged in")
        callback(UserProfile(id: userId, displayName: userId, provider: "HTTPBasic"))
    } else {
        Log.error("\(userId) login failed")
        callback(nil)
    }
})
credentials.register(plugin: httpBasic)

// Create logging middleware
class RequestLogger: RouterMiddleware {
    public func handle(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
        Log.info("\(request.method) request made for \(request.originalURL)")
        next()
    }
}

// Create a new router
let router = Router()

// Setup routes
router.all("/*", middleware: RequestLogger())
router.all("/*", middleware: credentials)

// Route requests
router.get("/") { request, response, next in
    response.send("nothing to see here, moving along\n")
    next()
}
router.get("/login") { request, response, next in
    let jwtPayload: [String : Any] = [
        "issuer": "udacity.swift.monolith",
        "issuedAt": Date().timeIntervalSince1970,
        "expiration": Date().append(months: 1).timeIntervalSince1970
    ]
    guard let jwt = JWTCreator(payload: jwtPayload) else {
        response.send("couldn't create token\n")
        next()
        return
    }
    let privateKeyAsPem = try PEMKey(source: privateKey)
    let signedJWTToken = try jwt.sign(alg: .rs256, key: privateKeyAsPem)
    response.send(json: ["token": signedJWTToken])
    next()
}

// Add an HTTP server and connect it to the router
Kitura.addHTTPServer(onPort: 8082, with: router)

// Start the Kitura runloop (this call never returns)
Kitura.run()
