Skip to content

Commit

Permalink
socialite 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Sep 20, 2020
1 parent ab19b30 commit 8cb8383
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 50 deletions.
65 changes: 42 additions & 23 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
<?php
$header = <<<EOF
This file is part of the overtrue/laravel-wechat.
(c) overtrue <[email protected]>
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'header_comment' => array('header' => $header),
'array_syntax' => array('syntax' => 'short'),
'ordered_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_construct' => true,
'php_unit_strict' => true,
))
->setRules([
'@PSR2' => true,
'binary_operator_spaces' => true,
'blank_line_after_opening_tag' => true,
'compact_nullable_typehint' => true,
'declare_equal_normalize' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'new_with_braces' => true,
'no_unused_imports' => true,
'no_blank_lines_after_class_opening' => true,
'no_leading_import_slash' => true,
'no_whitespace_in_blank_line' => true,
'ordered_class_elements' => [
'order' => [
'use_trait',
],
],
'ordered_imports' => [
'imports_order' => [
'class',
'function',
'const',
],
'sort_algorithm' => 'none',
],
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'single_trait_insert_per_statement' => true,
'ternary_operator_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
->in([__DIR__.'/src/'])
)
;
;
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"illuminate/container": "^5.1 || ^6.0 || ^7.0 || ^8.0",
"overtrue/wechat": "^4.0 || ^5.0"
},
"require-dev": {
"laravel/framework": "^8.5",
"friendsofphp/php-cs-fixer": "^2.16"
},
"autoload": {
"psr-4": {
"Overtrue\\LaravelWeChat\\": "src/"
Expand Down
59 changes: 32 additions & 27 deletions src/Middleware/OAuthAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Closure;
use http\Env\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Overtrue\LaravelWeChat\Events\WeChatUserAuthorized;

Expand All @@ -25,54 +26,58 @@ class OAuthAuthenticate
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $scope
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $account
* @param string|null $scope
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
*
* @return mixed
*/
public function handle($request, Closure $next, $account = 'default', $scope = null, $type = 'service')
{
$isNewSession = false;
//保证兼容性
$class = ('work' !== $type) ? 'wechat' : 'work';
$prefix = ('work' !== $type) ? 'official_account' : 'work';
$sessionKey = \sprintf($class . '.oauth_user.%s', $account);
$config = config(\sprintf('wechat.' . $prefix . '.%s', $account), []);
$officialAccount = app(\sprintf('wechat.' . $prefix . '.%s', $account));
$sessionKey = \sprintf('%s.oauth_user.%s', $class, $account);
$service = \sprintf('wechat.%s.%s', $prefix, $account);
$config = config($service, []);
$officialAccount = app($service);

$scope = $scope ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);

if (is_string($scope)) {
$scope = array_map('trim', explode(',', $scope));
}

$session = session($sessionKey, []);

if (!$session) {
// 是否强制使用 HTTPS 跳转
$enforceHttps = Arr::get($config, 'oauth.enforce_https', false);

if ($request->has('code')) {
session([$sessionKey => $officialAccount->oauth->user() ?? []]);
$isNewSession = true;
if (Session::has($sessionKey)) {
event(new WeChatUserAuthorized(session($sessionKey), false, $account));
return $next($request);
}

event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));
// 是否强制使用 HTTPS 跳转
$enforceHttps = Arr::get($config, 'oauth.enforce_https', false);

return redirect()->to($this->getTargetUrl($request, $enforceHttps));
if ($request->has('code')) {
if (\is_callable($officialAccount->oauth, 'user')) {

This comment has been minimized.

Copy link
@bolechen

bolechen Sep 23, 2020

是否应该是

if (\is_callable([$officialAccount->oauth, 'user'])) {

This comment has been minimized.

Copy link
@bolechen

bolechen Sep 23, 2020

phpstan 检查出错

 98     Parameter #2 $syntax_only of function is_callable expects bool,
         string given.
  99     Cannot call method user() on callable.

This comment has been minimized.

Copy link
@overtrue

overtrue Sep 23, 2020

Author Owner

我擦,我让朋友测试的。。。他说没问题我就提交了,我这就去反省并批斗他!

$user = $officialAccount->oauth->user();
} else {
$user = $officialAccount->oauth->userFromCode($request->query('code'));
}

session()->forget($sessionKey);
session([$sessionKey => $user]);

event(new WeChatUserAuthorized(session($sessionKey), true, $account));

// 跳转到微信授权页
return redirect()->away(
$officialAccount->oauth->scopes($scope)
->redirect($this->getRedirectUrl($request, $enforceHttps))
);
return redirect()->to($this->getTargetUrl($request, $enforceHttps));
}

event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));
session()->forget($sessionKey);

return $next($request);
// 跳转到微信授权页
return redirect()->away(
$officialAccount->oauth->scopes($scope)->redirect($this->getRedirectUrl($request, $enforceHttps))
);
}

/**
Expand Down

0 comments on commit 8cb8383

Please sign in to comment.