perfect.org
Documentation
|
API Documentation
PerfectLib
Perfect-CouchDB
Perfect-CURL
Perfect-Filemaker
Perfect-GoogleAnalytics-MeasurementProtocol
Perfect-HTTP
Perfect-HTTPServer
Perfect-Logger
Perfect-MongoDB
Perfect-Mustache
Perfect-MySQL
Perfect-MariaDB
Perfect-Net
Perfect-Notifications
Perfect-PostgreSQL
Perfect-Redis
Perfect-RequestLogger
Perfect-Session
Perfect-Session-SQLite
Perfect-Session-CouchDB
Perfect-Session-MySQL
Perfect-Session-PostgreSQL
Perfect-SQLite
Perfect-Thread
Perfect-WebSockets
Perfect-XML
Perfect-Zip
Turnstile-Perfect
Perfect-Turnstile-CouchDB
Perfect-Turnstile-MySQL
Perfect-Turnstile-PostgreSQL
Perfect-Turnstile-SQLite
StORM
CouchDB-StORM
MySQL-StORM
Postgres-StORM
SQLite-StORM
Module: Perfect-Session
extension
HTTPRequest
var
public(set) public var session: PerfectSession
extension
HTTPRequest
func
getCookie(name: String) -> String?
struct
SessionMemoryDriver
var
requestFilter: (HTTPRequestFilter, HTTPFilterPriority)
var
responseFilter: (HTTPResponseFilter, HTTPFilterPriority)
func
init()
class
SessionMemoryFilter
func
init()
extension
SessionMemoryFilter
func
filter(request: HTTPRequest, response: HTTPResponse, callback: (HTTPRequestFilterResult) -> ())
extension
SessionMemoryFilter
func
filterHeaders(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ())
Called once before headers are sent to the client.
func
filterBody(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ())
Called zero or more times for each bit of body data which is sent to the client.
struct
MemorySessions
static var
sessions = [String: PerfectSession]()
Dictionary of sessions
static func
func save(session: PerfectSession)
static func
func start(_ request: HTTPRequest) -> PerfectSession
static func
func destroy(token: String)
Deletes the session for a session identifier.
static func
func resume(token: String) throws -> PerfectSession
struct
PerfectSession
Holds the session information in memory for duration of request
var
token = ""
Token (session id)
var
userid = ""
Associated UserID. Optional to populate
var
created = 0
Date created, as an Int
var
updated = 0
Date updated, as an Int
var
idle = SessionConfig.idle
Idle time set at last update
var
data = [String: Any]()
Data held in storage associated with session
var
ipaddress = ""
IP Address of Session
var
useragent = ""
UserAgent of Session
var
_state = "recover"
func
init()
When creating a new session, the "created" and "updated" properties are set
func
tojson() -> String
Makes a JSON Encoded string
func
mutating func fromjson(_ str : String)
Sets the data property to a [String:Any] from JSON
func
mutating func touch()
updates the "updated" property
func
isValid(_ request:HTTPRequest) -> Bool
Compares the timestamps and idle to determine if session has expired
struct
SessionConfig
Configuration parameters for the session
static var
name = "PerfectSession"
For reference name in a cookie only
static var
cookieDomain = ""
Make cookie domain-specific
static var
cookiePath = "/"
Make cookie path-specific
static var
cookieSecure = false
Make cookie secure
static var
cookieHTTPOnly = true
Make cookie HTTP Only
static var
cookieSameSite:HTTPCookie.SameSite = .strict
Make cookie sameSite Only
.lax: Cross-site usage is allowed
.strict: The cookie is withheld with any cross-site usage
static var
idle = 86400
Idle time expiry of the session
static var
storage: SessionStorage = .memory
Session Storage Type
static var
userAgentLock = false
Session Locked to User Agent
static var
IPAddressLock = false
Session Locked to IP Address
static var
couchDatabase = "sessions"
CouchDB-Specific option
static var
mongoCollection = "sessions"
MongoDB-Specific option
enum
SessionStorage
Simple switch between memory session and database storage engines
case
memory, database
case
memory, database
protocol
PerfectSessionManager
PerfectSessionManager is a component that manages sessions.
Create a class conforming to this protocol that saves, resumes and destroys session objects.
func
resume(token: String) throws -> PerfectSession
Retrieves the session
func
save(session: PerfectSession)
Saves a session.
func
destroy(token: String)
Destroys the session.
struct
InvalidSessionError: Error
var
description = "Invalid session ID"
func
init()
class
BCrypt
static func
func hash(password: String, salt: BCryptSalt = BCryptSalt()) -> String
Hashes the password (using the UTF8 encoding) with the specified salt.
static func
func verify(password: String, matchesHash hash: String) throws -> Bool
Validates that the password matches the hash.
struct
BCryptSalt
Represents a salt object used in BCrypt
var
scheme: String
The MCF salt format. eg, 2, 2a, 2b
var
cost: Int
The cost factor for the BCrypt salt
var
data: [UInt8]
The raw salt data
var
string: String
String representation of the BCrypt Salt
func
init(string: String) throws
Creates a new salt object from a salt string or BCrypt MCF hash. Throws BCryptError if it's an invalid salt.
func
init(cost: Int = 10)
Creates a new random salt with the specified cost factor. Default cost factor of 10, which is probably
~100 ms to hash a password on a modern CPU.
struct
BCryptError: Error, CustomStringConvertible
BCrypt Error generated from parsing a bad BCrypt hash or salt.
var
description = "Invalid Hash or Salt"
BCrypt Error generated from parsing a bad BCrypt hash or salt.
protocol
Random
Random is a pluggable random number generator that depends on the OS provided.
func
random(numBytes: Int) -> [UInt8]
Get a byte array of random UInt8s
var
int8: Int8 { get }
Get a random int8
var
uint8: UInt8 { get }
Get a random uint8
var
int16: Int16 { get }
Get a random int16
var
uint16: UInt16 { get }
Get a random uint16
var
int32: Int32 { get }
Get a random int32
var
uint32: UInt32 { get }
Get a random uint32
var
int64: Int64 { get }
Get a random int64
var
uint64: UInt64 { get }
Get a random uint64
var
int: Int { get }
Get a random int
var
uint: UInt { get }
Get a random uint
var
secureToken: String { get }
Get a random string usable for authentication purposes
class
URandom: Random
URandom represents a file connection to /dev/urandom on Unix systems.
/dev/urandom is a cryptographically secure random generator provided
by the OS.
func
init()
Initialize URandom
func
random(numBytes: Int) -> [UInt8]
Get a byte array of random UInt8s
var
int8: Int8
Get a random int8
var
uint8: UInt8
Get a random uint8
var
int16: Int16
Get a random int16
var
uint16: UInt16
Get a random uint16
var
int32: Int32
Get a random int32
var
uint32: UInt32
Get a random uint32
var
int64: Int64
Get a random int64
var
uint64: UInt64
Get a random uint64
var
int: Int
Get a random int
var
uint: UInt
Get a random uint
var
secureToken: String
Get a random string usable for authentication purposes