amelinium.http.middleware.session

added in 1.0.0

amelinium service, session middleware.

active

(active src)(active src session-key)

Returns the :active (last active time) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

active-valid?

(active-valid? src)(active-valid? src session-key)

Returns true if the given session has valid last-active time. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

allow-expired

(allow-expired src)(allow-expired src session-key)

Temporarily marks expired session as valid. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

allow-hard-expired

(allow-hard-expired src)(allow-hard-expired src session-key)

Temporarily mark hard-expired session as valid.

allow-soft-expired

(allow-soft-expired src)(allow-soft-expired src session-key)

Temporarily mark soft-expired session as valid. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

any-id

(any-id src)(any-id src session-key)

Returns a session ID from the given source and optional session key. May return a value of the :id or :err-id field. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

calc-expired?

(calc-expired? src session-key)(calc-expired? src)

Returns true if the given session is expired by performing calculations on its last-active time and a configured timeout. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

calc-hard-expired?

(calc-hard-expired? src session-key)(calc-hard-expired? src)

Returns true if the given session is hard-expired by performing calculations on its last-active time and a configured timeout. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

calc-soft-expired?

(calc-soft-expired? src session-key)(calc-soft-expired? src)

Returns true if the given session is soft-expired by performing calculations on its last-active time and a configured timeout. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

check-encrypted

(check-encrypted plain-token encrypted-token-b64-str)

Checks if the encrypted token given as encrypted-token-b64-str matches the given plain-token. Verifies its correctness by re-encrypting it with the same salt parameter as it was used when original token was created.

config

(config src)(config src session-key)

Returns a SessionConfig object from the given source and optional session key. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

config?

(config? v)

Returns true if the given value v is an instance of SessionConfig record.

control

(control src)(control src session-key)

Returns a SessionControl object from the given source and optional session key. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

control?

(control? v)(control? v k)

Returns true if the given value is an instance of a class which fully satisfies SessionControl protocol (directly or indirectly). If the indirect testing is required, the tested object must be convertable to a control object (as described by the SessionControl protocol).

correct-state?

(correct-state? state-result)(correct-state? src ip-address)(correct-state? src session-key ip-address)

Returns true if a session exists and its state is correct. Never throws an exception.

create

(create src user-id user-email ip-address)

Creates a new session and puts it into a database. Returns a session record.

created

(created src)(created src session-key)

Returns the :created (creation time) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

created-valid?

(created-valid? src)(created-valid? src session-key)

Returns true if the given session has valid creation time. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

db-id

(db-id src)(db-id src session-key)

Returns a database session ID from a session object or any other value which can be transformed into a session.

db-sid

(db-sid obj)

Extracts a database session ID from a secure session object, a map, or session ID. Works with session objects, maps, strings and identifiers (symbols and keywords).

db-sid-smap

(db-sid-smap smap)

Extracts a database session ID from a secure session object. Tries :db-id field and if that fails uses :id or :err-id field of a session object and parses its string representation to obtain only its first part (if it consists of multiple parts separated with a dash character).

db-sid-str

(db-sid-str sid)

Extracts a database session ID from a secure session ID.

db-token

(db-token src)(db-token src session-key)

Returns the :db-token (security token in a database-suitable form) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

del-all-vars!

(del-all-vars! src session-key)(del-all-vars! src)

Deletes all session variables which belong to a user (is single-session? configuration option is true) or just variables for this session (if single-session? configuration option is false).

del-session-vars!

(del-session-vars! src session-key)(del-session-vars! src)

Deletes all session variables.

del-user-vars!

(del-user-vars! src session-key)(del-user-vars! src)

Deletes all session variables which belong to a user across all sessions.

del-var!

(del-var! src var-name)(del-var! src session-key var-name)

Deletes a session variable var-name.

del-vars!

(del-vars! src var-names)(del-vars! src session-key var-names)

Deletes a session variables from var-names.

delete!

(delete! src)(delete! src session-key)

Deletes a session from a database. The session is identified by a session object or any other value which can be transformed into a session (src) with optional key (session-key) if this object is associative. Session variables are also removed.

delete-all!

(delete-all! src)(delete-all! src session-key)(delete-all! src user-id)(delete-all! src session-key user-id)(delete-all! ctrl user-id user-email ip-address)

Deletes all user sessions from a database. The sessions are identified by a user ID derived from a session object, any other value which can be transformed into a session object (src) with optional session key for associative structures, or a user ID given directly (user-id). In the last case session will only be used to obtain control object so it may be a default session without any valid identifiers in it. Session variables are also removed.

delete-session-by-id

