class OAuth2Client extends AbstractOAuthClient implements OAuthClientInterface

This class represents the OAuth client dealing with providers supporting OAuth 2.0.

Properties

protected string $oauthUserAgent The User-Agent used in HTTP requests from AbstractOAuthClient
protected bool $debug Control whether debug output is enabled from AbstractOAuthClient
protected bool $debugHttp Control whether the dialog with the remote Web server should also be logged. from AbstractOAuthClient
protected string $logFileName Name of the file to store log messages from AbstractOAuthClient
protected bool $exit Determine if the current script should be exited. from AbstractOAuthClient
protected string $debugOutput Capture the debug output generated by the class from AbstractOAuthClient
protected string $debugPrefix Mark the lines of the debug output to identify actions performed by this class. from AbstractOAuthClient
protected string $accessToken Access token obtained from the OAuth provider from AbstractOAuthClient
protected string $accessTokenSecret Access token secret obtained from the OAuth provider from AbstractOAuthClient
protected string $accessTokenExpiry Timestamp of the expiry of the access token obtained from the OAuth provider. from AbstractOAuthClient
protected string $accessTokenType Type of access token obtained from the OAuth provider. from AbstractOAuthClient
protected array $accessTokenResponse The original response for the access token request from AbstractOAuthClient
protected string $refreshToken Refresh token obtained from the OAuth provider from AbstractOAuthClient
protected object $idToken The id_token value from OAuth providers compatible with OpenID Connect. from AbstractOAuthClient
protected integer $responseStatus HTTP response status returned by the server when calling an API from AbstractOAuthClient
protected array $responseHeaders HTTP response headers returned by the server when calling an API from AbstractOAuthClient
protected string $responseBody HTTP response body returned by the server when calling an API from AbstractOAuthClient
protected integer $responseTime Time of response of the HTTP request from AbstractOAuthClient
protected OAuthClientStrategy $strategy OAuth Client Strategy object from AbstractOAuthClient
protected OAuthProvider $provider OAuth Provider object from AbstractOAuthClient
protected TokenStorageInterface $storage Token Storage object from AbstractOAuthClient

Methods

__construct(string $provider = "")

Constructs an OAuth client object for the given provider name

bool
isDebug()

Determines whether debug output is enabled or not

bool
isDebugHttp()

Determines whether the dialog with the remote Web server should also be logged or not.

bool
shouldExit()

Determine if the current script should be exited.

string
getAccessToken()

Returns the obtained access token upon successful OAuth authentication.

string
getAccessTokenSecret()

Returns the access token secret obtained from the OAuth provider.

string
getAccessTokenExpiry()

Returns the timestamp of the expiry of the access token obtained from the OAuth provider.

string
getAccessTokenType()

Returns the type of access token obtained from the OAuth provider.

array
getAccessTokenResponse()

Returns the original response for the access token request

string
getRefreshToken()

Returns the obtained refresh token upon successful OAuth authentication.

getIdToken()

Returns the obtained ID token upon successful OpenID authentication.

integer
getResponseStatus()

Returns the HTTP response status returned by the server when calling an API.

array
getResponseHeaders()

Returns the HTTP response headers returned by the server when calling an API.

string|array|null
getResponseHeader(string $header)

Returns the HTTP response header value returned by the server when calling an API for a given header name.

string
getResponseBody()

Returns the HTTP response body returned by the server when calling an API.

string
getOauthUserAgent()

Returns the User-Agent used in HTTP requests.

integer
getResponseTime()

Returns the time of the response of the HTTP request.

getProvider()

Returns the current instance of the OAuthProvider class.

getStrategy()

Returns the current instance of the OAuthClientStrategy class.

setDebug(bool $debug)

Enables or disables the debug mode.

setDebugHttp(bool $debugHttp)

Enables or disables logging of the dialog with the remote Web server.

setLogFileName(string $logFileName)

Sets the name of the file to store log messages.

setExit(bool $exit)

Determines if the current script should be exited.

setRedirectUri(string $redirect_uri)

Sets the URL of the current script page that is calling this class.

setClientId(string $client_id)

Sets the identifier of your application registered with the OAuth provider.

setClientSecret(string $client_secret)

Sets the secret value assigned to your application.

