Skip to content

Commit

Permalink
Add coding.net.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Oct 10, 2022
1 parent f76a57d commit ab63676
Show file tree
Hide file tree
Showing 36 changed files with 647 additions and 455 deletions.
309 changes: 168 additions & 141 deletions README.md

Large diffs are not rendered by default.

308 changes: 157 additions & 151 deletions README_CN.md → README_EN.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"symfony/psr-http-message-bridge": "^2.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"mockery/mockery": "^1.3",
"phpunit/phpunit": "^9.0",
"jetbrains/phpstorm-attributes": "^1.0",
"phpstan/phpstan": "^1.7"
"phpstan/phpstan": "^1.7",
"laravel/pint": "^1.2"
},
"license": "MIT",
"authors": [
Expand All @@ -39,8 +39,8 @@
}
],
"scripts": {
"check-style": "@php vendor/bin/php-cs-fixer fix --using-cache=no --diff --dry-run --ansi .",
"fix-style": "@php vendor/bin/php-cs-fixer fix --using-cache=no --ansi .",
"check-style": "@php vendor/bin/pint --test",
"fix-style": "@php vendor/bin/pint",
"test": "@php vendor/bin/phpunit --colors=always",
"phpstan": "@php vendor/bin/phpstan analyse src"
}
Expand Down
6 changes: 3 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Config implements ArrayAccess, JsonSerializable
protected array $config;

/**
* @param array $config
* @param array $config
*/
public function __construct(array $config)
{
Expand All @@ -26,7 +26,7 @@ public function get(string $key, mixed $default = null): mixed
}

foreach (\explode('.', $key) as $segment) {
if (!\is_array($config) || !\array_key_exists($segment, $config)) {
if (! \is_array($config) || ! \array_key_exists($segment, $config)) {
return $default;
}
$config = $config[$segment];
Expand All @@ -42,7 +42,7 @@ public function set(string $key, mixed $value): array

while (\count($keys) > 1) {
$key = \array_shift($keys);
if (!isset($config[$key]) || !\is_array($config[$key])) {
if (! isset($config[$key]) || ! \is_array($config[$key])) {
$config[$key] = [];
}
$config = &$config[$key];
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function withRedirectUrl(string $redirectUrl): self;
public function withState(string $state): self;

/**
* @param string[] $scopes
* @param string[] $scopes
*/
public function scopes(array $scopes): self;

Expand Down
25 changes: 16 additions & 9 deletions src/Providers/Alipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@ class Alipay extends Base
public const NAME = 'alipay';

protected string $baseUrl = 'https://openapi.alipay.com/gateway.do';

protected string $authUrl = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm';

protected array $scopes = ['auth_user'];

protected string $apiVersion = '1.0';

protected string $signType = 'RSA2';

protected string $postCharset = 'UTF-8';

protected string $format = 'json';

protected bool $sandbox = false;

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->baseUrl = 'https://openapi.alipaydev.com/gateway.do';
$this->authUrl = 'https://openauth.alipaydev.com/oauth2/publicAppAuthorize.htm';
Expand Down Expand Up @@ -65,7 +72,7 @@ protected function getUserByToken(string $token): array

$response = $this->fromJsonBody($responseInstance);

if (!empty($response['error_response'] ?? null) || empty($response['alipay_user_info_share_response'] ?? [])) {
if (! empty($response['error_response'] ?? null) || empty($response['alipay_user_info_share_response'] ?? [])) {
throw new Exceptions\BadRequestException((string) $responseInstance->getBody());
}

Expand Down Expand Up @@ -99,8 +106,8 @@ public function tokenFromCode(string $code): array
);
$response = $this->fromJsonBody($responseInstance);

if (!empty($response['error_response'])) {
throw new Exceptions\BadRequestException((string)$responseInstance->getBody());
if (! empty($response['error_response'])) {
throw new Exceptions\BadRequestException((string) $responseInstance->getBody());
}

return $this->normalizeAccessTokenResponse($response['alipay_system_oauth_token_response']);
Expand Down Expand Up @@ -153,7 +160,7 @@ protected function getTokenFields(string $code): array
'sign_type' => 'string',
'method' => 'string',
'timestamp' => 'string',
'version' => 'string'
'version' => 'string',
])]
public function getPublicFields(string $method): array
{
Expand Down Expand Up @@ -187,9 +194,9 @@ protected function signWithSHA256RSA(string $signContent, string $key): string
throw new Exceptions\InvalidArgumentException('no RSA private key set.');
}

$key = "-----BEGIN RSA PRIVATE KEY-----\n" .
\chunk_split($key, 64, "\n") .
"-----END RSA PRIVATE KEY-----";
$key = "-----BEGIN RSA PRIVATE KEY-----\n".
\chunk_split($key, 64, "\n").
'-----END RSA PRIVATE KEY-----';

\openssl_sign($signContent, $signValue, $key, \OPENSSL_ALGO_SHA256);

Expand All @@ -203,7 +210,7 @@ public static function buildParams(array $params, bool $urlencode = false, array
if (\in_array($k, $except)) {
continue;
}
$param_str .= $k . '=';
$param_str .= $k.'=';
$param_str .= $urlencode ? \rawurlencode($v) : $v;
$param_str .= '&';
}
Expand Down
9 changes: 5 additions & 4 deletions src/Providers/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ class Azure extends Base
public const NAME = 'azure';

protected array $scopes = ['User.Read'];

protected string $scopeSeparator = ' ';

protected function getAuthUrl(): string
{
return $this->buildAuthUrlFromBase($this->getBaseUrl() . '/oauth2/v2.0/authorize');
return $this->buildAuthUrlFromBase($this->getBaseUrl().'/oauth2/v2.0/authorize');
}

protected function getBaseUrl(): string
{
return 'https://login.microsoftonline.com/' . $this->config['tenant'];
return 'https://login.microsoftonline.com/'.$this->config['tenant'];
}

protected function getTokenUrl(): string
{
return $this->getBaseUrl() . '/oauth2/v2.0/token';
return $this->getBaseUrl().'/oauth2/v2.0/token';
}

protected function getUserByToken(string $token, ?array $query = []): array
Expand All @@ -35,7 +36,7 @@ protected function getUserByToken(string $token, ?array $query = []): array
'https://graph.microsoft.com/v1.0/me',
['headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
'Authorization' => 'Bearer '.$token,
],
]
);
Expand Down
11 changes: 7 additions & 4 deletions src/Providers/Baidu.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class Baidu extends Base
public const NAME = 'baidu';

