{"id":20037997,"url":"https://github.com/perfectlysoft/perfect-localauthentication-postgresql","last_synced_at":"2025-05-05T06:31:57.216Z","repository":{"id":56692837,"uuid":"89729264","full_name":"PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL","owner":"PerfectlySoft","description":"Local Authentication processes and routes","archived":false,"fork":false,"pushed_at":"2020-10-24T20:30:13.000Z","size":1564,"stargazers_count":6,"open_issues_count":4,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-19T17:58:13.888Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"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.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-28T17:28:25.000Z","updated_at":"2022-11-19T11:16:43.000Z","dependencies_parsed_at":"2022-08-15T23:30:42.490Z","dependency_job_id":null,"html_url":"https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-PostgreSQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-PostgreSQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-PostgreSQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-PostgreSQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerfectlySoft","download_url":"https://codeload.github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224429234,"owners_count":17309673,"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":[],"created_at":"2024-11-13T10:24:35.214Z","updated_at":"2024-11-13T10:24:35.820Z","avatar_url":"https://github.com/PerfectlySoft.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Perfect Local Authentication (PostgreSQL) [简体中文](README.zh_CN.md)\n\n[![Perfect logo](http://www.perfect.org/github/Perfect_GH_header_854.jpg)](http://perfect.org/get-involved.html)\n\n[![Perfect logo](http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg)](https://github.com/PerfectlySoft/Perfect)\n[![Perfect logo](http://www.perfect.org/github/Perfect_GH_button_2_Git.jpg)](https://gitter.im/PerfectlySoft/Perfect)\n[![Perfect logo](http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg)](https://twitter.com/perfectlysoft)\n[![Perfect logo](http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg)](http://perfect.ly)\n\n\n[![Swift 3.0](https://img.shields.io/badge/Swift-3.0-orange.svg?style=flat)](https://developer.apple.com/swift/)\n[![Platforms OS X | Linux](https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat)](https://developer.apple.com/swift/)\n[![License Apache](https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat)](http://perfect.org/licensing.html)\n[![Twitter](https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat)](http://twitter.com/PerfectlySoft)\n[![Join the chat at https://gitter.im/PerfectlySoft/Perfect](https://img.shields.io/badge/Gitter-Join%20Chat-brightgreen.svg)](https://gitter.im/PerfectlySoft/Perfect?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![Slack Status](http://perfect.ly/badge.svg)](http://perfect.ly) [![GitHub version](https://badge.fury.io/gh/PerfectlySoft%2FPerfect-CURL.svg)](https://badge.fury.io/gh/PerfectlySoft%2FPerfect-CURL)\n\n## Perfect Local Authentication (PostgreSQL) Library\n\n\nThis package provides Local Authentication libraries for projects that require locally stored and handled authentication.\n\nFull documentation can be found at [http://www.perfect.org/docs/authentication.html](http://www.perfect.org/docs/authentication.html)\n\nA template application can be found at [https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template](https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template), providing a fully functional starting point, as well as demonstrating the usage of the system.\n\nThis package builds with Swift Package Manager and is part of the [Perfect](https://github.com/PerfectlySoft/Perfect) project. It was written to be stand-alone and so does not require PerfectLib or any other components.\n\nEnsure you have installed and activated the latest Swift 3.x tool chain.\n\n## Adding to your project\n\nAdd this project as a dependency in your Package.swift file.\n\n``` swift\n.Package(url: \"https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL.git\", majorVersion: 1)\n```\n\nTo then use the LocalAuthentication module in your code:\n\n``` swift\nimport LocalAuthentication\n```\n\n## Configuration\n\nIt is important to configure the following in main.swift to set up database and session configuration:\n\nImport the required modules:\n\n``` swift\nimport PerfectSession\nimport PerfectSessionPostgreSQL\nimport PerfectCrypto\nimport LocalAuthentication\n```\n\nInitialize PerfectCrypto:\n\n``` swift\nlet _ = PerfectCrypto.isInitialized\n```\n\nNow set some defaults:\n\n``` swift\n// Used in email communications\n// The Base link to your system, such as http://www.example.com/\nvar baseURL = \"\"\n\n// Configuration of Session\nSessionConfig.name = \"perfectSession\" // \u003c-- change\nSessionConfig.idle = 86400\nSessionConfig.cookieDomain = \"localhost\" //\u003c-- change\nSessionConfig.IPAddressLock = false\nSessionConfig.userAgentLock = false\nSessionConfig.CSRF.checkState = true\nSessionConfig.CORS.enabled = true\nSessionConfig.cookieSameSite = .lax\n```\n\nDetailed Session configuration documentation can be dound at [https://www.perfect.org/docs/sessions.html](https://www.perfect.org/docs/sessions.html)\n\nThe database and email configurations should be set as follows (if using JSON file config):\n\n``` swift\nlet opts = initializeSchema(\"./config/ApplicationConfiguration.json\") // \u003c-- loads base config like db and email configuration\nhttpPort = opts[\"httpPort\"] as? Int ?? httpPort\nbaseURL = opts[\"baseURL\"] as? String ?? baseURL\n```\n\nOtherwise, these will need to be set equivalent to this function [https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL/blob/master/Sources/LocalAuthentication/Schema/InitializeSchema.swift](https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL/blob/master/Sources/LocalAuthentication/Schema/InitializeSchema.swift).\n\nSet the session driver:\n\n``` swift\nlet sessionDriver = SessionPostgresDriver()\n```\n\n### Request \u0026 Response Filters\n\nThe following two session filters need to be added to your server config:\n\n``` swift\n// (where filter is a [[String: Any]] object)\nfilters.append([\"type\":\"request\",\"priority\":\"high\",\"name\":SessionPostgresFilter.filterAPIRequest])\nfilters.append([\"type\":\"response\",\"priority\":\"high\",\"name\":SessionPostgresFilter.filterAPIResponse])\n```\n\nFor example, see [https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Filters.swift](https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Filters.swift)\n\n### Add routes for login, register etc\n\nThe following routes can be added as needed or customized to add login, logout, register:\n\n``` swift\n// Login\nroutes.append([\"method\":\"get\", \"uri\":\"/login\", \"handler\":Handlers.login]) // simply a serving of the login GET\nroutes.append([\"method\":\"post\", \"uri\":\"/login\", \"handler\":LocalAuthWebHandlers.login])\nroutes.append([\"method\":\"get\", \"uri\":\"/logout\", \"handler\":LocalAuthWebHandlers.logout])\n\n// Register\nroutes.append([\"method\":\"get\", \"uri\":\"/register\", \"handler\":LocalAuthWebHandlers.register])\nroutes.append([\"method\":\"post\", \"uri\":\"/register\", \"handler\":LocalAuthWebHandlers.registerPost])\nroutes.append([\"method\":\"get\", \"uri\":\"/verifyAccount/{passvalidation}\", \"handler\":LocalAuthWebHandlers.registerVerify])\nroutes.append([\"method\":\"post\", \"uri\":\"/registrationCompletion\", \"handler\":LocalAuthWebHandlers.registerCompletion])\n\n// JSON\nroutes.append([\"method\":\"get\", \"uri\":\"/api/v1/session\", \"handler\":LocalAuthJSONHandlers.session])\nroutes.append([\"method\":\"get\", \"uri\":\"/api/v1/logout\", \"handler\":LocalAuthJSONHandlers.logout])\nroutes.append([\"method\":\"post\", \"uri\":\"/api/v1/register\", \"handler\":LocalAuthJSONHandlers.register])\nroutes.append([\"method\":\"login\", \"uri\":\"/api/v1/login\", \"handler\":LocalAuthJSONHandlers.login])\n```\n\nAn example can be found at [https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Routes.swift](https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Routes.swift)\n\n## Testing for authentication:\n\nThe user id can be accessed as follows:\n\n``` swift\nrequest.session?.userid ?? \"\"\n```\n\nIf a user id (i.e. logged in state) is required to access a page, code such as this could be used to detect and redirect:\n\n``` swift\nlet contextAuthenticated = !(request.session?.userid ?? \"\").isEmpty\nif !contextAuthenticated { response.redirect(path: \"/login\") }\n```\n\n## Further Information\nFor more information on the Perfect project, please visit [perfect.org](http://perfect.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-localauthentication-postgresql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectlysoft%2Fperfect-localauthentication-postgresql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-localauthentication-postgresql/lists"}