(delete-session-by-id opts db table db-sid)

Deletes session identified with db-sid from a database db. Returns a sequence a map indicating deleted session properties, having keys :id, :user-id, :user-email and :ip.

delete-session-vars

(delete-session-vars opts db sessions-table variables-table db-sid)

Deletes session variables of a session identified with db-sid from a database db. Returns the number of affected rows in case of success.

delete-sessions-by-uid

(delete-sessions-by-uid opts db table user-id)

Deletes sessions belonging to a used user-id from a database db. Returns a sequence of maps indicating deleted session rows, having keys :id, :user-id, :user-email and :ip.

delete-user-vars

(delete-user-vars opts db sessions-table variables-table user-id)

Deletes session variables which belong to a user user-id from a database db. Returns the number of affected rows in case of success.

empty

(empty src)(empty src session-key)

Returns an empty session record. Optional session-key may be given to express a key in associative structure (defaults to :session) used to perform a session lookup used to access the control object.

empty?

(empty? src)(empty? src session-key)

Returns false is src contains a session or is a session, and the session has usable identifier set (:id or :err-id field is set) or has the :error field set. Optional session-key may be given to express a key in associative structure (defaults to :session).

encrypt

(encrypt plain-token)

Encrypts the given plain-token with Scrypt algorithm using randomly generated salt. Returns a Base64-encoded string with salt and encrypted password separated with a dollar character. The encrypted password is a result of applying one-way hashing function to the plain-token and salt.

err-id

(err-id src)(err-id src session-key)

Returns an error session ID from the given source and optional session key. Works only on invalid sessions having :err-id field set. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

error

(error src)(error src session-key)

Returns the :error (session error) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

error?

(error? src)(error? src session-key)

Returns true if the :error (session error) property of a session is not nil. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

expired?

(expired? src)(expired? src session-key)

Returns true if the given session is marked as expired. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

fetch-var!

(fetch-var! src var-name)(fetch-var! src session-key var-name)

Like get-var but removes session variable after it is successfully read from a database. Variable is not removed if there was a problem with reading or de-serializing it.

fetch-vars!

(fetch-vars! src var-names)(fetch-vars! src session-key var-names)

Like get-vars but removes session variable after it is successfully read from a database. Variables are not removed if there was a problem with reading or de-serializing them.

get-last-active

(get-last-active opts db table sid-db remote-ip)

Returns last active time of a session identified with sid-db and selected from a database db.

get-session-by-id

(get-session-by-id opts db table sid-db remote-ip)

Standard session getter. Uses db to connect to a database and gets data identified by sid from a table table. Returns a map.

get-session-by-key

(get-session-by-key req)(get-session-by-key req session-key)

Obtains a session object from associative structure (usually a request map req) by looking for a value associated with a session key returned by calling get-session-key.

get-session-key

(get-session-key req)(get-session-key req session-key)

Returns a session key as keyword by reading the given session-key, and if it is nil or false by getting a value associated with the :session-key route data key of the req, and if that fails by returning the :session keyword.

get-var

(get-var src var-name)(get-var src session-key var-name)

Gets a session variable and de-serializes it into a Clojure data structure.

get-variable-failed?

(get-variable-failed? v)

Returns true if the value v obtained from a session variable indicates that it actually could not be successfully fetched from a database.

get-vars

(get-vars src var-names)(get-vars src session-key var-names)

Gets session variables and de-serializes them into a Clojure data structures.

handler

(handler src sid db-sid remote-ip)(handler src session-key sid db-sid remote-ip)(handler ctrl session-key id-field sid db-sid remote-ip)

Gets session data from a database and processes them with session control functions and configuration options to generate a session record. When the record is created it is validated for errors and expiration times.

The results of calling this function may be memoized to reduce database hits. Note that if the cache TTL is larger than the remaining expiration time for a session, it may be required to refresh the times and re-validate the session object.

hard-expired?

(hard-expired? src)(hard-expired? src session-key)

Returns true if the given session is marked as hard-expired. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

id

(id src)(id src session-key)

Returns a session ID from the given source and optional session key. Works only on valid sessions having :id field set. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

id-field

(id-field src)(id-field src session-key)

Returns the :id-field (field ID) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

identify-session-path-compile

(identify-session-path-compile path)

Returns a function which takes a request map and returns a session ID.

inject

(inject dst smap)(inject dst smap session-key)

Returns an object updated with session record of type Session under an optional session-key if session is to be put into an associative structure (defaults to :session).

insecure?

(insecure? src)(insecure? src session-key)

Checks if session is not secure where it should be. If :secured? option is not enabled in configuration, it returns false. If :secure? flag is set to a falsy value, it returns false. If there is no session, it returns true.

invalid?

(invalid? src)(invalid? src session-key)

Returns negated value of the :valid? (validity) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map. If there is no session, returns true.

invalidate-cache!

(invalidate-cache! src)(invalidate-cache! src session-key)(invalidate-cache! src session-key remote-ip)(invalidate-cache! ctrl sid remote-ip)

Invalidates cache for the specific session. The src can be a session object, a request (context) map with optional session-key as a second argument, or a SessionControl object with a session ID (sid). Remote IP address (used to match cache entries along with session database ID) will be obtained from the session object if not given or nil.

invalidate-multi

(invalidate-multi ctrl sessions)

Invalidates cache for multiple sessions by extracting their database IDs from maps. The sessions argument should be a sequence of maps or a sequence of Session objects. If they are maps they should have :id keys with associated database IDs. First argument should be a SessionControl object.

ip

(ip src)(ip src session-key)

Returns the :ip (IP address) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

ip-state

(ip-state smap user-id user-email remote-ip)

Returns session IP state for the given session. If IP address is correct, returns nil, otherwise a SessionError record.

make

(make secured? ctrl skey id-field user-id user-email ip)

Generates a session ID and adds it to newly created session object. The secured? flag can be set to generate secure session ID.

mem-cache-almost-expired?

(mem-cache-almost-expired? ctrl key)(mem-cache-almost-expired? ctrl key knee)(mem-cache-almost-expired? ctrl key knee t)

Returns true if an entry associated with the given key key in a TTL map of a current handler cache has almost expired. The knee argument should be a number of milliseconds (defaults to 1000 if not given) to be added to a current time in order to shift it forward. If the entry does not exist, true is returned. Negative knee values will cause it to have 0 impact. Optional argument t may be given with a time expressed as an instant (java.time.Instant) to be used as a source of current time.

mem-cache-expired?

(mem-cache-expired? ctrl key t)(mem-cache-expired? ctrl key)

Returns true if an entry associated with the given key key in a TTL map of a current handler cache has expired. If the entry does not exist, true is returned. Optional argument t may be given with a time expressed as an instant (java.time.Instant) to be used as a source of current time.

mem-cache-time-left

(mem-cache-time-left ctrl key)(mem-cache-time-left ctrl key t)

Returns a time left to a cache expiry for an entry associated with the given key key in a TTL map of a current handler cache. The returned object is of type java.time.Duration. If the entry does not exist, nil is returned. Optional t argument may be given to express current time as java.time.Instant.

mem-cache-time-passed

(mem-cache-time-passed ctrl key)(mem-cache-time-passed ctrl key t)

Returns a time which passed till now from the creation of an entry associated with the given key key in a TTL map of a current handler cache. The returned object is of type java.time.Duration and its value may exceed the configured TTL when there was no cache update nor eviction for the entry. Optional t argument may be given to express current time as java.time.Instant.

mem-ctime

(mem-ctime ctrl key)

Retrieves an entry creation time (in milliseconds) associated with the given key in a TTL map of a current handler cache. If the entry does not exist, nil is returned.

mem-etime

(mem-etime ctrl key)

Retrieves an expiration time (in milliseconds since the beginning of the Unix epoch) of an entry identified by the given key key in a TTL map of a current handler cache. It is calculated by adding cache’s TTL to entry’s creation time. If the entry does not exist, nil is returned.

mkbad

(mkbad smap k v a b c d & pairs)(mkbad smap k v a b c d)(mkbad smap k v a b)(mkbad smap k v)(mkbad smap)

Marks session as invalid and sets :err-id field’s value to the value of :id field, then sets :id to nil. The given object should be a session.

mkgood

(mkgood src session-key)(mkgood src)

Marks the given session smap as valid by setting :valid? field to true, :expired? and :hard-expired? fields to false, and :error field to nil. The given object should be a session.

not-empty-of

(not-empty-of src)(not-empty-of src session-key)

Returns a session if src contains a session or is a session, and the session has usable identifier set (:id or :err-id field is set) or has the :error field set. Optional session-key may be given to express a key in associative structure (defaults to :session). Returns nil if session is not usable (does not have :id, :err-id not :error set).

not-empty?

(not-empty? src)(not-empty? src session-key)

Returns true is src contains a session or is a session, and the session has usable identifier set (:id or :err-id field is set) or has the :error field set. Optional session-key may be given to express a key in associative structure (defaults to :session).

of

(of src)(of src session-key)

Returns a session record of type Session on a basis of configuration source provided and an optional session-key if session must be looked in an associative structure (defaults to :session).

one-second

process

(process ctrl malformed-session empty-session cache-margin expires-in req)

Takes a session control object, predefined session objects (for malformed and empty session), expiration settings and a request map, and validates a session against the database or memoized session data. Returns a session map or dummy session map if session was not obtained (session ID was not found in a database).

process-handler

(process-handler ctrl cache-margin expires-in sid remote-ip)

Session processing handler wrapper. For the given session ID sid and remote IP address remote-ip it runs handle (from SessionControl protocol) using ctrl. Then it quickly checks if the refresh is needed (by checking whether session is expired or by calling needs-refresh?). If re-reading expiration time from a database is required, it will do it and check if the time really has changed. In such case the session data in will be updated (by refreshing just the last active time or by handling it again after cache eviction).

prolong

(prolong src)(prolong src ip-address)(prolong src session-key ip-address)

Re-validates session by updating its timestamp and re-running validation.

prolonged?

(prolonged? src)(prolonged? src session-key)

Returns true if the given session has just been prolonged. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

put-var!

(put-var! src var-name value)(put-var! src session-key var-name value)

Puts a session variable var-name with a value value into a database.

put-vars!

(put-vars! src pairs)(put-vars! src session-key pairs)

Puts session variables with associated values (var-names-values) expressed as pairs into a database.

refresh-times

(refresh-times src)(refresh-times src session-key)(refresh-times src remote-ip)(refresh-times src session-key remote-ip)

Checks a last-active time of the session. If the time left to expiration is smaller than the cache TTL then the session record will be updated using a database query and session cache invalidated for a session ID. Uses pre-calculated value stored in the :cache-margin field of a session record (see calc-cache-margin for more info).

salt-splitter

scrypt-options

secure?

(secure? src)(secure? src session-key)

Checks if a session is secure according to configured security level. If :secured? option is not enabled in configuration, it returns true. If :secure? flag is set to a truthy value, it returns it. If there is no session, it returns false.

security-failed?

(security-failed? src)(security-failed? src session-key)

Checks if the additional security token was validated incorrectly unless the session is not secure (in such case it returns false). Does not test if session should be secured; to check it, use secure? or insecure?.

security-passed?

(security-passed? src)(security-passed? src session-key)

Checks if the additional security token was validated correctly or there was not a need to validate it because the session is not secure (in such case returns true). Does not test if session should be secured; to check it, use secure? or insecure?.

session-key

(session-key src)(session-key src session-key)

Returns the :session-key (session key) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

session?

(session? v)

Returns true if the given value v is an instance of Session record.

set-session

(set-session opts db table smap)

Puts session data into a database db. Returns the number of updated rows in case of success.

sid-match

sid-valid

(sid-valid sid)

Returns the given sid if it is valid after converting it to a string. Otherwise it returns nil.

sid-valid?

(sid-valid? sid)

Returns true if the given session ID is valid.

soft-expired?

(soft-expired? src)(soft-expired? src session-key)

Returns true if the given session is marked as soft-expired. The session can be passed directly or as a request map from which it should be extracted (in such case the optional session-key argument may be given).

state

(state smap ip-address)

Returns session state. If there is anything wrong, returns a SessionError record. Otherwise it returns nil. Unknown session detection is performed by checking if a value associated with the :id key is nil and a value associated with the :err-id key is not nil. First argument must be a session object of type Session.

token-splitter

update-last-active

(update-last-active opts db table sid-db remote-ip)(update-last-active opts db table sid-db remote-ip t)

Updates session last active time on a database db. Returns the number of updated rows in case of success.

user-email

(user-email src)(user-email src session-key)

Returns the :user-email (user e-mail) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

user-id

(user-id src)(user-id src session-key)

Returns the :user-id (user ID) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map.

valid-of

(valid-of src)(valid-of src session-key)

Returns a session if src contains a session or is a session, and the session is valid. Optional session-key may be given to express a key in associative structure (defaults to :session). Returns nil if session is not valid.

valid?

(valid? src)(valid? src session-key)

Returns the :valid? (validity) property of a session. The src can be a session record (Session) or any object which satisfies the Sessionable protocol and can be converted to a session. Optional session-key can be given when passing a request map. If there is no session, returns false.

when-sid-valid

macro

(when-sid-valid sid & body)

Conditionally evaluates expressions from body in an implicit do when the session ID given as sid is valid. Otherwise it returns nil without evaluating anything.

wrap-session

(wrap-session k config)

Session maintaining middleware.