setAccessToken(string $accessToken)

Sets access token obtained from the OAuth provider

setAccessTokenSecret(string $accessTokenSecret)

Sets the access token secret obtained from the OAuth 1.0 or 1.0a provider

setAccessTokenExpiry(string $accessTokenExpiry)

Sets the timestamp of the expiry of the access token obtained from the OAuth provider.

setAccessTokenType(string $accessTokenType)

Sets the type of access token obtained from the OAuth provider.

setAccessTokenResponse(array $accessTokenResponse)

Sets the original response for the access token request.

setRefreshToken(string $refreshToken)

Sets the refresh token obtained from the OAuth provider.

setIdToken(IdToken|null $idToken)

Sets the id_token object from OAuth providers compatible with OpenID Connect.

setResponseStatus(integer $responseStatus)

Sets the HTTP response status returned by the server when calling an API.

setResponseHeaders(array $responseHeaders)

Sets the HTTP response headers returned by the server when calling an API.

setResponseBody(string $responseBody)

Sets the HTTP response body returned by the server when calling an API.

setOauthUserAgent(string $oauthUserAgent)

Changes the User-Agent used in HTTP requests.

setResponseTime(integer $responseTime)

Sets the time of the response of the HTTP request.

bool
trace(string $message)

Writes a message to the log output if debugging is enabled.

string
getAuthorizationEndpoint(string $redirectUri = '', string $state = '', string $nonce = '')

Replaces the placeholders in the URL of the authorization endpoint and returns it.

string
getTokenEndpoint()

Replaces the placeholders in the URL of the token endpoint and returns it.

string
getRevocationEndpoint(string $token)

Replaces the placeholders in the URL of the revocation endpoint and returns it.

string|false|null
getRequestState()

Returns the value of the state parameter returned by the OAuth provider.

string|false|null
getRequestCode()

Returns the value of the code returned by the authorization endpoint of the OAuth provider.

string|null
getRequestError()

Returns the error returned by the OAuth provider.

string|null
getRequestDenied()

Returns the denied access token returned by the OAuth provider, if any.

string|null
getRequestToken()

Returns the request token received from the OAuth 1.0(a) provider

string|null
getRequestVerifier()

Returns the verification code received from the OAuth 1.0(a) provider

redirect(string $url)

Redirect the user browser to a given page.

array
signRequestData(string $url, string $method, array $parameters, array $oauth, string $requestContentType, bool $hasFiles, bool $postDataInUri)

Sign the request data in PLAINTEXT, HMAC_SHA1 or RSA_SHA1 using a key composed from the secret client registered with the provider and the oauth token secret returned by the token request endpoint.

sendHttpRequest(OAuthRequest $oauthRequest, array $options = [])

Sends a HTTP request to the OAuth provider.

OAuthRequest|false
prepareOAuthRequest(string $url, string $method, array $parameters, array $options, array $oauth = null)

Composes a OAuth request to be sent to the OAuth provider.

string|object|array|SimpleXMLElement|false
sendOAuthRequest(string $url, string $method, array $parameters, array $options, array $oauth = null)

Sends a OAuth request to the OAuth provider.

string|object|array|SimpleXMLElement|false
convertResponseBody(array $options)

Converts the response body of an OAuth request based on the options in the provided argument.

bool
isThereAStoredAccessToken()

Checks if there is a stored access token

bool
isStoredAccessTokenValid()

Checks if the stored access token is valid

mixed
callAPI(string $url, string $method, array $parameters, array $options)

Send a HTTP request to the Web services API using a previously obtained access token via OAuth.

getResourceOwner(string $endpoint = null)

Returns the information about the resource owner using a previously obtained access token via OAuth.

fetchResourceOwner(array $options)

Returns the information about the resource owner.

bool
checkTokenBeforeCall($options)

Checks the access token state before calling an API.

bool
initialize(array $options = [])

Initialize the class variables and internal state. It must be called before calling other class functions.

array
deepMerge(array $arr1, array $arr2)

Merges two arrays recursively.

void
initializeRegitrationOptions($options)

Initializes the options registered with the OAuth provider.

discover(string $discoveryEndpoint)

