{"id":20038004,"url":"https://github.com/perfectlysoft/perfect-localauthentication-mysql","last_synced_at":"2025-05-05T06:31:59.501Z","repository":{"id":63920163,"uuid":"92545368","full_name":"PerfectlySoft/Perfect-LocalAuthentication-MySQL","owner":"PerfectlySoft","description":"Local Authentication, MySQL module","archived":false,"fork":false,"pushed_at":"2019-04-24T20:33:32.000Z","size":1538,"stargazers_count":4,"open_issues_count":2,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-15T00:09:42.330Z","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-05-26T20:15:50.000Z","updated_at":"2021-03-07T12:47:58.000Z","dependencies_parsed_at":"2023-01-14T14:00:52.498Z","dependency_job_id":null,"html_url":"https://github.com/PerfectlySoft/Perfect-LocalAuthentication-MySQL","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-MySQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-MySQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-MySQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-LocalAuthentication-MySQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerfectlySoft","download_url":"https://codeload.github.com/PerfectlySoft/Perfect-LocalAuthentication-MySQL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224429247,"owners_count":17309674,"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:37.570Z","updated_at":"2024-11-13T10:24:38.179Z","avatar_url":"https://github.com/PerfectlySoft.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Perfect Local Authentication (MySQL) [简体中文](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 (MySQL) 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 PostgreSQL version 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. While this template provides PostgreSQL integration, the principles still apply to the MySQL version.\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-MySQL.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 PerfectSessionMySQL\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-MySQL/blob/master/Sources/PerfectLocalAuthentication/Schema/InitializeSchema.swift](https://github.com/PerfectlySoft/Perfect-LocalAuthentication-MySQL/blob/master/Sources/PerfectLocalAuthentication/Schema/InitializeSchema.swift).\n\nSet the session driver:\n\n``` swift\nlet sessionDriver = SessionMySQLDriver()\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\":SessionMySQLFilter.filterAPIRequest])\nfilters.append([\"type\":\"response\",\"priority\":\"high\",\"name\":SessionMySQLFilter.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-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectlysoft%2Fperfect-localauthentication-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-localauthentication-mysql/lists"}