On this page
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 = $_GET['code'];
$accessToken = $oauthClient->fetchAccessToken($code); // Get access token
See also:
Public Properties
Public Methods
Protected Methods
Property Details
$clientId public property
OAuth client ID.
public string $clientId = null
$clientSecret public property
OAuth client secret.
public string $clientSecret = null
$tokenUrl public property
Token request URL endpoint.
public string $tokenUrl = null
$validateAuthState public property (available since version 2.1)
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.
public boolean $validateAuthState = true
$version public property
Protocol version.
public string $version = '2.0'
Method Details
applyAccessTokenToRequest() public method
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. |
applyClientCredentialsToRequest() protected method (available since version 2.1.3)
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. |
authenticateClient() public method (available since version 2.1.0)
Authenticate OAuth client directly at the provider without third party (user) involved, using 'client_credentials' grant type.
See also http://tools.ietf.org/html/rfc6749#section-4.4.
public yii\authclient\OAuthToken authenticateClient ( $params = [] ) | ||
---|---|---|
$params | array | Additional request params. |
return | yii\authclient\OAuthToken | Access token. |
authenticateUser() public method (available since version 2.1.0)
Authenticates user directly by 'username/password' pair, using 'password' grant type.
See also https://tools.ietf.org/html/rfc6749#section-4.3.
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. |
authenticateUserJwt() public method (available since version 2.1.3)
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. |
buildAuthUrl() public method
Composes user authorization URL.
public string buildAuthUrl ( array $params = [] ) | ||
---|---|---|
$params | array | Additional auth GET params. |
return | string | Authorization URL. |
createToken() protected method
Creates token from its configuration.
protected yii\authclient\OAuthToken createToken ( array $tokenConfig = [] ) | ||
---|---|---|
$tokenConfig | array | Token configuration. |
return | yii\authclient\OAuthToken | Token instance. |
defaultReturnUrl() protected method
Composes default $returnUrl value.
protected string defaultReturnUrl ( ) | ||
---|---|---|
return | string | Return URL. |
fetchAccessToken() public method
Fetches access token from authorization code.
public yii\authclient\OAuthToken fetchAccessToken ( $authCode, array $params = [] ) | ||
---|---|---|
$authCode | string | Authorization code, usually comes at $_GET['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. |
generateAuthState() protected method (available since version 2.1)
Generates the auth state value.
protected string generateAuthState ( ) | ||
---|---|---|
return | string | Auth state value. |
refreshAccessToken() public method
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. |
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-authclient-oauth2.html