Sends a OAuth request to the discovery endpoint of the OpenID provider in order to obtain the configuration information, which is the list of all endpoints and the list of supported OAuth elements.

checkNoToken()

Checks that the authenticate method has not already been called

bool
isAuthenticated()

Checks if the user is authenticated with the current OAuth provider.

bool
authenticate()

Process the OAuth protocol interaction with the OAuth provider.

string|bool
checkAccessToken(string $redirectUrl)

Check if the access token was retrieved and if it is valid.

bool
resetAccessToken()

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

bool
canRevokeToken()

Determines whether the revokeToken function can be called.

bool
revokeToken(string $tokenTypeHint = 'access_token')

Revoke a previously obtained token so it becomes invalid.

finalize()

Cleanup any resources that may have been used during the OAuth protocol processing or execution of API calls.

bool
canLogOut()

Determines whether the logOut function can be called.

logOut(string $redirect = null)

Calls the end-session endpoint to notify the provider that the end-user has logged out of the relying party site.

object|false
verifyClaims(string $jwt)

Verifies the claims of an ID token and return them if they are valid

bool
verifySignature(string $jwt)

Verifies the signature of an ID token

bool
requestAnOAuthToken(string|null $code = null, bool $refresh = false)

Prepares and initiates the request for an access or refresh token

bool
isAccessTokenExpired()

Checks if an access token has expired

Details

in AbstractOAuthClient at line 276
__construct(string $provider = "")

Constructs an OAuth client object for the given provider name

Parameters

string $provider The provider name

in AbstractOAuthClient at line 286
protected bool isDebug()

Determines whether debug output is enabled or not

Return Value

bool

in AbstractOAuthClient at line 296
protected bool isDebugHttp()

Determines whether the dialog with the remote Web server should also be logged or not.

Return Value

bool

in AbstractOAuthClient at line 303
bool shouldExit()

Determine if the current script should be exited.

Call this function after calling the authenticate function and exit your script immediately if this function returns true.

Return Value

bool

in AbstractOAuthClient at line 310
string getAccessToken()

Returns the obtained access token upon successful OAuth authentication.

Call this function to get the obtained access token upon successful OAuth authorization.

Return Value

string

in AbstractOAuthClient at line 323
protected string getAccessTokenSecret()

Returns the access token secret obtained from the OAuth provider.

If the OAuth protocol version is 1.0 or 1.0a, check this variable to get the obtained access token secret upon successful OAuth authorization.

Return Value

string

in AbstractOAuthClient at line 337
protected string getAccessTokenExpiry()

Returns the timestamp of the expiry of the access token obtained from the OAuth provider.

Check this variable to get the obtained access token expiry time upon successful OAuth authorization. If this variable is empty, that means no expiry time was set.

Return Value

string

in AbstractOAuthClient at line 349
protected string getAccessTokenType()

Returns the type of access token obtained from the OAuth provider.

Check this variable to get the obtained access token type upon successful OAuth authorization.

Return Value

string

in AbstractOAuthClient at line 361
protected array getAccessTokenResponse()

Returns the original response for the access token request

Check this variable if the OAuth provider returns custom parameters in the request to obtain the access token.

Return Value

array

in AbstractOAuthClient at line 368
string getRefreshToken()

Returns the obtained refresh token upon successful OAuth authentication.

Call this function to get the obtained refresh token upon successful OAuth authorization.

Return Value

string

in AbstractOAuthClient at line 375
IdToken getIdToken()

Returns the obtained ID token upon successful OpenID authentication.

Call this function if the OAuth provider returns id_token values.

Return Value

IdToken the obtained ID token

in AbstractOAuthClient at line 389
protected integer getResponseStatus()

Returns the HTTP response status returned by the server when calling an API.

Check this variable after calling the callAPI function if the API calls and you need to process the error depending the response status. 200 means no error. 0 means the server response was not retrieved.

Return Value

integer

in AbstractOAuthClient at line 402
protected array getResponseHeaders()

Returns the HTTP response headers returned by the server when calling an API.

Check this variable after calling the callAPI function if the API calls and you need to process the error depending the response headers.

Return Value

array

in AbstractOAuthClient at line 414
protected string|array|null getResponseHeader(string $header)

Returns the HTTP response header value returned by the server when calling an API for a given header name.

Parameters

string $header the header name

Return Value

string|array|null

in AbstractOAuthClient at line 434
protected string getResponseBody()

Returns the HTTP response body returned by the server when calling an API.

Check this variable after calling the callAPI function if the API calls and you need to process the error depending the response headers.

Return Value

string

in AbstractOAuthClient at line 443
protected string getOauthUserAgent()

Returns the User-Agent used in HTTP requests.

Return Value

string

in AbstractOAuthClient at line 452
protected integer getResponseTime()

Returns the time of the response of the HTTP request.

Return Value

integer

in AbstractOAuthClient at line 459
OAuthProvider getProvider()

Returns the current instance of the OAuthProvider class.

Return Value

OAuthProvider

in AbstractOAuthClient at line 466
OAuthClientStrategy getStrategy()

Returns the current instance of the OAuthClientStrategy class.

Return Value

OAuthClientStrategy

in AbstractOAuthClient at line 482
AbstractOAuthClient setDebug(bool $debug)

Enables or disables the debug mode.

Set this variable to true if you need to check what is going on during calls to the class. When enabled, the debug output goes either to the variable debugOutput and the PHP error log.

Parameters

bool $debug

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 497
AbstractOAuthClient setDebugHttp(bool $debugHttp)

Enables or disables logging of the dialog with the remote Web server.

Set this variable to true if you want to inspect the data exchange with the OAuth provider.

Parameters

bool $debugHttp

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 513
AbstractOAuthClient setLogFileName(string $logFileName)

Sets the name of the file to store log messages.

Set this variable to the path of a file to which log messages will be appended instead of sending to PHP error log when the debug variable is set to true.

Parameters

string $logFileName

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 525
protected AbstractOAuthClient setExit(bool $exit)

Determines if the current script should be exited.

Parameters

bool $exit

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 537
AbstractOAuthClient setRedirectUri(string $redirect_uri)

Sets the URL of the current script page that is calling this class.

Parameters

string $redirect_uri

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 549
AbstractOAuthClient setClientId(string $client_id)

Sets the identifier of your application registered with the OAuth provider.

Parameters

string $client_id

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 561
AbstractOAuthClient setClientSecret(string $client_secret)

Sets the secret value assigned to your application.

Parameters

string $client_secret

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 573
protected AbstractOAuthClient setAccessToken(string $accessToken)

Sets access token obtained from the OAuth provider

Parameters

string $accessToken

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 585
protected AbstractOAuthClient setAccessTokenSecret(string $accessTokenSecret)

Sets the access token secret obtained from the OAuth 1.0 or 1.0a provider

Parameters

string $accessTokenSecret

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 598
protected AbstractOAuthClient setAccessTokenExpiry(string $accessTokenExpiry)

Sets the timestamp of the expiry of the access token obtained from the OAuth provider.

Parameters

string $accessTokenExpiry

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 610
protected AbstractOAuthClient setAccessTokenType(string $accessTokenType)

Sets the type of access token obtained from the OAuth provider.

Parameters

string $accessTokenType

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 622
protected AbstractOAuthClient setAccessTokenResponse(array $accessTokenResponse)

Sets the original response for the access token request.

Parameters

array $accessTokenResponse

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 634
protected AbstractOAuthClient setRefreshToken(string $refreshToken)

Sets the refresh token obtained from the OAuth provider.

Parameters

string $refreshToken

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 646
protected AbstractOAuthClient setIdToken(IdToken|null $idToken)

Sets the id_token object from OAuth providers compatible with OpenID Connect.

Parameters

IdToken|null $idToken

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 658
protected AbstractOAuthClient setResponseStatus(integer $responseStatus)

Sets the HTTP response status returned by the server when calling an API.

Parameters

integer $responseStatus

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 670
protected AbstractOAuthClient setResponseHeaders(array $responseHeaders)

Sets the HTTP response headers returned by the server when calling an API.

Parameters

array $responseHeaders

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 682
protected AbstractOAuthClient setResponseBody(string $responseBody)

Sets the HTTP response body returned by the server when calling an API.

Parameters

string $responseBody

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 694
protected AbstractOAuthClient setOauthUserAgent(string $oauthUserAgent)

Changes the User-Agent used in HTTP requests.

Parameters

string $oauthUserAgent

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 706
protected AbstractOAuthClient setResponseTime(integer $responseTime)

Sets the time of the response of the HTTP request.

Parameters

integer $responseTime

Return Value

AbstractOAuthClient

in AbstractOAuthClient at line 718
bool trace(string $message)

Writes a message to the log output if debugging is enabled.

Parameters

string $message

Return Value

bool

in AbstractOAuthClient at line 740
protected string getAuthorizationEndpoint(string $redirectUri = '', string $state = '', string $nonce = '')

Replaces the placeholders in the URL of the authorization endpoint and returns it.

Parameters

string $redirectUri the redirection URI registered with the OAuth provider.
string $state the value of the state parameter associated with the request sent to the authorization endpoint to mitigate CSRF attacks.
string $nonce the value of the nonce parameter associated with the request sent to the authorization endpoint to prevent replay attacks.

Return Value

string the URL of the authorization endpoint

in AbstractOAuthClient at line 773
protected string getTokenEndpoint()

Replaces the placeholders in the URL of the token endpoint and returns it.

Return Value

string the URL of the token endpoint

in AbstractOAuthClient at line 784
protected string getRevocationEndpoint(string $token)

Replaces the placeholders in the URL of the revocation endpoint and returns it.

Parameters

string $token the access or refresh token to revoke.

Return Value

string the URL of the revocation endpoint

in AbstractOAuthClient at line 797
protected string|false|null getRequestState()

Returns the value of the state parameter returned by the OAuth provider.

Return Value

string|false|null the value of the state parameter, false if OAuth provider has returned an error or null if there is no state returned.

in AbstractOAuthClient at line 812
protected string|false|null getRequestCode()

Returns the value of the code returned by the authorization endpoint of the OAuth provider.

Return Value

string|false|null the value of the authorization code, false if OAuth provider an error has occurred or null if there is no code returned.

in AbstractOAuthClient at line 821
protected string|null getRequestError()

Returns the error returned by the OAuth provider.

Return Value

string|null the error returned or null if there is no error.

in AbstractOAuthClient at line 830
protected string|null getRequestDenied()

Returns the denied access token returned by the OAuth provider, if any.

Return Value

string|null the denied access token returned or null if there is no denied access token.

in AbstractOAuthClient at line 839
protected string|null getRequestToken()

Returns the request token received from the OAuth 1.0(a) provider

Return Value

string|null the request token received or null if there is no request token.

in AbstractOAuthClient at line 848
protected string|null getRequestVerifier()

Returns the verification code received from the OAuth 1.0(a) provider

Return Value

string|null the verification code received or null if there is no verification code.

in AbstractOAuthClient at line 864
protected redirect(string $url)

Redirect the user browser to a given page.

This function is meant to be only be called from inside the class. By default it issues HTTP 302 response status and sets the redirection location to a given URL. Sub-classes may override this function to implement a different way to redirect the user browser.

Parameters

string $url the full URL of the page to redirect.

in AbstractOAuthClient at line 887
protected array signRequestData(string $url, string $method, array $parameters, array $oauth, string $requestContentType, bool $hasFiles, bool $postDataInUri)

Sign the request data in PLAINTEXT, HMAC_SHA1 or RSA_SHA1 using a key composed from the secret client registered with the provider and the oauth token secret returned by the token request endpoint.

For OAuth 1.0 an Oauth 1.0a only.

Parameters

string $url
string $method
array $parameters
array $oauth
string $requestContentType
bool $hasFiles
bool $postDataInUri

Return Value

array ($url, $authorization, $postData)

Exceptions

OAuthClientException if an error occurs.

in AbstractOAuthClient at line 905
protected sendHttpRequest(OAuthRequest $oauthRequest, array $options = [])

Sends a HTTP request to the OAuth provider.

Parameters

OAuthRequest $oauthRequest
array $options

Exceptions

OAuthClientException

in AbstractOAuthClient at line 983
protected OAuthRequest|false prepareOAuthRequest(string $url, string $method, array $parameters, array $options, array $oauth = null)

Composes a OAuth request to be sent to the OAuth provider.

Parameters

