Abstract Class yii\authclient\OAuth2
OAuth2 serves as a client for the OAuth 2 flow.
In oder to acquire access token perform following sequence:
use yii\authclient\OAuth2;
// assuming class MyAuthClient extends OAuth2
$oauthClient = new MyAuthClient();
$url = $oauthClient->buildAuthUrl(); // Build authorization URL
Yii::$app->getResponse()->redirect($url); // Redirect to authorization URL.
// After user returns at our site:
$code = Yii::$app->getRequest()->get('code');
$accessToken = $oauthClient->fetchAccessToken($code); // Get access token
See also:
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $apiBaseUrl | string | API base URL. | yii\authclient\BaseOAuth |
| $authUrl | string | Authorize URL. | yii\authclient\BaseOAuth |
| $autoRefreshAccessToken | boolean | Whether to automatically perform 'refresh access token' request on expired access token. | yii\authclient\BaseOAuth |
| $clientId | string | OAuth client ID. | yii\authclient\OAuth2 |
| $clientSecret | string | OAuth client secret. | yii\authclient\OAuth2 |
| $parametersToKeepInReturnUrl | array | List of the parameters to keep in default return url. | yii\authclient\BaseOAuth |
| $scope | string | Auth request scope. | yii\authclient\BaseOAuth |
| $tokenUrl | string | Token request URL endpoint. | yii\authclient\OAuth2 |
| $validateAuthState | boolean | Whether to use and validate auth 'state' parameter in authentication flow. | yii\authclient\OAuth2 |
| $version | string | Protocol version. | yii\authclient\OAuth2 |
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| applyClientCredentialsToRequest() | Applies client credentials (e.g. $clientId and $clientSecret) to the HTTP request instance. | yii\authclient\OAuth2 |
| composeUrl() | Composes URL from base URL and GET params. | yii\authclient\BaseOAuth |
| createHttpClient() | {@inheritdoc} | yii\authclient\BaseOAuth |
| createSignatureMethod() | Creates signature method instance from its configuration. | yii\authclient\BaseOAuth |
| createToken() | Creates token from its configuration. | yii\authclient\OAuth2 |
| defaultName() | Generates service name. | yii\authclient\BaseClient |
| defaultNormalizeUserAttributeMap() | Returns the default \yii\authclient\normalizeUserAttributeMap value. | yii\authclient\BaseClient |
| defaultRequestOptions() | {@inheritdoc} | yii\authclient\BaseOAuth |
| defaultReturnUrl() | Composes default \yii\authclient\returnUrl value. | yii\authclient\BaseOAuth |
| defaultTitle() | Generates service title. | yii\authclient\BaseClient |
| defaultViewOptions() | Returns the default \yii\authclient\viewOptions value. | yii\authclient\BaseClient |
| generateAuthState() | Generates the auth state value. | yii\authclient\OAuth2 |
| getState() | Returns persistent state value. | yii\authclient\BaseClient |
| getStateKeyPrefix() | Returns session key prefix, which is used to store internal states. | yii\authclient\BaseClient |
| initUserAttributes() | Initializes authenticated user attributes. | yii\authclient\BaseClient |
| normalizeUserAttributes() | Normalize given user attributes according to \yii\authclient\normalizeUserAttributeMap. | yii\authclient\BaseClient |
| removeState() | Removes persistent state value. | yii\authclient\BaseClient |
| restoreAccessToken() | Restores access token. | yii\authclient\BaseOAuth |
| saveAccessToken() | Saves token as persistent state. | yii\authclient\BaseOAuth |
| sendRequest() | Sends the given HTTP request, returning response data. | yii\authclient\BaseOAuth |
| setState() | Sets persistent state. | yii\authclient\BaseClient |
Property Details
OAuth client ID.
OAuth client secret.
Token request URL endpoint.
Whether to use and validate auth 'state' parameter in authentication flow. If enabled - the opaque value will be generated and applied to auth URL to maintain state between the request and callback. The authorization server includes this value, when redirecting the user-agent back to the client. The option is used for preventing cross-site request forgery.
Protocol version.
Method Details
{@inheritdoc}
| public void applyAccessTokenToRequest ( $request, $accessToken ) | ||
| $request | ||
| $accessToken | ||
Applies client credentials (e.g. $clientId and $clientSecret) to the HTTP request instance.
This method should be invoked before sending any HTTP request, which requires client credentials.
| protected void applyClientCredentialsToRequest ( $request ) | ||
| $request | \yii\httpclient\Request | HTTP request instance. |
Authenticate OAuth client directly at the provider without third party (user) involved, using 'client_credentials' grant type.
| public yii\authclient\OAuthToken authenticateClient ( $params = [] ) | ||
| $params | array | Additional request params. |
| return | yii\authclient\OAuthToken | Access token. |
|---|---|---|
Authenticates user directly by 'username/password' pair, using 'password' grant type.
| public yii\authclient\OAuthToken authenticateUser ( $username, $password, $params = [] ) | ||
| $username | string | User name. |
| $password | string | User password. |
| $params | array | Additional request params. |
| return | yii\authclient\OAuthToken | Access token. |
|---|---|---|
Authenticates user directly using JSON Web Token (JWT).
See also https://tools.ietf.org/html/rfc7515.
| public yii\authclient\OAuthToken authenticateUserJwt ( $username, $signature = null, $options = [], $params = [] ) | ||
| $username | string | |
| $signature | yii\authclient\signature\BaseMethod|array | Signature method or its array configuration. If empty - \yii\authclient\signatureMethod will be used. |
| $options | array | Additional options. Valid options are:
|
| $params | array | Additional request params. |
| return | yii\authclient\OAuthToken | Access token. |
|---|---|---|
Composes user authorization URL.
| public string buildAuthUrl ( array $params = [] ) | ||
| $params | array | Additional auth GET params. |
| return | string | Authorization URL. |
|---|---|---|
Creates token from its configuration.
| protected yii\authclient\OAuthToken createToken ( array $tokenConfig = [] ) | ||
| $tokenConfig | array | Token configuration. |
| return | yii\authclient\OAuthToken | Token instance. |
|---|---|---|
Fetches access token from authorization code.
| public yii\authclient\OAuthToken fetchAccessToken ( $authCode, array $params = [] ) | ||
| $authCode | string | Authorization code, usually comes at GET parameter 'code'. |
| $params | array | Additional request params. |
| return | yii\authclient\OAuthToken | Access token. |
|---|---|---|
| throws | yii\web\HttpException | on invalid auth state in case \yii\authclient\enableStateValidation is enabled. |
Generates the auth state value.
| protected string generateAuthState ( ) | ||
| return | string | Auth state value. |
|---|---|---|
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. |
|---|---|---|