diff --git a/composer.json b/composer.json index 9b8eebe..17af1d0 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,7 @@ "php": ">=8.0.2", "ext-json": "*", "ext-openssl": "*", - "guzzlehttp/guzzle": "^7.0", - "symfony/psr-http-message-bridge": "^2.1|^6.0|^7.1" + "guzzlehttp/guzzle": "^7.0" }, "require-dev": { "mockery/mockery": "^1.3", diff --git a/src/Config.php b/src/Config.php index 550fa20..9b2018c 100644 --- a/src/Config.php +++ b/src/Config.php @@ -9,9 +9,6 @@ class Config implements ArrayAccess, JsonSerializable { protected array $config; - /** - * @param array $config - */ public function __construct(array $config) { $this->config = $config; diff --git a/src/Exceptions/BadRequestException.php b/src/Exceptions/BadRequestException.php index 9764c83..b4ef1ab 100644 --- a/src/Exceptions/BadRequestException.php +++ b/src/Exceptions/BadRequestException.php @@ -2,6 +2,4 @@ namespace Overtrue\Socialite\Exceptions; -class BadRequestException extends Exception -{ -} +class BadRequestException extends Exception {} diff --git a/src/Exceptions/FeiShu/InvalidTicketException.php b/src/Exceptions/FeiShu/InvalidTicketException.php index 1704318..935ec47 100644 --- a/src/Exceptions/FeiShu/InvalidTicketException.php +++ b/src/Exceptions/FeiShu/InvalidTicketException.php @@ -4,6 +4,4 @@ use Overtrue\Socialite\Exceptions; -class InvalidTicketException extends Exceptions\Exception -{ -} +class InvalidTicketException extends Exceptions\Exception {} diff --git a/src/Providers/Base.php b/src/Providers/Base.php index e3d6c20..54ecb29 100644 --- a/src/Providers/Base.php +++ b/src/Providers/Base.php @@ -15,29 +15,29 @@ abstract class Base implements Contracts\ProviderInterface { public const NAME = null; - protected ?string $state = null; + protected ?string $state = null; - protected Config $config; + protected Config $config; - protected ?string $redirectUrl; + protected ?string $redirectUrl; - protected array $parameters = []; + protected array $parameters = []; - protected array $scopes = []; + protected array $scopes = []; - protected string $scopeSeparator = ','; + protected string $scopeSeparator = ','; protected GuzzleClient $httpClient; - protected array $guzzleOptions = []; + protected array $guzzleOptions = []; - protected int $encodingType = PHP_QUERY_RFC1738; + protected int $encodingType = PHP_QUERY_RFC1738; - protected string $expiresInKey = Contracts\RFC6749_ABNF_EXPIRES_IN; + protected string $expiresInKey = Contracts\RFC6749_ABNF_EXPIRES_IN; - protected string $accessTokenKey = Contracts\RFC6749_ABNF_ACCESS_TOKEN; + protected string $accessTokenKey = Contracts\RFC6749_ABNF_ACCESS_TOKEN; - protected string $refreshTokenKey = Contracts\RFC6749_ABNF_REFRESH_TOKEN; + protected string $refreshTokenKey = Contracts\RFC6749_ABNF_REFRESH_TOKEN; public function __construct(array $config) { @@ -53,7 +53,7 @@ public function __construct(array $config) // normalize Contracts\RFC6749_ABNF_CLIENT_ID if (! $this->config->has(Contracts\RFC6749_ABNF_CLIENT_ID)) { $id = $this->config->get(Contracts\ABNF_APP_ID); - if (null != $id) { + if ($id != null) { $this->config->set(Contracts\RFC6749_ABNF_CLIENT_ID, $id); } } @@ -61,7 +61,7 @@ public function __construct(array $config) // normalize Contracts\RFC6749_ABNF_CLIENT_SECRET if (! $this->config->has(Contracts\RFC6749_ABNF_CLIENT_SECRET)) { $secret = $this->config->get(Contracts\ABNF_APP_SECRET); - if (null != $secret) { + if ($secret != null) { $this->config->set(Contracts\RFC6749_ABNF_CLIENT_SECRET, $secret); } } diff --git a/src/Providers/Coding.php b/src/Providers/Coding.php index d0c6222..63d46ed 100644 --- a/src/Providers/Coding.php +++ b/src/Providers/Coding.php @@ -11,7 +11,7 @@ class Coding extends Base { - public const NAME = 'coding'; + public const NAME = 'coding'; // example: https://{your-team}.coding.net protected string $teamUrl; diff --git a/src/Providers/Douban.php b/src/Providers/Douban.php index 48da1c3..b606af1 100644 --- a/src/Providers/Douban.php +++ b/src/Providers/Douban.php @@ -25,7 +25,6 @@ protected function getTokenUrl(): string } /** - * @param string $token * @param ?array $query */ protected function getUserByToken(string $token, ?array $query = []): array diff --git a/src/Providers/Facebook.php b/src/Providers/Facebook.php index 301d516..1ab9822 100644 --- a/src/Providers/Facebook.php +++ b/src/Providers/Facebook.php @@ -17,7 +17,7 @@ class Facebook extends Base protected string $version = 'v3.3'; - protected array $fields = ['first_name', 'last_name', 'email', 'gender', 'verified','picture']; + protected array $fields = ['first_name', 'last_name', 'email', 'gender', 'verified', 'picture']; protected array $scopes = []; diff --git a/src/Providers/FeiShu.php b/src/Providers/FeiShu.php index 6581cc7..2fce3b2 100644 --- a/src/Providers/FeiShu.php +++ b/src/Providers/FeiShu.php @@ -21,7 +21,7 @@ class FeiShu extends Base protected string $expiresInKey = 'refresh_expires_in'; - protected bool $isInternalApp = false; + protected bool $isInternalApp = false; public function __construct(array $config) { diff --git a/src/Providers/GitHub.php b/src/Providers/GitHub.php index 44f463e..84df89a 100644 --- a/src/Providers/GitHub.php +++ b/src/Providers/GitHub.php @@ -9,9 +9,9 @@ class GitHub extends Base { - public const NAME = 'github'; + public const NAME = 'github'; - protected array $scopes = ['read:user']; + protected array $scopes = ['read:user']; protected string $scopeSeparator = ' '; diff --git a/src/Providers/Linkedin.php b/src/Providers/Linkedin.php index f2e13f7..60b05c4 100644 --- a/src/Providers/Linkedin.php +++ b/src/Providers/Linkedin.php @@ -51,7 +51,7 @@ protected function getBasicProfile(string $token): array $response = $this->getHttpClient()->get($url, [ 'headers' => [ - 'Authorization' => 'Bearer ' . $token, + 'Authorization' => 'Bearer '.$token, 'X-RestLi-Protocol-Version' => '2.0.0', ], ]); @@ -65,7 +65,7 @@ protected function getEmailAddress(string $token): array $response = $this->getHttpClient()->get($url, [ 'headers' => [ - 'Authorization' => 'Bearer ' . $token, + 'Authorization' => 'Bearer '.$token, 'X-RestLi-Protocol-Version' => '2.0.0', ], ]); @@ -75,11 +75,11 @@ protected function getEmailAddress(string $token): array protected function mapUserToObject(array $user): Contracts\UserInterface { - $preferredLocale = ($user['firstName']['preferredLocale']['language'] ?? null) . '_' . + $preferredLocale = ($user['firstName']['preferredLocale']['language'] ?? null).'_'. ($user['firstName']['preferredLocale']['country'] ?? null); $firstName = $user['firstName']['localized'][$preferredLocale] ?? null; $lastName = $user['lastName']['localized'][$preferredLocale] ?? null; - $name = $firstName . ' ' . $lastName; + $name = $firstName.' '.$lastName; $images = $user['profilePicture']['displayImage~']['elements'] ?? []; $avatars = \array_filter($images, static fn ($image) => ($image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] ?? 0) === 100); diff --git a/src/Providers/PayPal.php b/src/Providers/PayPal.php index 3ee65aa..386d4a9 100644 --- a/src/Providers/PayPal.php +++ b/src/Providers/PayPal.php @@ -8,8 +8,9 @@ /** * Class PayPal + * * @author zhiqiang - * @package Overtrue\Socialite\Providers + * * @see https://developer.paypal.com/docs/log-in-with-paypal/ */ class PayPal extends Base @@ -17,19 +18,23 @@ class PayPal extends Base public const NAME = 'paypal'; protected string $scopeSeparator = ' '; + /** * @var string|null - * code or id_token + * code or id_token */ protected ?string $responseType = Contracts\RFC6749_ABNF_CODE; - protected string $flowEntry = 'static'; - protected string $authUrl = 'https://www.paypal.com/signin/authorize'; - protected string $tokenURL = "https://api.sandbox.paypal.com/v1/oauth2/token"; - protected string $userinfoURL = "https://api.paypal.com/v1/identity/openidconnect/userinfo"; + protected string $flowEntry = 'static'; + + protected string $authUrl = 'https://www.paypal.com/signin/authorize'; + + protected string $tokenURL = 'https://api.sandbox.paypal.com/v1/oauth2/token'; + + protected string $userinfoURL = 'https://api.paypal.com/v1/identity/openidconnect/userinfo'; protected array $scopes = [ - 'openid', 'profile', 'email', 'address' + 'openid', 'profile', 'email', 'address', ]; protected bool $sandbox = true; @@ -37,22 +42,23 @@ class PayPal extends Base public function __construct(array $config) { parent::__construct($config); - $this->sandbox = (bool)$this->config->get('sandbox', false); + $this->sandbox = (bool) $this->config->get('sandbox', false); if ($this->sandbox) { - $this->authUrl = 'https://www.sandbox.paypal.com/signin/authorize'; - $this->tokenURL = 'https://api-m.sandbox.paypal.com/v1/oauth2/token'; + $this->authUrl = 'https://www.sandbox.paypal.com/signin/authorize'; + $this->tokenURL = 'https://api-m.sandbox.paypal.com/v1/oauth2/token'; $this->userinfoURL = 'https://api-m.sandbox.paypal.com/v1/identity/openidconnect/userinfo'; } } /** - * @param string|null $responseType * @return $this + * * @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/generate-button/ */ public function withResponseType(?string $responseType) { $this->responseType = $responseType; + return $this; } @@ -65,11 +71,11 @@ protected function getCodeFields(): array { $fields = \array_merge( [ - 'flowEntry' => $this->flowEntry, - Contracts\RFC6749_ABNF_CLIENT_ID => $this->getClientId(), + 'flowEntry' => $this->flowEntry, + Contracts\RFC6749_ABNF_CLIENT_ID => $this->getClientId(), Contracts\RFC6749_ABNF_RESPONSE_TYPE => $this->responseType, - Contracts\RFC6749_ABNF_SCOPE => $this->formatScopes($this->scopes, $this->scopeSeparator), - Contracts\RFC6749_ABNF_REDIRECT_URI => $this->redirectUrl, + Contracts\RFC6749_ABNF_SCOPE => $this->formatScopes($this->scopes, $this->scopeSeparator), + Contracts\RFC6749_ABNF_REDIRECT_URI => $this->redirectUrl, ], $this->parameters ); @@ -77,20 +83,19 @@ protected function getCodeFields(): array if ($this->state) { $fields[Contracts\RFC6749_ABNF_STATE] = $this->state; } + return $fields; } - protected function getTokenUrl(): string { return $this->tokenURL; } /** - * @param string $code - * @return array * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException + * * @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-getaccesstoken */ public function tokenFromCode(string $code): array @@ -100,46 +105,46 @@ public function tokenFromCode(string $code): array [ 'form_params' => [ Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_AUTHORATION_CODE, - Contracts\RFC6749_ABNF_CODE => $code, + Contracts\RFC6749_ABNF_CODE => $code, ], - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())), + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Basic '.\base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())), ], ] ); - return $this->normalizeAccessTokenResponse((string)$response->getBody()); + + return $this->normalizeAccessTokenResponse((string) $response->getBody()); } /** - * @param string $refreshToken - * @return mixed * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException + * * @see https://developer.paypal.com/docs/log-in-with-paypal/integrate/#link-exchangerefreshtokenforaccesstoken */ public function refreshToken(string $refreshToken): mixed { - $response = $this->getHttpClient()->post( + $response = $this->getHttpClient()->post( $this->getTokenUrl(), [ 'form_params' => [ - Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_REFRESH_TOKEN, + Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_REFRESH_TOKEN, Contracts\RFC6749_ABNF_REFRESH_TOKEN => $refreshToken, ], - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())), + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Basic '.\base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())), ], ] ); - return $this->normalizeAccessTokenResponse((string)$response->getBody()); + + return $this->normalizeAccessTokenResponse((string) $response->getBody()); } /** - * @param string $token - * @return array * @throws \GuzzleHttp\Exception\GuzzleException + * * @see https://developer.paypal.com/docs/api/identity/v1/#userinfo_get */ protected function getUserByToken(string $token): array @@ -148,22 +153,24 @@ protected function getUserByToken(string $token): array $this->userinfoURL, [ 'headers' => [ - 'Content-Type' => 'application/x-www-form-urlencoded', - 'Authorization' => 'Bearer ' . $token, + 'Content-Type' => 'application/x-www-form-urlencoded', + 'Authorization' => 'Bearer '.$token, ], ] ); + return $this->fromJsonBody($response); } #[Pure] protected function mapUserToObject(array $user): Contracts\UserInterface { - $user[Contracts\ABNF_ID] = $user['user_id'] ?? null; + $user[Contracts\ABNF_ID] = $user['user_id'] ?? null; $user[Contracts\ABNF_NICKNAME] = $user['given_name'] ?? $user['family_name'] ?? $user['middle_name'] ?? null; - $user[Contracts\ABNF_NAME] = $user['name'] ?? ''; - $user[Contracts\ABNF_EMAIL] = $user[Contracts\ABNF_EMAIL] ?? null; - $user[Contracts\ABNF_AVATAR] = $user['picture'] ?? null; + $user[Contracts\ABNF_NAME] = $user['name'] ?? ''; + $user[Contracts\ABNF_EMAIL] = $user[Contracts\ABNF_EMAIL] ?? null; + $user[Contracts\ABNF_AVATAR] = $user['picture'] ?? null; + return new User($user); } } diff --git a/src/Providers/WeChat.php b/src/Providers/WeChat.php index 90173cb..f7a0ba8 100644 --- a/src/Providers/WeChat.php +++ b/src/Providers/WeChat.php @@ -205,11 +205,11 @@ protected function prepareForComponent(array $component): void } } - if (2 !== \count($config)) { + if (\count($config) !== 2) { throw new Exceptions\InvalidArgumentException('Please check your config arguments were available.'); } - if (1 === \count($this->scopes) && \in_array('snsapi_login', $this->scopes)) { + if (\count($this->scopes) === 1 && \in_array('snsapi_login', $this->scopes)) { $this->scopes = ['snsapi_base']; } diff --git a/src/Traits/HasAttributes.php b/src/Traits/HasAttributes.php index 9d88ad3..7e9c742 100644 --- a/src/Traits/HasAttributes.php +++ b/src/Traits/HasAttributes.php @@ -68,7 +68,7 @@ public function toJSON(): string { $result = \json_encode($this->getAttributes(), JSON_UNESCAPED_UNICODE); - false === $result && throw new Exceptions\Exception('Cannot Processing this instance as JSON stringify.'); + $result === false && throw new Exceptions\Exception('Cannot Processing this instance as JSON stringify.'); return $result; } diff --git a/tests/OAuthTest.php b/tests/OAuthTest.php index 0b40f5d..9be74f6 100644 --- a/tests/OAuthTest.php +++ b/tests/OAuthTest.php @@ -199,8 +199,6 @@ protected function mapUserToObject(array $user): User /** * Get a fresh instance of the Guzzle HTTP client. - * - * @return \GuzzleHttp\Client */ public function getHttpClient(): GuzzleHttp\Client {