|
8 | 8 | namespace yii\authclient;
|
9 | 9 |
|
10 | 10 | use Yii;
|
| 11 | +use yii\base\InvalidConfigException; |
11 | 12 | use yii\helpers\Json;
|
12 | 13 | use yii\helpers\Url;
|
13 | 14 | use yii\web\HttpException;
|
|
37 | 38 | */
|
38 | 39 | abstract class OAuth2 extends BaseOAuth
|
39 | 40 | {
|
| 41 | + /** |
| 42 | + * Apply the access token to the request header |
| 43 | + * @since 2.2.16 |
| 44 | + */ |
| 45 | + const ACCESS_TOKEN_LOCATION_HEADER = 'header'; |
| 46 | + |
| 47 | + /** |
| 48 | + * Apply the access token to the request body |
| 49 | + * @since 2.2.16 |
| 50 | + */ |
| 51 | + const ACCESS_TOKEN_LOCATION_BODY = 'body'; |
| 52 | + |
40 | 53 | /**
|
41 | 54 | * @var string protocol version.
|
42 | 55 | */
|
@@ -71,6 +84,15 @@ abstract class OAuth2 extends BaseOAuth
|
71 | 84 | */
|
72 | 85 | public $enablePkce = false;
|
73 | 86 |
|
| 87 | + /** |
| 88 | + * @var string The location of the access token when it is applied to the request. |
| 89 | + * NOTE: According to the OAuth2 specification this should be `header` by default, |
| 90 | + * however, for backwards compatibility the default value used here is `body`. |
| 91 | + * @since 2.2.16 |
| 92 | + * |
| 93 | + * @see https://datatracker.ietf.org/doc/html/rfc6749#section-7 |
| 94 | + */ |
| 95 | + public $accessTokenLocation = self::ACCESS_TOKEN_LOCATION_BODY; |
74 | 96 |
|
75 | 97 | /**
|
76 | 98 | * Composes user authorization URL.
|
@@ -167,12 +189,22 @@ public function fetchAccessToken($authCode, array $params = [])
|
167 | 189 |
|
168 | 190 | /**
|
169 | 191 | * {@inheritdoc}
|
| 192 | + * @throws InvalidConfigException |
170 | 193 | */
|
171 | 194 | public function applyAccessTokenToRequest($request, $accessToken)
|
172 | 195 | {
|
173 |
| - $data = $request->getData(); |
174 |
| - $data['access_token'] = $accessToken->getToken(); |
175 |
| - $request->setData($data); |
| 196 | + switch($this->accessTokenLocation) { |
| 197 | + case self::ACCESS_TOKEN_LOCATION_BODY: |
| 198 | + $data = $request->getData(); |
| 199 | + $data['access_token'] = $accessToken->getToken(); |
| 200 | + $request->setData($data); |
| 201 | + break; |
| 202 | + case self::ACCESS_TOKEN_LOCATION_HEADER: |
| 203 | + $request->getHeaders()->set('Authorization', 'Bearer ' . $accessToken->getToken()); |
| 204 | + break; |
| 205 | + default: |
| 206 | + throw new InvalidConfigException('Unknown access token location: ' . $this->accessTokenLocation); |
| 207 | + } |
176 | 208 | }
|
177 | 209 |
|
178 | 210 | /**
|
|
0 commit comments