Module: Perfect-Session
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
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 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 userAgentLock = false
Session Locked to User Agent
static var IPAddressLock = false
Session Locked to IP Address
enum SessionStorage
Simple switch between memory session and database storage engines
protocol PerfectSessionManager
PerfectSessionManager is a component that manages sessions.
Create a class conforming to this protocol that saves, resumes and destroys session objects.
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