Skip to content

Commit

Permalink
use league/oauth2-client 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
arai-ta committed Apr 15, 2023
1 parent c3a9432 commit 651f7c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 57 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"require": {
"php": "^7.0 || ^8.0",
"league/oauth2-client": ">=2.2.0 <2.4.0"
"league/oauth2-client": "^2.6.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0 || ^9.0"
Expand Down
63 changes: 7 additions & 56 deletions src/ChatWorkProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace ChatWork\OAuth2\Client;

use League\OAuth2\Client\Grant\AuthorizationCode;
use League\OAuth2\Client\Grant\RefreshToken;
use League\OAuth2\Client\OptionProvider\HttpBasicAuthOptionProvider;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
Expand All @@ -26,11 +25,10 @@ final class ChatWorkProvider extends AbstractProvider
*/
public function __construct(string $clientId, string $clientSecret, string $redirectUri = '', array $collaborators = [])
{
parent::__construct([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri
], $collaborators);
// use Basic Auth: https://developer.chatwork.com/docs/oauth#%E8%AA%8D%E8%A8%BC
$collaborators += ['optionProvider' => new HttpBasicAuthOptionProvider()];

parent::__construct(compact('clientId', 'clientSecret', 'redirectUri') , $collaborators);
}

/**
Expand Down Expand Up @@ -69,7 +67,7 @@ protected function getDefaultScopes(): array
/**
* @inheritdoc
*/
protected function checkResponse(ResponseInterface $response, $data): void
protected function checkResponse(ResponseInterface $response, $data)
{
if (isset($data['error'])) {
throw new IdentityProviderException(
Expand Down Expand Up @@ -116,56 +114,9 @@ protected function createResourceOwner(array $response, AccessToken $token): Res
/**
* @inheritdoc
*/
protected function getScopeSeparator()
protected function getScopeSeparator(): string
{
return ' ';
}

/**
* @inheritdoc
*/
protected function getAccessTokenOptions(array $params)
{
$options = [
'headers' => [
'content-type' => 'application/x-www-form-urlencoded',
'authorization' => 'Basic ' . base64_encode($params['client_id'] . ':' . $params['client_secret']),
]
];

if ($this->getAccessTokenMethod() === self::METHOD_POST) {
$options['body'] = $this->getAccessTokenBody(
$params['grant_type'] === (string) new AuthorizationCode() ? $this->getAuthorizationCodeGrantOptions($params) : $this->getRefreshTokenGrantOptions($params)
);
}

return $options;
}

/**
* @param array $params
* @return array
*/
private function getAuthorizationCodeGrantOptions(array $params)
{
return [
'code' => $params['code'],
'redirect_uri' => $params['redirect_uri'],
'grant_type' => (string) new AuthorizationCode()
];
}

/**
* @param array $params
* @return array
*/
private function getRefreshTokenGrantOptions(array $params)
{
return [
'refresh_token' => $params['refresh_token'],
'redirect_uri' => $params['redirect_uri'],
'grant_type' => (string) new RefreshToken()
];
}

}

0 comments on commit 651f7c3

Please sign in to comment.