https://github.com/perfectlysoft/perfect-localauthentication-postgresql
Local Authentication processes and routes
https://github.com/perfectlysoft/perfect-localauthentication-postgresql
Last synced: about 1 year ago
JSON representation
Local Authentication processes and routes
- Host: GitHub
- URL: https://github.com/perfectlysoft/perfect-localauthentication-postgresql
- Owner: PerfectlySoft
- License: apache-2.0
- Created: 2017-04-28T17:28:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-10-24T20:30:13.000Z (over 5 years ago)
- Last Synced: 2024-10-19T17:58:13.888Z (over 1 year ago)
- Language: Swift
- Size: 1.49 MB
- Stars: 6
- Watchers: 5
- Forks: 11
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Perfect Local Authentication (PostgreSQL) [简体中文](README.zh_CN.md)
[](http://perfect.org/get-involved.html)
[](https://github.com/PerfectlySoft/Perfect)
[](https://gitter.im/PerfectlySoft/Perfect)
[](https://twitter.com/perfectlysoft)
[](http://perfect.ly)
[](https://developer.apple.com/swift/)
[](https://developer.apple.com/swift/)
[](http://perfect.org/licensing.html)
[](http://twitter.com/PerfectlySoft)
[](https://gitter.im/PerfectlySoft/Perfect?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](http://perfect.ly) [](https://badge.fury.io/gh/PerfectlySoft%2FPerfect-CURL)
## Perfect Local Authentication (PostgreSQL) Library
This package provides Local Authentication libraries for projects that require locally stored and handled authentication.
Full documentation can be found at [http://www.perfect.org/docs/authentication.html](http://www.perfect.org/docs/authentication.html)
A 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.
This 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.
Ensure you have installed and activated the latest Swift 3.x tool chain.
## Adding to your project
Add this project as a dependency in your Package.swift file.
``` swift
.Package(url: "https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL.git", majorVersion: 1)
```
To then use the LocalAuthentication module in your code:
``` swift
import LocalAuthentication
```
## Configuration
It is important to configure the following in main.swift to set up database and session configuration:
Import the required modules:
``` swift
import PerfectSession
import PerfectSessionPostgreSQL
import PerfectCrypto
import LocalAuthentication
```
Initialize PerfectCrypto:
``` swift
let _ = PerfectCrypto.isInitialized
```
Now set some defaults:
``` swift
// Used in email communications
// The Base link to your system, such as http://www.example.com/
var baseURL = ""
// Configuration of Session
SessionConfig.name = "perfectSession" // <-- change
SessionConfig.idle = 86400
SessionConfig.cookieDomain = "localhost" //<-- change
SessionConfig.IPAddressLock = false
SessionConfig.userAgentLock = false
SessionConfig.CSRF.checkState = true
SessionConfig.CORS.enabled = true
SessionConfig.cookieSameSite = .lax
```
Detailed Session configuration documentation can be dound at [https://www.perfect.org/docs/sessions.html](https://www.perfect.org/docs/sessions.html)
The database and email configurations should be set as follows (if using JSON file config):
``` swift
let opts = initializeSchema("./config/ApplicationConfiguration.json") // <-- loads base config like db and email configuration
httpPort = opts["httpPort"] as? Int ?? httpPort
baseURL = opts["baseURL"] as? String ?? baseURL
```
Otherwise, 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).
Set the session driver:
``` swift
let sessionDriver = SessionPostgresDriver()
```
### Request & Response Filters
The following two session filters need to be added to your server config:
``` swift
// (where filter is a [[String: Any]] object)
filters.append(["type":"request","priority":"high","name":SessionPostgresFilter.filterAPIRequest])
filters.append(["type":"response","priority":"high","name":SessionPostgresFilter.filterAPIResponse])
```
For 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)
### Add routes for login, register etc
The following routes can be added as needed or customized to add login, logout, register:
``` swift
// Login
routes.append(["method":"get", "uri":"/login", "handler":Handlers.login]) // simply a serving of the login GET
routes.append(["method":"post", "uri":"/login", "handler":LocalAuthWebHandlers.login])
routes.append(["method":"get", "uri":"/logout", "handler":LocalAuthWebHandlers.logout])
// Register
routes.append(["method":"get", "uri":"/register", "handler":LocalAuthWebHandlers.register])
routes.append(["method":"post", "uri":"/register", "handler":LocalAuthWebHandlers.registerPost])
routes.append(["method":"get", "uri":"/verifyAccount/{passvalidation}", "handler":LocalAuthWebHandlers.registerVerify])
routes.append(["method":"post", "uri":"/registrationCompletion", "handler":LocalAuthWebHandlers.registerCompletion])
// JSON
routes.append(["method":"get", "uri":"/api/v1/session", "handler":LocalAuthJSONHandlers.session])
routes.append(["method":"get", "uri":"/api/v1/logout", "handler":LocalAuthJSONHandlers.logout])
routes.append(["method":"post", "uri":"/api/v1/register", "handler":LocalAuthJSONHandlers.register])
routes.append(["method":"login", "uri":"/api/v1/login", "handler":LocalAuthJSONHandlers.login])
```
An 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)
## Testing for authentication:
The user id can be accessed as follows:
``` swift
request.session?.userid ?? ""
```
If 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:
``` swift
let contextAuthenticated = !(request.session?.userid ?? "").isEmpty
if !contextAuthenticated { response.redirect(path: "/login") }
```
## Further Information
For more information on the Perfect project, please visit [perfect.org](http://perfect.org).