Abstract Class yii\authclient\OAuth1
Inheritance | yii\authclient\OAuth1 » yii\authclient\BaseOAuth » yii\authclient\BaseClient » yii\base\Component |
---|---|
Implements | yii\authclient\ClientInterface |
Subclasses | yii\authclient\clients\Twitter |
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-authclient/blob/master/src/OAuth1.php |
OAuth1 serves as a client for the OAuth 1/1.0a flow.
In order to acquire access token perform following sequence:
use yii\authclient\OAuth1;
use Yii;
// assuming class MyAuthClient extends OAuth1
$oauthClient = new MyAuthClient();
$requestToken = $oauthClient->fetchRequestToken(); // Get request token
$url = $oauthClient->buildAuthUrl($requestToken); // Get authorization URL
return Yii::$app->getResponse()->redirect($url); // Redirect to authorization URL
// After user returns at our site:
$accessToken = $oauthClient->fetchAccessToken(Yii::$app->request->get('oauth_token'), $requestToken); // Upgrade to access token
See also https://oauth.net/1/ https://tools.ietf.org/html/rfc5849.
Public Properties
Public Methods
Protected Methods
Property Details
Access token HTTP method.
Request token HTTP method.
Method Details
Defined in: yii\authclient\BaseOAuth::api()
Performs request to the OAuth API returning response data.
You may use createApiRequest() method instead, gaining more control over request execution.
See also createApiRequest().
public array api ( $apiSubUrl, $method = 'GET', $data = [], $headers = [] ) | ||
$apiSubUrl | string |
API sub URL, which will be append to $apiBaseUrl, or absolute API URL. |
$method | string |
Request method. |
$data | array|string |
Request data or content. |
$headers | array |
Additional request headers. |
return | array |
API response data. |
---|
public function api($apiSubUrl, $method = 'GET', $data = [], $headers = [])
{
$request = $this->createApiRequest()
->setMethod($method)
->setUrl($apiSubUrl)
->addHeaders($headers);
if (!empty($data)) {
if (is_array($data)) {
$request->setData($data);
} else {
$request->setContent($data);
}
}
return $this->sendRequest($request);
}
Applies access token to the HTTP request instance.
public void applyAccessTokenToRequest ( $request, $accessToken ) | ||
$request | \yii\httpclient\Request |
HTTP request instance. |
$accessToken | yii\authclient\OAuthToken |
Access token instance. |
public function applyAccessTokenToRequest($request, $accessToken)
{
$data = $request->getData();
$data['oauth_consumer_key'] = $this->consumerKey;
$data['oauth_token'] = $accessToken->getToken();
$request->setData($data);
}
Defined in: yii\authclient\BaseOAuth::beforeApiRequestSend()
Handles Request::EVENT_BEFORE_SEND event.
Applies $accessToken to the request.
public void beforeApiRequestSend ( $event ) | ||
$event | \yii\httpclient\RequestEvent |
Event instance. |
throws | \yii\base\Exception |
on invalid access token. |
---|
public function beforeApiRequestSend($event)
{
$accessToken = $this->getAccessToken();
if (!is_object($accessToken) || !$accessToken->getIsValid()) {
throw new Exception('Invalid access token.');
}
$this->applyAccessTokenToRequest($event->request, $accessToken);
}
Handles Request::EVENT_BEFORE_SEND event.
Ensures every request has been signed up before sending.
public void beforeRequestSend ( $event ) | ||
$event | \yii\httpclient\RequestEvent |
Event instance. |
public function beforeRequestSend($event)
{
$this->signRequest($event->request);
}
Composes user authorization URL.
public string buildAuthUrl ( yii\authclient\OAuthToken $requestToken = null, array $params = [] ) | ||
$requestToken | yii\authclient\OAuthToken |
OAuth request token. |
$params | array |
Additional request params. |
return | string |
Authorize URL |
---|---|---|
throws | \yii\base\InvalidParamException |
on failure. |
public function buildAuthUrl(OAuthToken $requestToken = null, array $params = [])
{
if (!is_object($requestToken)) {
$requestToken = $this->getState('requestToken');
if (!is_object($requestToken)) {
throw new InvalidParamException('Request token is required to build authorize URL!');
}
}
$params['oauth_token'] = $requestToken->getToken();
return $this->composeUrl($this->authUrl, $params);
}
Composes authorization header.
protected array composeAuthorizationHeader ( array $params, $realm = '' ) | ||
$params | array |
Request params. |
$realm | string |
Authorization realm. |
return | array |
Authorization header in format: [name => content]. |
---|
protected function composeAuthorizationHeader(array $params, $realm = '')
{
$header = 'OAuth';
$headerParams = [];
if (!empty($realm)) {
$headerParams[] = 'realm="' . rawurlencode($realm) . '"';
}
foreach ($params as $key => $value) {
if (substr_compare($key, 'oauth', 0, 5)) {
continue;
}
$headerParams[] = rawurlencode($key) . '="' . rawurlencode($value) . '"';
}
if (!empty($headerParams)) {
$header .= ' ' . implode(', ', $headerParams);
}
return ['Authorization' => $header];
}
Creates signature base string, which will be signed by $signatureMethod.
protected string composeSignatureBaseString ( $method, $url, array $params ) | ||
$method | string |
Request method. |
$url | string |
Request URL. |
$params | array |
Request params. |
return | string |
Base signature string. |
---|
protected function composeSignatureBaseString($method, $url, array $params)
{
if (strpos($url, '?') !== false) {
list($url, $queryString) = explode('?', $url, 2);
parse_str($queryString, $urlParams);
$params = array_merge($urlParams, $params);
}
unset($params['oauth_signature']);
uksort($params, 'strcmp'); // Parameters are sorted by name, using lexicographical byte value ordering. Ref: Spec: 9.1.1
$parts = [
strtoupper($method),
$url,
http_build_query($params, '', '&', PHP_QUERY_RFC3986)
];
$parts = array_map('rawurlencode', $parts);
return implode('&', $parts);
}
Composes request signature key.
protected string composeSignatureKey ( $token = null ) | ||
$token | yii\authclient\OAuthToken|null |
OAuth token to be used for signature key. |
return | string |
Signature key. |
---|
protected function composeSignatureKey($token = null)
{
$signatureKeyParts = [
$this->consumerSecret
];
if ($token === null) {
$token = $this->getAccessToken();
}
if (is_object($token)) {
$signatureKeyParts[] = $token->getTokenSecret();
} else {
$signatureKeyParts[] = '';
}
$signatureKeyParts = array_map('rawurlencode', $signatureKeyParts);
return implode('&', $signatureKeyParts);
}
Defined in: yii\authclient\BaseOAuth::composeUrl()
Composes URL from base URL and GET params.
protected string composeUrl ( $url, array $params = [] ) | ||
$url | string |
Base URL. |
$params | array |
GET params. |
return | string |
Composed URL. |
---|
protected function composeUrl($url, array $params = [])
{
if (!empty($params)) {
if (strpos($url, '?') === false) {
$url .= '?';
} else {
$url .= '&';
}
$url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986);
}
return $url;
}
Creates an HTTP request for the API call.
The created request will be automatically processed adding access token parameters and signature before sending. You may use createRequest() to gain full control over request composition and execution.
public \yii\httpclient\Request createApiRequest ( ) | ||
return | \yii\httpclient\Request |
HTTP request instance. |
---|
public function createApiRequest()
{
$request = parent::createApiRequest();
// ensure correct event handlers order :
$request->off(Request::EVENT_BEFORE_SEND, [$this, 'beforeRequestSend']);
$request->on(Request::EVENT_BEFORE_SEND, [$this, 'beforeRequestSend']);
return $request;
}
Defined in: yii\authclient\BaseOAuth::createHttpClient()
Creates HTTP client instance from reference or configuration.
protected \yii\httpclient\Client createHttpClient ( $reference ) | ||
$reference | string|array |
Component name or array configuration. |
return | \yii\httpclient\Client |
HTTP client instance. |
---|
protected function createHttpClient($reference)
{
$httpClient = parent::createHttpClient($reference);
$httpClient->baseUrl = $this->apiBaseUrl;
return $httpClient;
}
Creates HTTP request instance.
public \yii\httpclient\Request createRequest ( ) | ||
return | \yii\httpclient\Request |
HTTP request instance. |
---|
public function createRequest()
{
$request = parent::createRequest();
$request->on(Request::EVENT_BEFORE_SEND, [$this, 'beforeRequestSend']);
return $request;
}
Defined in: yii\authclient\BaseOAuth::createSignatureMethod()
Creates signature method instance from its configuration.
protected yii\authclient\signature\BaseMethod createSignatureMethod ( array $signatureMethodConfig ) | ||
$signatureMethodConfig | array |
Signature method configuration. |
return | yii\authclient\signature\BaseMethod |
Signature method instance. |
---|
protected function createSignatureMethod(array $signatureMethodConfig)
{
if (!array_key_exists('class', $signatureMethodConfig)) {
$signatureMethodConfig['class'] = signature\HmacSha1::className();
}
return Yii::createObject($signatureMethodConfig);
}
Defined in: yii\authclient\BaseOAuth::createToken()
Creates token from its configuration.
protected yii\authclient\OAuthToken createToken ( array $tokenConfig = [] ) | ||
$tokenConfig | array |
Token configuration. |
return | yii\authclient\OAuthToken |
Token instance. |
---|
protected function createToken(array $tokenConfig = [])
{
if (!array_key_exists('class', $tokenConfig)) {
$tokenConfig['class'] = OAuthToken::className();
}
return Yii::createObject($tokenConfig);
}
Defined in: yii\authclient\BaseClient::defaultName()
Generates service name.
protected string defaultName ( ) | ||
return | string |
Service name. |
---|
protected function defaultName()
{
return Inflector::camel2id(StringHelper::basename(get_class($this)));
}
Defined in: yii\authclient\BaseClient::defaultNormalizeUserAttributeMap()
Returns the default $normalizeUserAttributeMap value.
Particular client may override this method in order to provide specific default map.
protected array defaultNormalizeUserAttributeMap ( ) | ||
return | array |
Normalize attribute map. |
---|
protected function defaultNormalizeUserAttributeMap()
{
return [];
}
Defined in: yii\authclient\BaseOAuth::defaultRequestOptions()
Returns default HTTP request options.
protected array defaultRequestOptions ( ) | ||
return | array |
HTTP request options. |
---|
protected function defaultRequestOptions()
{
return [
'userAgent' => Yii::$app->name . ' OAuth ' . $this->version . ' Client',
'timeout' => 30,
'sslVerifyPeer' => false,
];
}
Composes default $returnUrl value.
protected string defaultReturnUrl ( ) | ||
return | string |
Return URL. |
---|
protected function defaultReturnUrl()
{
$params = Yii::$app->getRequest()->getQueryParams();
unset($params['oauth_token']);
$params[0] = Yii::$app->controller->getRoute();
return Yii::$app->getUrlManager()->createAbsoluteUrl($params);
}
Defined in: yii\authclient\BaseClient::defaultTitle()
Generates service title.
protected string defaultTitle ( ) | ||
return | string |
Service title. |
---|
protected function defaultTitle()
{
return StringHelper::basename(get_class($this));
}
Defined in: yii\authclient\BaseClient::defaultViewOptions()
Returns the default $viewOptions value.
Particular client may override this method in order to provide specific default view options.
protected array defaultViewOptions ( ) | ||
return | array |
List of default $viewOptions |
---|
protected function defaultViewOptions()
{
return [];
}
Fetches OAuth access token.
public yii\authclient\OAuthToken fetchAccessToken ( $oauthToken = null, yii\authclient\OAuthToken $requestToken = null, $oauthVerifier = null, array $params = [] ) | ||
$oauthToken | string |
OAuth token returned with redirection back to client. |
$requestToken | yii\authclient\OAuthToken |
OAuth request token. |
$oauthVerifier | string |
OAuth verifier. |
$params | array |
Additional request params. |
return | yii\authclient\OAuthToken |
OAuth access token. |
---|---|---|
throws | \yii\base\InvalidParamException |
on failure. |
throws | \yii\web\HttpException |
in case oauth token miss-matches request token. |
public function fetchAccessToken($oauthToken = null, OAuthToken $requestToken = null, $oauthVerifier = null, array $params = [])
{
$incomingRequest = Yii::$app->getRequest();
if ($oauthToken === null) {
$oauthToken = $incomingRequest->get('oauth_token', $incomingRequest->post('oauth_token', $oauthToken));
}
if (!is_object($requestToken)) {
$requestToken = $this->getState('requestToken');
if (!is_object($requestToken)) {
throw new InvalidParamException('Request token is required to fetch access token!');
}
}
if (strcmp($requestToken->getToken(), $oauthToken) !== 0) {
throw new HttpException(400, 'Invalid auth state parameter.');
}
$this->removeState('requestToken');
$defaultParams = [
'oauth_consumer_key' => $this->consumerKey,
'oauth_token' => $requestToken->getToken()
];
if ($oauthVerifier === null) {
$oauthVerifier = $incomingRequest->get('oauth_verifier', $incomingRequest->post('oauth_verifier'));
}
if (!empty($oauthVerifier)) {
$defaultParams['oauth_verifier'] = $oauthVerifier;
}
$request = $this->createRequest()
->setMethod($this->accessTokenMethod)
->setUrl($this->accessTokenUrl)
->setData(array_merge($defaultParams, $params));
$this->signRequest($request, $requestToken);
$response = $this->sendRequest($request);
$token = $this->createToken([
'params' => $response
]);
$this->setAccessToken($token);
return $token;
}
Fetches the OAuth request token.
public yii\authclient\OAuthToken fetchRequestToken ( array $params = [] ) | ||
$params | array |
Additional request params. |
return | yii\authclient\OAuthToken |
Request token. |
---|
public function fetchRequestToken(array $params = [])
{
$this->setAccessToken(null);
$defaultParams = [
'oauth_consumer_key' => $this->consumerKey,
'oauth_callback' => $this->getReturnUrl(),
//'xoauth_displayname' => Yii::$app->name,
];
if (!empty($this->scope)) {
$defaultParams['scope'] = $this->scope;
}
$request = $this->createRequest()
->setMethod($this->requestTokenMethod)
->setUrl($this->requestTokenUrl)
->setData(array_merge($defaultParams, $params));
$this->signRequest($request);
$response = $this->sendRequest($request);
$token = $this->createToken([
'params' => $response
]);
$this->setState('requestToken', $token);
return $token;
}
Generate common request params like version, timestamp etc.
protected array generateCommonRequestParams ( ) | ||
return | array |
Common request params. |
---|
protected function generateCommonRequestParams()
{
$params = [
'oauth_version' => $this->version,
'oauth_nonce' => $this->generateNonce(),
'oauth_timestamp' => $this->generateTimestamp(),
];
return $params;
}
Generates nonce value.
protected string generateNonce ( ) | ||
return | string |
Nonce value. |
---|
protected function generateNonce()
{
return md5(microtime() . mt_rand());
}
Generates timestamp.
protected integer generateTimestamp ( ) | ||
return | integer |
Timestamp. |
---|
protected function generateTimestamp()
{
return time();
}
Defined in: yii\authclient\BaseOAuth::getAccessToken()
public yii\authclient\OAuthToken getAccessToken ( ) | ||
return | yii\authclient\OAuthToken |
Auth token instance. |
---|
public function getAccessToken()
{
if (!is_object($this->_accessToken)) {
$this->_accessToken = $this->restoreAccessToken();
}
return $this->_accessToken;
}
Defined in: yii\authclient\BaseClient::getHttpClient()
Returns HTTP client.
public \yii\httpclient\Client getHttpClient ( ) | ||
return | \yii\httpclient\Client |
Internal HTTP client. |
---|
public function getHttpClient()
{
if (!is_object($this->_httpClient)) {
$this->_httpClient = $this->createHttpClient($this->_httpClient);
}
return $this->_httpClient;
}
Defined in: yii\authclient\BaseClient::getId()
public string getId ( ) | ||
return | string |
Service id |
---|
public function getId()
{
if (empty($this->_id)) {
$this->_id = $this->getName();
}
return $this->_id;
}
Defined in: yii\authclient\BaseClient::getName()
public string getName ( ) | ||
return | string |
Service name. |
---|
public function getName()
{
if ($this->_name === null) {
$this->_name = $this->defaultName();
}
return $this->_name;
}
public array getNormalizeUserAttributeMap ( ) | ||
return | array |
Normalize user attribute map. |
---|
public function getNormalizeUserAttributeMap()
{
if ($this->_normalizeUserAttributeMap === null) {
$this->_normalizeUserAttributeMap = $this->defaultNormalizeUserAttributeMap();
}
return $this->_normalizeUserAttributeMap;
}
Defined in: yii\authclient\BaseClient::getRequestOptions()
public array getRequestOptions ( ) | ||
return | array |
HTTP request options. |
---|
public function getRequestOptions()
{
return $this->_requestOptions;
}
Defined in: yii\authclient\BaseOAuth::getReturnUrl()
public string getReturnUrl ( ) | ||
return | string |
Return URL. |
---|
public function getReturnUrl()
{
if ($this->_returnUrl === null) {
$this->_returnUrl = $this->defaultReturnUrl();
}
return $this->_returnUrl;
}
Defined in: yii\authclient\BaseOAuth::getSignatureMethod()
public yii\authclient\signature\BaseMethod getSignatureMethod ( ) | ||
return | yii\authclient\signature\BaseMethod |
Signature method instance. |
---|
public function getSignatureMethod()
{
if (!is_object($this->_signatureMethod)) {
$this->_signatureMethod = $this->createSignatureMethod($this->_signatureMethod);
}
return $this->_signatureMethod;
}
Defined in: yii\authclient\BaseClient::getState()
Returns persistent state value.
protected mixed getState ( $key ) | ||
$key | string |
State key. |
return | mixed |
State value. |
---|
protected function getState($key)
{
return $this->getStateStorage()->get($this->getStateKeyPrefix() . $key);
}
Defined in: yii\authclient\BaseClient::getStateKeyPrefix()
Returns session key prefix, which is used to store internal states.
protected string getStateKeyPrefix ( ) | ||
return | string |
Session key prefix. |
---|
protected function getStateKeyPrefix()
{
return get_class($this) . '_' . $this->getId() . '_';
}
Defined in: yii\authclient\BaseClient::getStateStorage()
public yii\authclient\StateStorageInterface getStateStorage ( ) | ||
return | yii\authclient\StateStorageInterface |
Stage storage. |
---|
public function getStateStorage()
{
if (!is_object($this->_stateStorage)) {
$this->_stateStorage = Yii::createObject($this->_stateStorage);
}
return $this->_stateStorage;
}
Defined in: yii\authclient\BaseClient::getTitle()
public string getTitle ( ) | ||
return | string |
Service title. |
---|
public function getTitle()
{
if ($this->_title === null) {
$this->_title = $this->defaultTitle();
}
return $this->_title;
}
Defined in: yii\authclient\BaseClient::getUserAttributes()
public array getUserAttributes ( ) | ||
return | array |
List of user attributes |
---|
public function getUserAttributes()
{
if ($this->_userAttributes === null) {
$this->_userAttributes = $this->normalizeUserAttributes($this->initUserAttributes());
}
return $this->_userAttributes;
}
Defined in: yii\authclient\BaseClient::getViewOptions()
public array getViewOptions ( ) | ||
return | array |
View options in format: optionName => optionValue |
---|
public function getViewOptions()
{
if ($this->_viewOptions === null) {
$this->_viewOptions = $this->defaultViewOptions();
}
return $this->_viewOptions;
}
Defined in: yii\authclient\BaseClient::initUserAttributes()
Initializes authenticated user attributes.
protected abstract array initUserAttributes ( ) | ||
return | array |
Auth user attributes. |
---|
abstract protected function initUserAttributes();
Defined in: yii\authclient\BaseClient::normalizeUserAttributes()
Normalize given user attributes according to $normalizeUserAttributeMap.
protected array normalizeUserAttributes ( $attributes ) | ||
$attributes | array |
Raw attributes. |
return | array |
Normalized attributes. |
---|---|---|
throws | \yii\base\InvalidConfigException |
on incorrect normalize attribute map. |
protected function normalizeUserAttributes($attributes)
{
foreach ($this->getNormalizeUserAttributeMap() as $normalizedName => $actualName) {
if (is_scalar($actualName)) {
if (array_key_exists($actualName, $attributes)) {
$attributes[$normalizedName] = $attributes[$actualName];
}
} else {
if (is_callable($actualName)) {
$attributes[$normalizedName] = call_user_func($actualName, $attributes);
} elseif (is_array($actualName)) {
$haystack = $attributes;
$searchKeys = $actualName;
$isFound = true;
while (($key = array_shift($searchKeys)) !== null) {
if (is_array($haystack) && array_key_exists($key, $haystack)) {
$haystack = $haystack[$key];
} else {
$isFound = false;
break;
}
}
if ($isFound) {
$attributes[$normalizedName] = $haystack;
}
} else {
throw new InvalidConfigException('Invalid actual name "' . gettype($actualName) . '" specified at "' . get_class($this) . '::normalizeUserAttributeMap"');
}
}
}
return $attributes;
}
Gets new auth token to replace expired one.
public yii\authclient\OAuthToken refreshAccessToken ( yii\authclient\OAuthToken $token ) | ||
$token | yii\authclient\OAuthToken |
Expired auth token. |
return | yii\authclient\OAuthToken |
New auth token. |
---|
public function refreshAccessToken(OAuthToken $token)
{
// @todo
return null;
}
Defined in: yii\authclient\BaseClient::removeState()
Removes persistent state value.
protected boolean removeState ( $key ) | ||
$key | string |
State key. |
return | boolean |
Success. |
---|
protected function removeState($key)
{
return $this->getStateStorage()->remove($this->getStateKeyPrefix() . $key);
}
Defined in: yii\authclient\BaseOAuth::restoreAccessToken()
Restores access token.
protected yii\authclient\OAuthToken restoreAccessToken ( ) | ||
return | yii\authclient\OAuthToken |
Auth token. |
---|
protected function restoreAccessToken()
{
$token = $this->getState('token');
if (is_object($token)) {
/* @var $token OAuthToken */
if ($token->getIsExpired() && $this->autoRefreshAccessToken) {
$token = $this->refreshAccessToken($token);
}
}
return $token;
}
Defined in: yii\authclient\BaseOAuth::saveAccessToken()
Saves token as persistent state.
protected $this saveAccessToken ( $token ) | ||
$token | yii\authclient\OAuthToken|null |
Auth token to be saved. |
return | $this |
The object itself. |
---|
protected function saveAccessToken($token)
{
return $this->setState('token', $token);
}
Defined in: yii\authclient\BaseOAuth::sendRequest()
Sends the given HTTP request, returning response data.
protected array sendRequest ( $request ) | ||
$request | \yii\httpclient\Request |
HTTP request to be sent. |
return | array |
Response data. |
---|---|---|
throws | yii\authclient\InvalidResponseException |
on invalid remote response. |
protected function sendRequest($request)
{
$response = $request->send();
if (!$response->getIsOk()) {
throw new InvalidResponseException($response, 'Request failed with code: ' . $response->getStatusCode() . ', message: ' . $response->getContent());
}
return $response->getData();
}
Defined in: yii\authclient\BaseOAuth::setAccessToken()
Sets access token to be used.
public void setAccessToken ( $token ) | ||
$token | array|yii\authclient\OAuthToken |
Access token or its configuration. |
public function setAccessToken($token)
{
if (!is_object($token) && $token !== null) {
$token = $this->createToken($token);
}
$this->_accessToken = $token;
$this->saveAccessToken($token);
}
Defined in: yii\authclient\BaseOAuth::setHttpClient()
Sets HTTP client to be used.
public void setHttpClient ( $httpClient ) | ||
$httpClient | array|\yii\httpclient\Client |
Internal HTTP client. |
public function setHttpClient($httpClient)
{
if (is_object($httpClient)) {
$httpClient = clone $httpClient;
$httpClient->baseUrl = $this->apiBaseUrl;
}
parent::setHttpClient($httpClient);
}
Defined in: yii\authclient\BaseClient::setId()
public void setId ( $id ) | ||
$id | string |
Service id. |
public function setId($id)
{
$this->_id = $id;
}
Defined in: yii\authclient\BaseClient::setName()
public void setName ( $name ) | ||
$name | string |
Service name. |
public function setName($name)
{
$this->_name = $name;
}
public void setNormalizeUserAttributeMap ( $normalizeUserAttributeMap ) | ||
$normalizeUserAttributeMap | array |
Normalize user attribute map. |
public function setNormalizeUserAttributeMap($normalizeUserAttributeMap)
{
$this->_normalizeUserAttributeMap = $normalizeUserAttributeMap;
}
Defined in: yii\authclient\BaseClient::setRequestOptions()
public void setRequestOptions ( array $options ) | ||
$options | array |
HTTP request options. |
public function setRequestOptions(array $options)
{
$this->_requestOptions = $options;
}
Defined in: yii\authclient\BaseOAuth::setReturnUrl()
public void setReturnUrl ( $returnUrl ) | ||
$returnUrl | string |
Return URL |
public function setReturnUrl($returnUrl)
{
$this->_returnUrl = $returnUrl;
}
Defined in: yii\authclient\BaseOAuth::setSignatureMethod()
Set signature method to be used.
public void setSignatureMethod ( $signatureMethod ) | ||
$signatureMethod | array|yii\authclient\signature\BaseMethod |
Signature method instance or its array configuration. |
throws | \yii\base\InvalidParamException |
on wrong argument. |
---|
public function setSignatureMethod($signatureMethod)
{
if (!is_object($signatureMethod) && !is_array($signatureMethod)) {
throw new InvalidParamException('"' . get_class($this) . '::signatureMethod" should be instance of "\yii\autclient\signature\BaseMethod" or its array configuration. "' . gettype($signatureMethod) . '" has been given.');
}
$this->_signatureMethod = $signatureMethod;
}
Defined in: yii\authclient\BaseClient::setState()
Sets persistent state.
protected $this setState ( $key, $value ) | ||
$key | string |
State key. |
$value | mixed |
State value |
return | $this |
The object itself |
---|
protected function setState($key, $value)
{
$this->getStateStorage()->set($this->getStateKeyPrefix() . $key, $value);
return $this;
}
Defined in: yii\authclient\BaseClient::setStateStorage()
public void setStateStorage ( $stateStorage ) | ||
$stateStorage | yii\authclient\StateStorageInterface|array|string |
Stage storage to be used. |
public function setStateStorage($stateStorage)
{
$this->_stateStorage = $stateStorage;
}
Defined in: yii\authclient\BaseClient::setTitle()
public void setTitle ( $title ) | ||
$title | string |
Service title. |
public function setTitle($title)
{
$this->_title = $title;
}
Defined in: yii\authclient\BaseClient::setUserAttributes()
public void setUserAttributes ( $userAttributes ) | ||
$userAttributes | array |
List of user attributes |
public function setUserAttributes($userAttributes)
{
$this->_userAttributes = $this->normalizeUserAttributes($userAttributes);
}
Defined in: yii\authclient\BaseClient::setViewOptions()
public void setViewOptions ( $viewOptions ) | ||
$viewOptions | array |
View options in format: optionName => optionValue |
public function setViewOptions($viewOptions)
{
$this->_viewOptions = $viewOptions;
}
Sign given request with $signatureMethod.
public void signRequest ( $request, $token = null ) | ||
$request | \yii\httpclient\Request |
Request instance. |
$token | yii\authclient\OAuthToken|null |
OAuth token to be used for signature, if not set $accessToken will be used. |
Version | Description |
---|---|
2.1 | this method is public. |
public function signRequest($request, $token = null)
{
$params = $request->getData();
if (isset($params['oauth_signature_method']) || $request->hasHeaders() && $request->getHeaders()->has('authorization')) {
// avoid double sign of request
return;
}
if (empty($params)) {
$params = $this->generateCommonRequestParams();
} else {
$params = array_merge($this->generateCommonRequestParams(), $params);
}
$url = $request->getFullUrl();
$signatureMethod = $this->getSignatureMethod();
$params['oauth_signature_method'] = $signatureMethod->getName();
$signatureBaseString = $this->composeSignatureBaseString($request->getMethod(), $url, $params);
$signatureKey = $this->composeSignatureKey($token);
$params['oauth_signature'] = $signatureMethod->generateSignature($signatureBaseString, $signatureKey);
if ($this->authorizationHeaderMethods === null || in_array(strtoupper($request->getMethod()), array_map('strtoupper', $this->authorizationHeaderMethods), true)) {
$authorizationHeader = $this->composeAuthorizationHeader($params);
if (!empty($authorizationHeader)) {
$request->addHeaders($authorizationHeader);
// removing authorization header params, avoiding duplicate param server error :
foreach ($params as $key => $value) {
if (substr_compare($key, 'oauth', 0, 5) === 0) {
unset($params[$key]);
}
}
}
}
$request->setData($params);
}