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
Public Methods
Protected Methods
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
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. |
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 - $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. |
---|
Composes default $returnUrl value.
protected string defaultReturnUrl ( ) | ||
return | string | Return URL. |
---|
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. |
---|