Skip to content

Instantly share code, notes, and snippets.

@iljaiwas
Last active November 24, 2016 20:51
Show Gist options
  • Save iljaiwas/5bd75a6c054c34a8ce365804214fcc67 to your computer and use it in GitHub Desktop.
Save iljaiwas/5bd75a6c054c34a8ce365804214fcc67 to your computer and use it in GitHub Desktop.
How do you use the verifyPassword parameter when initializing an CredentialsHTTPBasic object?
import Credentials
import CredentialsHTTP
import Kitura
import HeliumLogger
// Initialize HeliumLogger
HeliumLogger.use()
// Create a new router
let router = Router()
let credentials = Credentials()
let users = ["John" : "12345", "Mary" : "qwerasdf"]
let basicCredentials = CredentialsHTTPBasic(verifyPassword: { (userID, password, profile: (UserProfile?) -> Void) in
if (users[userID] == password)
{
}
else
{
// nomatch
}
}, realm: "CredentialsHTTPTest")
credentials.register(plugin: basicCredentials)
router.all("/profile", middleware: credentials)
router.get("/profile", handler:
{ request, response, next in
if let profile = request.userProfile
{
let userId = profile.id
let userName = profile.displayName
response.send("Hello, User!")
}
else
{
response.send("No User")
}
next()
})
router.get("/") {
request, response, next in
response.send("Hello, World!")
next()
}
// Add an HTTP server and connect it to the router
Kitura.addHTTPServer(onPort: 8090, with: router)
// Start the Kitura runloop (this call never returns)
Kitura.run()
import PackageDescription
let package = Package(
name: "CredentialsHTTPTest",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 2),
.Package(url: "https://github.com/IBM-Swift/HeliumLogger.git", majorVersion: 1, minor: 1),
.Package(url: "https://github.com/IBM-Swift/Kitura-CredentialsHTTP.git", majorVersion: 1)
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment