algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network::FileTokenStore Class Reference

A TokenStore in a private folder: one token file and one lock file per identity. POSIX only; not provided on Windows. More...

#include <algotradelib/src/network/file_token_store.hpp>

Inheritance diagram for algotradelib::network::FileTokenStore:
Collaboration diagram for algotradelib::network::FileTokenStore:

Public Member Functions

 FileTokenStore (std::string folder, std::chrono::milliseconds lock_wait=std::chrono::seconds(30))
 Constructs a store; nothing is created or opened until the first call.
Result< std::unique_ptr< Lock > > lock (const std::string &identity) override
 Takes the identity's lock, opening its lock file anew, so two callers in one process exclude each other as two processes do.
Result< std::optional< StoredToken > > load (const std::string &identity) override
 Reads the identity's token file.
std::optional< BrokerError > publish (const std::string &identity, const StoredToken &token) override
 Replaces the identity's token file, unless the stored token expires later.
std::optional< BrokerError > discard (const std::string &identity, const std::string &access_token) override
 Removes the identity's token file if it holds the given token.
std::optional< BrokerError > remove (const std::string &identity) override
 Removes the identity's token file; its lock file stays.

Detailed Description

A TokenStore in a private folder: one token file and one lock file per identity. POSIX only; not provided on Windows.

The folder is created (mode 0700) if it does not exist; its parent must. For identity ID the store keeps ID.token (JSON: the token and its expiry in nanoseconds since the epoch, replaced by writing ID.tmp, flushing it and renaming it) and ID.lock (an flock lock, never removed). The folder and every file must be owned by the user running the program and give no permission to group or others; a store that finds otherwise refuses and leaves the file as it was. A symbolic link at the folder's path or at a file's is not followed. Descriptors are closed on exec. A token file that is not well formed reads as no token.

Keep the folder on a local disk: flock over a network file system is unreliable. A lock held when the process forks without exec is held by the child too, until the child closes the descriptor or exits. The token is a bearer credential; the folder is as sensitive as the app secret.

Definition at line 43 of file file_token_store.hpp.

Constructor & Destructor Documentation

◆ FileTokenStore()

algotradelib::network::FileTokenStore::FileTokenStore ( std::string folder,
std::chrono::milliseconds lock_wait = std::chrono::seconds(30) )
inlineexplicit

Constructs a store; nothing is created or opened until the first call.

Parameters
folderThe folder.
lock_waitHow long lock waits for another holder before it fails.

Definition at line 50 of file file_token_store.hpp.

Member Function Documentation

◆ discard()

std::optional< BrokerError > algotradelib::network::FileTokenStore::discard ( const std::string & identity,
const std::string & access_token )
inlineoverridevirtual

Removes the identity's token file if it holds the given token.

Parameters
identityThe identity.
access_tokenThe token.
Returns
The store's error, or nothing.

Implements algotradelib::network::TokenStore.

Definition at line 133 of file file_token_store.hpp.

◆ load()

Result< std::optional< StoredToken > > algotradelib::network::FileTokenStore::load ( const std::string & identity)
inlineoverridevirtual

Reads the identity's token file.

Parameters
identityThe identity.
Returns
The token; nothing if there is no file or it is not well formed; or the store's error.

Implements algotradelib::network::TokenStore.

Definition at line 92 of file file_token_store.hpp.

◆ lock()

Result< std::unique_ptr< Lock > > algotradelib::network::FileTokenStore::lock ( const std::string & identity)
inlineoverridevirtual

Takes the identity's lock, opening its lock file anew, so two callers in one process exclude each other as two processes do.

Parameters
identityThe identity.
Returns
The held lock, or the store's error (lock not obtained in time after the wait).

Implements algotradelib::network::TokenStore.

Definition at line 59 of file file_token_store.hpp.

◆ publish()

std::optional< BrokerError > algotradelib::network::FileTokenStore::publish ( const std::string & identity,
const StoredToken & token )
inlineoverridevirtual

Replaces the identity's token file, unless the stored token expires later.

Parameters
identityThe identity.
tokenThe token.
Returns
The store's error, or nothing; the old file is left as it was on failure.

Implements algotradelib::network::TokenStore.

Definition at line 110 of file file_token_store.hpp.

◆ remove()

std::optional< BrokerError > algotradelib::network::FileTokenStore::remove ( const std::string & identity)
inlineoverridevirtual

Removes the identity's token file; its lock file stays.

Parameters
identityThe identity.
Returns
The store's error, or nothing.

Implements algotradelib::network::TokenStore.

Definition at line 156 of file file_token_store.hpp.


The documentation for this class was generated from the following file: