Moodle APIs 4.3
Moodle 4.3.6 (Build: 20240812)
oauth_helper Class Reference

OAuth helper class. More...

Inheritance diagram for oauth_helper:

Public Member Functions

 __construct ($args)
 Contructor for oauth_helper.
 
 get ($url, $params=array(), $token='', $secret='')
 shortcut to start http get request
 
 get_access_token ($token, $secret, $verifier='')
 Request oauth access token from server.
 
 get_nonce ()
 Generate nonce for oauth request.
 
 get_signable_parameters ($params)
 Build parameters list: oauth_consumer_key="0685bd9184jfhq22", oauth_nonce="4572616e48616d6d65724c61686176", oauth_token="ad180jjd733klru7", oauth_signature_method="HMAC-SHA1", oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", oauth_timestamp="137131200", oauth_version="1.0" oauth_verifier="1.0".
 
 get_timestamp ()
 Generate timestamp.
 
 parse_result ($str)
 A method to parse oauth response to get oauth_token and oauth_token_secret.
 
 post ($url, $params=array(), $token='', $secret='')
 shortcut to start http post request
 
 prepare_oauth_parameters ($url, $params, $http_method='POST')
 Initilize oauth request parameters, including: oauth_consumer_key="0685bd9184jfhq22", oauth_token="ad180jjd733klru7", oauth_signature_method="HMAC-SHA1", oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", oauth_timestamp="137131200", oauth_nonce="4572616e48616d6d65724c61686176", oauth_version="1.0" To access protected resources, oauth_token should be defined.
 
 request ($method, $url, $params=array(), $token='', $secret='')
 Request oauth protected resources.
 
 request_token ()
 Request token for authentication This is the first step to use OAuth, it will return oauth_token and oauth_token_secret.
 
 set_access_token ($token, $secret)
 Set oauth access token for oauth request.
 
 set_nonce ($str)
 Set nonce.
 
 set_timestamp ($time)
 Set timestamp.
 
 setup_oauth_http_header ($params)
 
 setup_oauth_http_options ($options)
 Sets the options for the next curl request.
 
 sign ($http_method, $url, $params, $secret)
 Create signature for oauth request.
 

Protected Attributes

string $access_token
 access token.
 
string $access_token_api
 
string $access_token_secret
 access secret token.
 
string $api_root
 oauth root
 
string $authorize_url
 authorize url
 
string $consumer_key
 consumer key, issued by oauth provider
 
string $consumer_secret
 consumer secret, issued by oauth provider
 
curl $http
 
 $http_method
 
array $http_options
 options to pass to the next curl request
 
string $nonce
 nonce.
 
moodle_url $oauth_callback
 oauth callback URL.
 
string $request_token_api
 request token url
 
string $sign_secret
 sign secret.
 
int $timestamp
 timestamp.
 

Detailed Description

OAuth helper class.

  1. You can extends oauth_helper to add specific functions, such as twitter extends oauth_helper
  2. Call request_token method to get oauth_token and oauth_token_secret, and redirect user to authorize_url, developer needs to store oauth_token and oauth_token_secret somewhere, we will use them to request access token later on
  3. User approved the request, and get back to moodle
  4. Call get_access_token, it takes previous oauth_token and oauth_token_secret as arguments, oauth_token will be used in OAuth request, oauth_token_secret will be used to bulid signature, this method will return access_token and access_secret, store these two values in database or session
  5. Now you can access oauth protected resources by access_token and access_secret using oauth_helper\request method (or get() post())

Note:

  1. This class only support HMAC-SHA1
  2. oauth_helper class don't store tokens and secrets, you must store them manually
  3. Some functions are based on http://code.google.com/p/oauth/
License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

oauth_helper::__construct ( $args)

Contructor for oauth_helper.

Subclass can override construct to build its own $this->http

Parameters
array$argsrequires at least three keys, oauth_consumer_key oauth_consumer_secret and api_root, oauth_helper will guess request_token_api, authrize_url and access_token_api based on api_root, but it not always works

Member Function Documentation

◆ get_access_token()

oauth_helper::get_access_token ( $token,
$secret,
$verifier = '' )

Request oauth access token from server.

Parameters
string$method
string$url
string$token
string$secret

◆ get_signable_parameters()

oauth_helper::get_signable_parameters ( $params)

Build parameters list: oauth_consumer_key="0685bd9184jfhq22", oauth_nonce="4572616e48616d6d65724c61686176", oauth_token="ad180jjd733klru7", oauth_signature_method="HMAC-SHA1", oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", oauth_timestamp="137131200", oauth_version="1.0" oauth_verifier="1.0".

Parameters
array$param
Return values
string

◆ parse_result()

oauth_helper::parse_result ( $str)

A method to parse oauth response to get oauth_token and oauth_token_secret.

Parameters
string$str
Return values
array

◆ prepare_oauth_parameters()

oauth_helper::prepare_oauth_parameters ( $url,
$params,
$http_method = 'POST' )

Initilize oauth request parameters, including: oauth_consumer_key="0685bd9184jfhq22", oauth_token="ad180jjd733klru7", oauth_signature_method="HMAC-SHA1", oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", oauth_timestamp="137131200", oauth_nonce="4572616e48616d6d65724c61686176", oauth_version="1.0" To access protected resources, oauth_token should be defined.

Parameters
string$url
string$token
string$http_method
Return values
array

◆ request()

oauth_helper::request ( $method,
$url,
$params = array(),
$token = '',
$secret = '' )

Request oauth protected resources.

Parameters
string$method
string$url
string$token
string$secret

◆ request_token()

oauth_helper::request_token ( )

Request token for authentication This is the first step to use OAuth, it will return oauth_token and oauth_token_secret.

Return values
array

◆ set_access_token()

oauth_helper::set_access_token ( $token,
$secret )

Set oauth access token for oauth request.

Parameters
string$token
string$secret

◆ setup_oauth_http_options()

oauth_helper::setup_oauth_http_options ( $options)

Sets the options for the next curl request.

Parameters
array$options

◆ sign()

oauth_helper::sign ( $http_method,
$url,
$params,
$secret )

Create signature for oauth request.

Parameters
string$url
string$secret
array$params
Return values
string

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