string $url The URL of the relevant endpoint of the OAuth provider.
string $method The HTTP method of the request
array $parameters The parameters of the request that will be added to the url or inserted into the body as appropriate.
array $options Various options for composing the request, including:

  • request_content_type : the Content-Type header of the request
  • files : uploaded files if any
  • headers : the headers of the request
  • body : the body of the request
  • accept : the Accept header of the request
  • accept_language the Accept-Language header of the reques t(default: '*')
  • post_data_in_uri : the request data must be passed in the URI even if the method is POST (OAuth 1.0 or 1.0a only).
array $oauth Additional options if OAuth 1.0 or 1.0a

Return Value

OAuthRequest|false the prepared OAuth request or false

Exceptions

OAuthClientException if an error occurs.

in AbstractOAuthClient at line 1102
protected string|object|array|SimpleXMLElement|false sendOAuthRequest(string $url, string $method, array $parameters, array $options, array $oauth = null)

Sends a OAuth request to the OAuth provider.

Parameters

string $url The URL of the relevant endpoint of the OAuth provider.
string $method The HTTP method of the request
array $parameters The parameters of the request that will be added to the url or inserted into the body as appropriate.
array $options Various options, including:

  • resource : the type of the resource we want to get with this request
  • fail_on_access_error : if an exception must be raised when the status of the response is not between 200 and 299
array $oauth Additional options if OAuth 1.0 or 1.0a

Return Value

string|object|array|SimpleXMLElement|false the response or false if the request can not be prepared

Exceptions

OAuthClientException if an error occurs.
OAuthClientAccessTokenException if the response status if not between 200 and 299

in AbstractOAuthClient at line 1149
protected string|object|array|SimpleXMLElement|false convertResponseBody(array $options)

Converts the response body of an OAuth request based on the options in the provided argument.

Parameters

array $options Various options, including:

  • resource : the type of the resource obtained with this request
  • response_content_type : the name of the resource we want to get with this request
  • convert_json_to_array : converts the body of the response into an array if the content type of the response is 'application/json'
  • convert_xml_to_object : converts the body of the response into an \SimpleXMLElement object if the content type of the response is 'text/xml' or 'application/xml' or derived.

Return Value

string|object|array|SimpleXMLElement|false the response or false if the body can not be converted

Exceptions

OAuthClientException if an error occurs.

in AbstractOAuthClient at line 1207
protected bool isThereAStoredAccessToken()

Checks if there is a stored access token

Return Value

bool true if there is a stored access token, false otherwise

in AbstractOAuthClient at line 1216
protected bool isStoredAccessTokenValid()

Checks if the stored access token is valid

Return Value

bool true if the stored access token is valid, false otherwise

at line 281
mixed callAPI(string $url, string $method, array $parameters, array $options)

Send a HTTP request to the Web services API using a previously obtained access token via OAuth.

This function can be used to call an API after having previously obtained an access token through the OAuth protocol using the authenticate function, or by directly setting the variables access_token, as well as access_token_secret in case of using OAuth 1.0 or 1.0a services. The response_status variable returns the HTTP response status of the request. The responseHeaders variable returns the HTTP response headers. The responseBody variable returns the HTTP response body.

Parameters

string $url URL of the API where the HTTP request will be sent.
string $method HTTP method that will be used to send the request. It can be 'GET', 'POST', 'DELETE', 'PUT', etc..
array $parameters Associative array with the names and values of the API call request parameters.
array $options Associative array with additional options to configure the request.

