{"id":20038041,"url":"https://github.com/perfectlysoft/perfect-turnstile-mongodb","last_synced_at":"2025-07-11T20:01:42.386Z","repository":{"id":69670176,"uuid":"79086056","full_name":"PerfectlySoft/Perfect-Turnstile-MongoDB","owner":"PerfectlySoft","description":"A MongoDB ORM single-package integration for Turnstile authentication. ","archived":false,"fork":false,"pushed_at":"2017-10-04T21:59:15.000Z","size":21,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-25T12:02:00.963Z","etag":null,"topics":["authentication","mongodb","perfect","server-side-swift","swift","turnstile","turnstile-authentication"],"latest_commit_sha":null,"homepage":"https://github.com/PerfectExamples/Perfect-Turnstile-MongoDB-Demo","language":"Swift","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PerfectlySoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-16T05:33:47.000Z","updated_at":"2018-02-25T05:57:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"0bba8a24-7fb1-4c4b-8b77-16b517a9c1b8","html_url":"https://github.com/PerfectlySoft/Perfect-Turnstile-MongoDB","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/PerfectlySoft/Perfect-Turnstile-MongoDB","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Turnstile-MongoDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Turnstile-MongoDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Turnstile-MongoDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Turnstile-MongoDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerfectlySoft","download_url":"https://codeload.github.com/PerfectlySoft/Perfect-Turnstile-MongoDB/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Turnstile-MongoDB/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264890029,"owners_count":23678825,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","mongodb","perfect","server-side-swift","swift","turnstile","turnstile-authentication"],"created_at":"2024-11-13T10:24:53.141Z","updated_at":"2025-07-11T20:01:42.282Z","avatar_url":"https://github.com/PerfectlySoft.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Perfect Turnstile with MongoDB [简体中文](README.zh_CN.md)\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"http://perfect.org/get-involved.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.org/assets/github/perfect_github_2_0_0.jpg\" alt=\"Get Involed with Perfect!\" width=\"854\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/PerfectlySoft/Perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg\" alt=\"Star Perfect On Github\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://stackoverflow.com/questions/tagged/perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/perfect_gh_button_2_SO.jpg\" alt=\"Stack Overflow\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"https://twitter.com/perfectlysoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg\" alt=\"Follow Perfect on Twitter\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg\" alt=\"Join the Perfect Slack\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Swift-3.0-orange.svg?style=flat\" alt=\"Swift 3.0\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat\" alt=\"Platforms OS X | Linux\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.org/licensing.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat\" alt=\"License Apache\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://twitter.com/PerfectlySoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat\" alt=\"PerfectlySoft Twitter\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.ly/badge.svg\" alt=\"Slack Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\nThis project integrates Stormpath's Turnstile authentication system into a single package with Perfect, and a MongoDB ORM.\n\n## Installation\n\nIn your Package.swift file, include the following line inside the dependancy array:\n\n``` swift\n.Package(\n\turl: \"https://github.com/PerfectlySoft/Perfect-Turnstile-MongoDB.git\",\n\tmajorVersion: 1\n\t)\n```\n\n## Included JSON Routes\n\nThe framework includes certain basic routes:\n\n```\nPOST /api/v1/login (with username \u0026 password form elements)\nPOST /api/v1/register (with username \u0026 password form elements)\nGET /api/v1/logout\n```\n\n## Included Routes for Browser\n\nThe following routes are available for browser testing:\n\n```\nhttp://localhost:8181\nhttp://localhost:8181/login\nhttp://localhost:8181/register\n```\n\nThese routes are using Mustache files in the webroot directory.\n\nExample Mustache files can be found in [https://github.com/PerfectExamples/Perfect-Turnstile-PostgreSQL-Demo](https://github.com/PerfectExamples/Perfect-Turnstile-PostgreSQL-Demo)\n\n## Creating an HTTP Server with Authentication\n\n``` swift \nimport PerfectLib\nimport PerfectHTTP\nimport PerfectHTTPServer\n\nimport StORM\nimport MongoDBStORM\nimport PerfectTurnstileMongoDB\n\n// Used later in script for the Realm and how the user authenticates.\nlet pturnstile = TurnstilePerfectRealm()\n\n// Set the connection\nMongoDBConnection.host = \"localhost\"\nMongoDBConnection.database = \"perfect_testing\"\n\n// Connect the AccessTokenStore\ntokenStore = AccessTokenStore(connect!)\n\n// Create HTTP server.\nlet server = HTTPServer()\n\n// Register routes and handlers\nlet authWebRoutes = makeWebAuthRoutes()\nlet authJSONRoutes = makeJSONAuthRoutes(\"/api/v1\")\n\n// Add the routes to the server.\nserver.addRoutes(authWebRoutes)\nserver.addRoutes(authJSONRoutes)\n\n// Add more routes here\nvar routes = Routes()\n// routes.add(method: .get, uri: \"/api/v1/test\", handler: AuthHandlersJSON.testHandler)\n\n// Add the routes to the server.\nserver.addRoutes(routes)\n\n// add routes to be checked for auth\nvar authenticationConfig = AuthenticationConfig()\nauthenticationConfig.include(\"/api/v1/check\")\nauthenticationConfig.exclude(\"/api/v1/login\")\nauthenticationConfig.exclude(\"/api/v1/register\")\n\nlet authFilter = AuthFilter(authenticationConfig)\n\n// Note that order matters when the filters are of the same priority level\nserver.setRequestFilters([pturnstile.requestFilter])\nserver.setResponseFilters([pturnstile.responseFilter])\n\nserver.setRequestFilters([(authFilter, .high)])\n\n// Set a listen port of 8181\nserver.serverPort = 8181\n\n// Where to serve static files from\nserver.documentRoot = \"./webroot\"\n\ndo {\n\t// Launch the HTTP server.\n\ttry server.start()\n} catch PerfectError.networkError(let err, let msg) {\n\tprint(\"Network error thrown: \\(err) \\(msg)\")\n}\n\n```\n\n### Requirements\n\nDefine the \"Realm\" - this is the Turnstile definition of how the authentication is handled. The implementation is specific to the PostgreSQL datasource, although it is very similar between datasources and is designed to be generic and extendable.\n\n``` swift \nlet pturnstile = TurnstilePerfectRealm()\n```\n\nDefine the connection details to access the MongoDB server:\n\n``` swift\nMongoDBConnection.host = \"localhost\"\nMongoDBConnection.database = \"perfect_testing\"\n```\n\nConnect the AccessTokenStore:\n\n``` swift\ntokenStore = AccessTokenStore(connect!)\n```\n\nCreate the HTTP Server:\n\n``` swift\nlet server = HTTPServer()\n```\n\nRegister routes and handlers and add the routes to the server:\n\n``` swift \nlet authWebRoutes = makeWebAuthRoutes()\nlet authJSONRoutes = makeJSONAuthRoutes(\"/api/v1\")\n\nserver.addRoutes(authWebRoutes)\nserver.addRoutes(authJSONRoutes)\n```\n\nAdd routes to be checked for authentication:\n\n``` swift\nvar authenticationConfig = AuthenticationConfig()\nauthenticationConfig.include(\"/api/v1/check\")\nauthenticationConfig.exclude(\"/api/v1/login\")\nauthenticationConfig.exclude(\"/api/v1/register\")\n\nlet authFilter = AuthFilter(authenticationConfig)\n```\n\nThese 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.\n\nAdd request \u0026 response filters. Note the order which you specify filters that are of the same priority level:\n\n``` swift\nserver.setRequestFilters([pturnstile.requestFilter])\nserver.setResponseFilters([pturnstile.responseFilter])\n\nserver.setRequestFilters([(authFilter, .high)])\n```\n\nNow, set the port, static files location, and start the server:\n\n``` swift\n// Set a listen port of 8181\nserver.serverPort = 8181\n\n// Where to serve static files from\nserver.documentRoot = \"./webroot\"\n\ndo {\n\t// Launch the HTTP server.\n\ttry server.start()\n} catch PerfectError.networkError(let err, let msg) {\n\tprint(\"Network error thrown: \\(err) \\(msg)\")\n}\n```\n\n\n## Issues\n\nWe use JIRA for all bugs and support related issues, therefore the GitHub issues has been disabled.\n\nIf you find a mistake, bug, or any other helpful suggestion you'd like to make on the docs please head over to [http://jira.perfect.org:8080/servicedesk/customer/portal/1](http://jira.perfect.org:8080/servicedesk/customer/portal/1) and raise it.\n\nA comprehensive list of open issues can be found at [http://jira.perfect.org:8080/projects/ISS/issues](http://jira.perfect.org:8080/projects/ISS/issues)\n\n\n\n## Further Information\nFor more information on the Perfect project, please visit [perfect.org](http://perfect.org).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-turnstile-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectlysoft%2Fperfect-turnstile-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-turnstile-mongodb/lists"}