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:
OAuth client ID.
public string $clientId = null
OAuth client secret.
public string $clientSecret = null
Token request URL endpoint.
public string $tokenUrl = null
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
Protocol version.
public string $version = '2.0'
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.
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. |
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. |
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['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. |
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-authclient-oauth2.html