Currently it supports the following options:

  • '2legged' : boolean option that determines if the API request should be 2 legged. The default value is false.
  • 'accept' : content type value of the Accept HTTP header to be sent in the API call HTTP request. Some APIs require that a certain value be sent to specify which version of the API is being called. The default value is '/'.
  • 'accept_language' : value of the Accept-Language HTTP header to be sent in the API call HTTP request. Some APIs require that a certain value be sent to specify which version of the API is being called.
  • 'convert_json_to_array' : boolean option that determines if objects should be converted into arrays when the response is returned in JSON format. The default value is false.
  • 'convert_xml_to_object' : name of the method to convert XML responses. Currently only 'simplexml' is supported. It makes a XML response be parsed and returned as a SimpleXMLElement object.
  • 'fail_on_access_error' : boolean option that determines if this functions should fail when the provider response status is not between 200 and 299. The default value is false.
  • 'files' : associative array with details of the parameters that must be passed as file uploads. The array indexes must have the same name of the parameters to be sent as files. The respective array entry values must also be associative arrays with the parameters for each file. Currently it supports the following parameters:

                         - 'type' MIME value of the content type of the file.
                         - 'filename' defines a custom file name for the file to be uploaded. Default: none
                         - 'contents' optional, the contents of the file
    
  • 'post_data_in_uri' : boolean option to determine that a POST request should pass the request data in the URI. The default value is false.

  • 'max_redirects' : limit number of times that HTTP response redirects will be followed. If it is set to 0, redirection responses fail in error. The default value is 0.
  • 'body' : request body data of a custom type. The 'request_content_type' option must be specified, so the 'body' option is considered.
  • 'request_content_type' : content type that should be used to send the request values. It can be either 'application/x-www-form-urlencoded' for sending values like from Web forms, or 'application/json' for sending the values encoded in JSON format. The default value is 'application/x-www-form-urlencoded'.
  • 'headers' : associative array of custom headers to be sent with the API call. These headers override any values set by the class when sending the API call HTTP request.
  • 'resource' : string with a label that will be used in the error messages and debug log entries to identify what operation the request is performing. The default value is 'API call'.
  • 'response_content_type': content type that should be considered when decoding the API request response. This overrides the Content-Type header returned by the server. If the content type is 'application/x-www-form-urlencoded' the function will parse the data returning an array of key-value pairs. If the content type is 'application/json' the response will be decode as a JSON-encoded data type. Other content type values will make the function return the original response value as it was returned from the server. The default value for this option is to use what the server returned in the Content-Type header.

Return Value

mixed Return the value of the API response. If the value is JSON encoded, this function will decode it and return the value converted to respective types. If the value is form encoded, this function will decode the response and return it as an array. Otherwise, the class will return the value as a string. This function returns false if the call was not done successfully.

in AbstractOAuthClient at line 1283
ResourceOwner getResourceOwner(string $endpoint = null)

Returns the information about the resource owner using a previously obtained access token via OAuth.

This function must be called after having previously obtained an access token through the OAuth protocol using the authenticate function, or by directly setting the variables access_token, as well as access_token_secret in case of using OAuth 1.0 or 1.0a services.

Parameters

string $endpoint URL of the user info endpoint.

Return Value

ResourceOwner The resource owner

in AbstractOAuthClient at line 1308
ResourceOwner fetchResourceOwner(array $options)

Returns the information about the resource owner.

This function is a high-level function that perform all the necessary actions (initalization, authentication, ...) before requesting the information about the resource owner.

Parameters

array $options array of parameters.

Return Value

ResourceOwner The resource owner

in AbstractOAuthClient at line 1329
protected bool checkTokenBeforeCall($options)

Checks the access token state before calling an API.

Parameters

$options

Return Value

bool true if access token is valid, false otherwise

in AbstractOAuthClient at line 1346
bool initialize(array $options = [])

Initialize the class variables and internal state. It must be called before calling other class functions.

Set the provider variable before calling this function to let it initialize the class variables to work with the specified provider. Alternatively, you can set other class variables manually to make it work with providers that are not yet built-in supported.

Parameters

array $options class variables to work with the specified provider.

Return Value

bool This function returns true if it was able to successfully initialize the class for the specified server type.

in AbstractOAuthClient at line 1406
private array deepMerge(array $arr1, array $arr2)

Merges two arrays recursively.

array_merge_recursive does indeed merge arrays, but it converts values with duplicate keys to arrays rather than overwriting the value in the first array with the duplicate value in the second array, as array_merge does. I.e., with array_merge_recursive, this happens

Parameters

array $arr1 the first array
array $arr2 the second array

Return Value

array the merged array

in AbstractOAuthClient at line 1423
protected void initializeRegitrationOptions($options)

Initializes the options registered with the OAuth provider.

Parameters

$options

Return Value

void

in AbstractOAuthClient at line 1465
protected discover(string $discoveryEndpoint)

Sends a OAuth request to the discovery endpoint of the OpenID provider in order to obtain the configuration information, which is the list of all endpoints and the list of supported OAuth elements.

Parameters

string $discoveryEndpoint The discovery endpoint

Exceptions

OAuthClientException if an error occurs.

in AbstractOAuthClient at line 1480
protected checkNoToken()

Checks that the authenticate method has not already been called

Exceptions

OAuthClientException if the authenticate method has already been called.

in AbstractOAuthClient at line 1490
bool isAuthenticated()

Checks if the user is authenticated with the current OAuth provider.

Call this function if you want to know if the user needs to log in to access the resources he owns without doing so.

Return Value

bool This function returns true if the user is authenticated, false otherwise.

in AbstractOAuthClient at line 1507
bool authenticate()

Process the OAuth protocol interaction with the OAuth provider.

Call this function when you need to retrieve the OAuth access token. Check the access_token to determine if the access token was obtained successfully.

Return Value

bool This function returns true if the OAuth protocol was processed without errors.

at line 320
string|bool checkAccessToken(string $redirectUrl)

Check if the access token was retrieved and if it is valid.

Call this function when you need to check of an access token is valid without forcing to redirect the user to the OAuth provider authorization page.

If a previously retrieved access token has expired, this function may renew it automatically.

Parameters

string $redirectUrl

Return Value

string|bool the URL of the OAuth provider authorization to redirect the user if the access token was not yet retrieved or is not valid. This function returns true if the OAuth protocol was checked without errors.

in AbstractOAuthClient at line 1527
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 function if for some reason the token to access the API was revoked and you need to ask the user to authorize the access again.

By default the class stores and retrieves access tokens in a session variable named 'OAUTH_ACCESS_TOKEN'.

This function 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 function 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.

in AbstractOAuthClient at line 1534
bool canRevokeToken()

Determines whether the revokeToken function can be called.

Return Value

bool returns true if OAuth provider has a revocation endpoint and there is a valid access token.

in AbstractOAuthClient at line 1552
bool revokeToken(string $tokenTypeHint = 'access_token')

Revoke a previously obtained token so it becomes invalid.

Call this function when you need to invalidate a token that you no longer need to use, so it is not used by any other application.

Parameters

string $tokenTypeHint the type of token to be revoked if it is not an access token.

Return Value

bool This function returns true if the OAuth protocol was revoked without errors.

in AbstractOAuthClient at line 1595
finalize()

Cleanup any resources that may have been used during the OAuth protocol processing or execution of API calls.

Always call this function as the last step after calling the functions authenticate() or callAPI(...).

in AbstractOAuthClient at line 1601
bool canLogOut()

Determines whether the logOut function can be called.

Return Value

bool returns true if OAuth provider has a end-session endpoint and there is a valid access token.

in AbstractOAuthClient at line 1615
logOut(string $redirect = null)

Calls the end-session endpoint to notify the provider that the end-user has logged out of the relying party site.

Parameters

string $redirect URL to which the RP is requesting that the End-User's User Agent be redirected after a logout has been performed. The value MUST have been previously registered with the OP. Value can be null.

Exceptions

OAuthClientException

at line 21
protected object|false verifyClaims(string $jwt)

Verifies the claims of an ID token and return them if they are valid

Parameters

string $jwt the JSON Web Token containing the ID Token

Return Value

object|false the claims if they are valid, false otherwise.

at line 63
protected bool verifySignature(string $jwt)

Verifies the signature of an ID token

Parameters

string $jwt the JSON Web Token containing the ID Token

Return Value

bool true if the signature is valid, false otherwise

Exceptions

OAuthClientException if an error occurs.

at line 115
protected bool requestAnOAuthToken(string|null $code = null, bool $refresh = false)

Prepares and initiates the request for an access or refresh token

Parameters

string|null $code eventually, the code received from the authorization endpoint.
bool $refresh true if the request is for a refresh token

Return Value

bool true if the token has been successfully obtained, false otherwise

Exceptions

OAuthClientException if an error occurs.
OAuthClientAuthorizationException if the provider has sent an error message.

at line 311
protected bool isAccessTokenExpired()

Checks if an access token has expired

Return Value

bool true if the access token has expired and there is no refresh token