Giter VIP home page Giter VIP logo

perfect-turnstile-mongokittendb's Introduction

Perfect-Turnstile-MongoKittenDB

This project integrates Stormpath's Turnstile authentication system into a single package with Perfect, and MongoDB.

This project used https://github.com/PerfectlySoft/Perfect-Turnstile-SQLite as a base and was modified to work with MongoDB, using MongoKitten without an ORM.

Installation

In your Package.swift file, include the following line inside the dependancy array:

.Package(
	url: "https://github.com/bkostjens/Perfect-Turnstile-MongoKittenDB.git",
	majorVersion: 1)

Included JSON Routes

The framework includes certain basic routes:

POST /api/v1/login (with username & password as json encoded strings)
POST /api/v1/register (with username & password as json encoded strings)
GET /api/v1/logout

Included Routes for Browser

The following routes are available for browser testing:

http://localhost:8181
http://localhost:8181/login
http://localhost:8181/register

These routes are using Mustache files in the webroot directory.

Example Mustache files can be found in https://github.com/PerfectExamples/Perfect-Turnstile-SQLite-Demo

Creating an HTTP Server with Authentication

import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import PerfectTurnstileMongoDB

// Setup the mongo db connection for PerfectTurnstileMongoDB
let connect = MongoConnect(hostname:"localhost", databaseName: "ProProject")
     
// Connect the AccessTokenStore
tokenStore = AccessTokenStore(server: connect)
    
// Used later in script for the Realm and how the user authenticates.
let pturnstile = TurnstilePerfectRealm(server: connect)
    
// add routes to be checked for auth
var authenticationConfig = AuthenticationConfig()
authenticationConfig.include("/api/v1/*")
authenticationConfig.exclude("/api/v1/login")
authenticationConfig.exclude("/api/v1/register")
    
let authFilter = AuthFilter(authenticationConfig)

// Create HTTP server.
let server = HTTPServer()

// Register routes and handlers
let authWebRoutes = makeWebAuthRoutes()
let authJSONRoutes = makeJSONAuthRoutes("/api/v1")

// Add the routes to the server.
server.addRoutes(authWebRoutes)
server.addRoutes(authJSONRoutes)

// Add more routes here
var routes = Routes()
// routes.add(method: .get, uri: "/api/v1/test", handler: AuthHandlersJSON.testHandler)

// Add the routes to the server.
server.addRoutes(routes)

// add routes to be checked for auth
var authenticationConfig = AuthenticationConfig()
authenticationConfig.include("/api/v1/*")
authenticationConfig.exclude("/api/v1/login")
authenticationConfig.exclude("/api/v1/register")

let authFilter = AuthFilter(authenticationConfig)

// Note that order matters when the filters are of the same priority level
server.setRequestFilters([pturnstile.requestFilter])
server.setResponseFilters([pturnstile.responseFilter])

server.setRequestFilters([(authFilter, .high)])

// Set a listen port of 8181
server.serverPort = 8181

// Where to serve static files from
server.documentRoot = "./webroot"

do {
	// Launch the HTTP server.
	try server.start()
} catch PerfectError.networkError(let err, let msg) {
	print("Network error thrown: \(err) \(msg)")
}

Requirements

Define the "Realm" - this is the Turnstile definition of how the authentication is handled. The implementation is specific to the MongoDB datasource, although it is very similar between datasources and is designed to be generic and extendable.

let pturnstile = TurnstilePerfectRealm(server: connect)

Define the location and name of the MongoDB database:

connect = MongoConnect(hostname:"localhost", databaseName: "ProProject")

Connect the AccessTokenStore:

tokenStore = AccessTokenStore(server: connect)

Create the HTTP Server:

let server = HTTPServer()

Register routes and handlers and add the routes to the server:

let authWebRoutes = makeWebAuthRoutes()
let authJSONRoutes = makeJSONAuthRoutes("/api/v1")

server.addRoutes(authWebRoutes)
server.addRoutes(authJSONRoutes)

Add routes to be checked for authentication:

var authenticationConfig = AuthenticationConfig()
authenticationConfig.include("/api/v1/check")
authenticationConfig.exclude("/api/v1/login")
authenticationConfig.exclude("/api/v1/register")

let authFilter = AuthFilter(authenticationConfig)

These routes can be either seperate, or as an array of strings. They describe inclusions and exclusions. In a forthcoming release wildcard routes will be supported.

Add request & response filters. Note the order which you specify filters that are of the same priority level:

server.setRequestFilters([pturnstile.requestFilter])
server.setResponseFilters([pturnstile.responseFilter])

server.setRequestFilters([(authFilter, .high)])

Now, set the port, static files location, and start the server:

// Set a listen port of 8181
server.serverPort = 8181

// Where to serve static files from
server.documentRoot = "./webroot"

do {
	// Launch the HTTP server.
	try server.start()
} catch PerfectError.networkError(let err, let msg) {
	print("Network error thrown: \(err) \(msg)")
}

perfect-turnstile-mongokittendb's People

Stargazers

Muhammad Rusdi avatar

Watchers

Barry Kostjens avatar James Cloos avatar

perfect-turnstile-mongokittendb's Issues

fails to compile

this should compile but it doesnt for some strange reason. using mongokitten 4

error: argument passed to call that takes no arguments
tokenStore = AccessTokenStore(server: connect)
~~~~~~~~~^~~~~~~~
error: incorrect argument label in call (have 'server:', expected 'sessionManager:')
let pturnstile = TurnstilePerfectRealm(server: connect)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.