protected string $baseUrl = 'https://openapi.baidu.com';

protected string $version = '2.0';

protected array $scopes = ['basic'];

protected string $display = 'popup';

public function withDisplay(string $display): self
Expand All @@ -35,7 +38,7 @@ public function withScopes(array $scopes): self

protected function getAuthUrl(): string
{
return $this->buildAuthUrlFromBase($this->baseUrl . '/oauth/' . $this->version . '/authorize');
return $this->buildAuthUrlFromBase($this->baseUrl.'/oauth/'.$this->version.'/authorize');
}

protected function getCodeFields(): array
Expand All @@ -51,7 +54,7 @@ protected function getCodeFields(): array

protected function getTokenUrl(): string
{
return $this->baseUrl . '/oauth/' . $this->version . '/token';
return $this->baseUrl.'/oauth/'.$this->version.'/token';
}

#[ArrayShape([
Expand All @@ -71,7 +74,7 @@ protected function getTokenFields(string $code): array
protected function getUserByToken(string $token): array
{
$response = $this->getHttpClient()->get(
$this->baseUrl . '/rest/' . $this->version . '/passport/users/getInfo',
$this->baseUrl.'/rest/'.$this->version.'/passport/users/getInfo',
[
'query' => [
Contracts\RFC6749_ABNF_ACCESS_TOKEN => $token,
Expand All @@ -93,7 +96,7 @@ protected function mapUserToObject(array $user): Contracts\UserInterface
Contracts\ABNF_NICKNAME => $user['realname'] ?? null,
Contracts\ABNF_NAME => $user['username'] ?? null,
Contracts\ABNF_EMAIL => '',
Contracts\ABNF_AVATAR => $user['portrait'] ? 'http://tb.himg.baidu.com/sys/portraitn/item/' . $user['portrait'] : null,
Contracts\ABNF_AVATAR => $user['portrait'] ? 'http://tb.himg.baidu.com/sys/portraitn/item/'.$user['portrait'] : null,
]);
}
}
35 changes: 23 additions & 12 deletions src/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Utils;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
use JetBrains\PhpStorm\ArrayShape;
use Overtrue\Socialite\Config;
use Overtrue\Socialite\Contracts;
use Overtrue\Socialite\Exceptions;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;

class_exists(Contracts\FactoryInterface::class);
class_exists(Contracts\UserInterface::class);
Expand All @@ -19,16 +19,27 @@ abstract class Base implements Contracts\ProviderInterface
public const NAME = null;

