class PdoTokenStorage extends AbstractTokenStorage implements TokenStorageInterface, TokenStorageManagementInterface

This class uses a PDO database (MySQL, PostGreSQL or SQLite) to store the OAuth session variables.

This class is instantiated if the 'storage' option is set as follows: 'storage' => [ 'type' => 'pdo', 'dsn' => '< THE DATABASE DSN>' ]

Constants

CREATE_TABLE

Properties

protected OAuthClientInterface $client The Oauth client object using this storage. from AbstractTokenStorage
protected array $parameters Parameters of this token storage. from AbstractTokenStorage
protected string $sessionId The id of the session from AbstractTokenStorage
protected string $sessionPath The path on the server in which the cookie will be available on. from AbstractTokenStorage
private mixed|null $pdo

Methods

__construct(OAuthClientInterface $client, array $parameters = [])

Constructs a token storage object derived from this class

string
getSessionId()

Returns the id of the session

string
getSessionCookieName()

Returns the name of a cookie that will be used to store the session id regardless of the token storage system

string
getSessionCookie()

Returns the session id stored in a cookie regardless of the token storage system

string
getSessionPath()

Returns the path on the server in which the cookie will be available on

setSessionId(string $sessionId)

Sets the id of the session

setSessionPath(string $sessionPath)

Sets the path on the server in which the cookie will be available on

deleteSessionCookie()

Deletes the session cookie that holds the session id

initializeOAuthSession()

Creates an OAuthSession object and initializes it.

fetchOAuthSession()

Retrieves the OAuth session or create it if it does not exist

array
getProvidersInCookie()

Returns the OAuth configuration for all providers where the user has logged on.

void
addProviderInCookie()

Adds the OAuth configuration of the current provider to the list of providers where the user has logged on.

void
removeProviderFromCookie()

Removes the OAuth configuration of the current provider from the list of providers where the user has logged on.

bool
createOAuthSession(OAuthSession|null $session)

Creates and saves a new OAuth session

bool
getOAuthSession($sessionId, string $provider, OAuthSession|null $oauthSession)

Retrieves an OAuth session for a given provider

bool
saveOAuthSession(OAuthSession $session)

Saves a session for later use

bool
storeAccessToken(array $accessToken)

Store the values of the access token when it is succefully retrieved from the OAuth provider.

storeUserid(string $userId)

Stores the user ID returned by the OAuth provider

array|bool
getStoredAccessToken()

Retrieve the OAuth access token if it was already previously stored by the storeAccessToken method.

string|null
getStoredState()

Returns the stored state

string|null
getStoredNonce()

Returns the stored nonce

string|null
getStoredUserId()

Returns the stored user ID

bool
resetAccessToken()

Reset the access token to a state back when the user has not yet authorized the access to the OAuth provider API.

setOAuthSession(array $session)

Creates an OAuthSession object from an array of session variables.

bool
tableExists()

Checks if the OAuth session table exists

createTable()

Creates the OAuth session table

connect()

Connects to the database

bool
query($sql, $parameters, $results)

Executes a query on the session table

Details

in AbstractTokenStorage at line 60
__construct(OAuthClientInterface $client, array $parameters = [])

Constructs a token storage object derived from this class

Parameters

OAuthClientInterface $client
array $parameters Possible key values are :

  • type

    • session : usage of the superglobal $_SESSION to store the token
    • pdo : usage of a PDO database to store the token
    • cookie : usage of encrypted cookies to store the token
    • apcu : usage of the PHP APC User Cache (APCu) to store the token
  • key (only for type = cookie) : the key used to encrypt the cookie

  • dsn (only for type = pdo) : the information required to connect to the database.

in AbstractTokenStorage at line 70
protected string getSessionId()

Returns the id of the session

Return Value

string the id of the session

in AbstractTokenStorage at line 80
protected string getSessionCookieName()

Returns the name of a cookie that will be used to store the session id regardless of the token storage system

Return Value

string the name of the cookie

in AbstractTokenStorage at line 90
protected string getSessionCookie()

Returns the session id stored in a cookie regardless of the token storage system

Return Value

string

in AbstractTokenStorage at line 100
protected string getSessionPath()

Returns the path on the server in which the cookie will be available on

Return Value

string the path on the server

in AbstractTokenStorage at line 111
protected AbstractTokenStorage setSessionId(string $sessionId)

Sets the id of the session

Parameters

string $sessionId the id of the session

Return Value

AbstractTokenStorage

in AbstractTokenStorage at line 123
protected AbstractTokenStorage setSessionPath(string $sessionPath)

Sets the path on the server in which the cookie will be available on

Parameters

string $sessionPath the path on the server

Return Value

AbstractTokenStorage

in AbstractTokenStorage at line 132
protected deleteSessionCookie()

Deletes the session cookie that holds the session id

in AbstractTokenStorage at line 142
protected OAuthSession initializeOAuthSession()

Creates an OAuthSession object and initializes it.

Return Value

OAuthSession the initialized OAuthSession object

in AbstractTokenStorage at line 157
protected OAuthSession fetchOAuthSession()

Retrieves the OAuth session or create it if it does not exist

Return Value

OAuthSession the OAuthSession object

in AbstractTokenStorage at line 185
protected array getProvidersInCookie()

Returns the OAuth configuration for all providers where the user has logged on.

Return Value

array the OAuth configuration by provider

in AbstractTokenStorage at line 200
protected void addProviderInCookie()

Adds the OAuth configuration of the current provider to the list of providers where the user has logged on.

Return Value

void

in AbstractTokenStorage at line 218
protected void removeProviderFromCookie()

Removes the OAuth configuration of the current provider from the list of providers where the user has logged on.

Return Value

void

at line 98
bool createOAuthSession(OAuthSession|null $session)

Creates and saves a new OAuth session

Parameters

OAuthSession|null $session The variable receiving information about the new session

Return Value

bool true if the session is created, false if not

at line 117
bool getOAuthSession($sessionId, string $provider, OAuthSession|null $oauthSession)

Retrieves an OAuth session for a given provider

Parameters

$sessionId
string $provider The provider
OAuthSession|null $oauthSession The variable receiving information about the session

Return Value

bool

at line 136
bool saveOAuthSession(OAuthSession $session)

Saves a session for later use

Parameters

OAuthSession $session The information about the session

Return Value

bool true if the session is successfully saved, false otherwise

in AbstractTokenStorage at line 247
bool storeAccessToken(array $accessToken)

Store the values of the access token when it is succefully retrieved from the OAuth provider.

This method is meant to be only be called from inside the class.

Actual implementations should create a sub-class and override this function to make the access token values be stored in other types of containers, like for instance databases.

Parameters

array $accessToken Associative array with properties of the access token. The array may have set the following properties:

  • value: string value of the access token
  • authorized: boolean value that determines if the access token was obtained successfully
  • expiry: (optional) timestamp in ISO format relative to UTC time zone of the access token expiry time
  • type: (optional) type of OAuth token that may determine how it should be used when sending API call requests.
  • refresh: (optional) token that some servers may set to allowing refreshing access tokens when they expire.

Return Value

bool This function should return true if the access token was stored successfully.

in AbstractTokenStorage at line 258
storeUserid(string $userId)

Stores the user ID returned by the OAuth provider

Parameters

string $userId

in AbstractTokenStorage at line 269
array|bool getStoredAccessToken()

Retrieve the OAuth access token if it was already previously stored by the storeAccessToken method.

This method is meant to be only be called from inside the class.

Actual implementations should create a sub-class and override this method to retrieve the access token values from other types of containers, like for instance databases.

Return Value

array|bool the properties of the access token in an associative array. If the access token was not yet stored, it returns an empty array. Otherwise, the properties it may return are the same that may be passed to the storeAccessToken. This function should return true if the access token was retrieved successfully.

in AbstractTokenStorage at line 281
string|null getStoredState()

Returns the stored state

Return Value

string|null the stored state

in AbstractTokenStorage at line 291
string|null getStoredNonce()

Returns the stored nonce

Return Value

string|null the stored nonce

in AbstractTokenStorage at line 301
string|null getStoredUserId()

Returns the stored user ID

Return Value

string|null the stored user ID

at line 162
bool resetAccessToken()

Reset the access token to a state back when the user has not yet authorized the access to the OAuth provider API.

Call this method if for some reason the token to access the API was revoked and you need to ask the user to authorize the access again.

This method must be called when the user is accessing your site pages, so it can reset the information stored in session variables that cache the state of a previously retrieved access token.

Actual implementations should create a sub-class and override this method to reset the access token state when it is stored in other types of containers, like for instance databases.

Return Value

bool This function should return true if the access token was resetted successfully.

at line 188
private OAuthSession setOAuthSession(array $session)

Creates an OAuthSession object from an array of session variables.

Parameters

array $session the array of session variables

Return Value

OAuthSession the OAuthSession object to create

at line 217
private bool tableExists()

Checks if the OAuth session table exists

Return Value

bool true if the OAuth session table exists, false otherwise

at line 231
private createTable()

Creates the OAuth session table

Exceptions

OAuthClientException if an error occurs.

at line 271
private connect()

Connects to the database

Exceptions

OAuthClientException if an error occurs.

at line 308
private bool query($sql, $parameters, $results)

Executes a query on the session table

Parameters

$sql
$parameters
$results

Return Value

bool true if the query succeeds, false otherwise