protected ?string $state = null;

protected Config $config;

protected ?string $redirectUrl;

protected array $parameters = [];

protected array $scopes = [];

protected string $scopeSeparator = ',';

protected GuzzleClient $httpClient;

protected array $guzzleOptions = [];

protected int $encodingType = PHP_QUERY_RFC1738;

protected string $expiresInKey = Contracts\RFC6749_ABNF_EXPIRES_IN;

protected string $accessTokenKey = Contracts\RFC6749_ABNF_ACCESS_TOKEN;

protected string $refreshTokenKey = Contracts\RFC6749_ABNF_REFRESH_TOKEN;

public function __construct(array $config)
Expand All @@ -39,27 +50,27 @@ public function __construct(array $config)
if ($this->config->has('scopes') && is_array($this->config->get('scopes'))) {
$this->scopes = $this->getConfig()->get('scopes');
} elseif ($this->config->has(Contracts\RFC6749_ABNF_SCOPE) && is_string($this->getConfig()->get(Contracts\RFC6749_ABNF_SCOPE))) {
$this->scopes = array($this->getConfig()->get(Contracts\RFC6749_ABNF_SCOPE));
$this->scopes = [$this->getConfig()->get(Contracts\RFC6749_ABNF_SCOPE)];
}

// normalize Contracts\RFC6749_ABNF_CLIENT_ID
if (!$this->config->has(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) {
$this->config->set(Contracts\RFC6749_ABNF_CLIENT_ID, $id);
}
}

// normalize Contracts\RFC6749_ABNF_CLIENT_SECRET
if (!$this->config->has(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) {
$this->config->set(Contracts\RFC6749_ABNF_CLIENT_SECRET, $secret);
}
}

// normalize 'redirect_url'
if (!$this->config->has('redirect_url')) {
if (! $this->config->has('redirect_url')) {
$this->config->set('redirect_url', $this->config->get('redirect'));
}
$this->redirectUrl = $this->config->get('redirect_url');
Expand All @@ -75,7 +86,7 @@ abstract protected function mapUserToObject(array $user): Contracts\UserInterfac

public function redirect(?string $redirectUrl = null): string
{
if (!empty($redirectUrl)) {
if (! empty($redirectUrl)) {
$this->withRedirectUrl($redirectUrl);
}

Expand Down Expand Up @@ -111,7 +122,7 @@ public function tokenFromCode(string $code): array
]
);

return $this->normalizeAccessTokenResponse((string)$response->getBody());
return $this->normalizeAccessTokenResponse((string) $response->getBody());
}

/**
Expand Down Expand Up @@ -198,7 +209,7 @@ protected function formatScopes(array $scopes, string $scopeSeparator): string
Contracts\RFC6749_ABNF_CLIENT_ID => 'null|string',
Contracts\RFC6749_ABNF_CLIENT_SECRET => 'null|string',
Contracts\RFC6749_ABNF_CODE => 'string',
Contracts\RFC6749_ABNF_REDIRECT_URI => 'null|string'
Contracts\RFC6749_ABNF_REDIRECT_URI => 'null|string',
])]
protected function getTokenFields(string $code): array
{
Expand All @@ -214,7 +225,7 @@ protected function buildAuthUrlFromBase(string $url): string
{
$query = $this->getCodeFields() + ($this->state ? [Contracts\RFC6749_ABNF_STATE => $this->state] : []);

return $url . '?' . \http_build_query($query, '', '&', $this->encodingType);
return $url.'?'.\http_build_query($query, '', '&', $this->encodingType);
}

protected function getCodeFields(): array
Expand Down Expand Up @@ -250,12 +261,12 @@ protected function normalizeAccessTokenResponse(mixed $response): array
$response = Utils::jsonDecode($response, true);
}

if (!\is_array($response)) {
if (! \is_array($response)) {
throw new Exceptions\AuthorizeFailedException('Invalid token response', [$response]);
}

if (empty($response[$this->accessTokenKey])) {
throw new Exceptions\AuthorizeFailedException('Authorize Failed: ' . Utils::jsonEncode($response, \JSON_UNESCAPED_UNICODE), $response);
throw new Exceptions\AuthorizeFailedException('Authorize Failed: '.Utils::jsonEncode($response, \JSON_UNESCAPED_UNICODE), $response);
}

return $response + [
Expand Down
Loading

0 comments on commit ab63676

Please sign in to comment.