Skip to content

Commit 8cb8383

Browse files
committed
socialite 3.0
1 parent ab19b30 commit 8cb8383

File tree

3 files changed

+78
-50
lines changed

3 files changed

+78
-50
lines changed

.php_cs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
1-
<?php
2-
$header = <<<EOF
3-
This file is part of the overtrue/laravel-wechat.
4-
5-
(c) overtrue <[email protected]>
6-
7-
This source file is subject to the MIT license that is bundled
8-
with this source code in the file LICENSE.
9-
EOF;
10-
111
return PhpCsFixer\Config::create()
12-
->setRiskyAllowed(true)
13-
->setRules(array(
14-
'@Symfony' => true,
15-
'header_comment' => array('header' => $header),
16-
'array_syntax' => array('syntax' => 'short'),
17-
'ordered_imports' => true,
18-
'no_useless_else' => true,
19-
'no_useless_return' => true,
20-
'php_unit_construct' => true,
21-
'php_unit_strict' => true,
22-
))
2+
->setRules([
3+
'@PSR2' => true,
4+
'binary_operator_spaces' => true,
5+
'blank_line_after_opening_tag' => true,
6+
'compact_nullable_typehint' => true,
7+
'declare_equal_normalize' => true,
8+
'lowercase_cast' => true,
9+
'lowercase_static_reference' => true,
10+
'new_with_braces' => true,
11+
'no_unused_imports' => true,
12+
'no_blank_lines_after_class_opening' => true,
13+
'no_leading_import_slash' => true,
14+
'no_whitespace_in_blank_line' => true,
15+
'ordered_class_elements' => [
16+
'order' => [
17+
'use_trait',
18+
],
19+
],
20+
'ordered_imports' => [
21+
'imports_order' => [
22+
'class',
23+
'function',
24+
'const',
25+
],
26+
'sort_algorithm' => 'none',
27+
],
28+
'return_type_declaration' => true,
29+
'short_scalar_cast' => true,
30+
'single_blank_line_before_namespace' => true,
31+
'single_trait_insert_per_statement' => true,
32+
'ternary_operator_spaces' => true,
33+
'unary_operator_spaces' => true,
34+
'visibility_required' => [
35+
'elements' => [
36+
'const',
37+
'method',
38+
'property',
39+
],
40+
],
41+
])
2342
->setFinder(
2443
PhpCsFixer\Finder::create()
2544
->exclude('vendor')
26-
->in(__DIR__)
45+
->in([__DIR__.'/src/'])
2746
)
28-
;
47+
;

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
"illuminate/container": "^5.1 || ^6.0 || ^7.0 || ^8.0",
1414
"overtrue/wechat": "^4.0 || ^5.0"
1515
},
16+
"require-dev": {
17+
"laravel/framework": "^8.5",
18+
"friendsofphp/php-cs-fixer": "^2.16"
19+
},
1620
"autoload": {
1721
"psr-4": {
1822
"Overtrue\\LaravelWeChat\\": "src/"

src/Middleware/OAuthAuthenticate.php

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Closure;
1515
use http\Env\Request;
1616
use Illuminate\Support\Arr;
17+
use Illuminate\Support\Facades\Session;
1718
use Illuminate\Support\Str;
1819
use Overtrue\LaravelWeChat\Events\WeChatUserAuthorized;
1920

@@ -25,54 +26,58 @@ class OAuthAuthenticate
2526
/**
2627
* Handle an incoming request.
2728
*
28-
* @param \Illuminate\Http\Request $request
29-
* @param \Closure $next
30-
* @param string|null $scope
31-
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
29+
* @param \Illuminate\Http\Request $request
30+
* @param \Closure $next
31+
* @param string $account
32+
* @param string|null $scope
33+
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
34+
*
3235
* @return mixed
3336
*/
3437
public function handle($request, Closure $next, $account = 'default', $scope = null, $type = 'service')
3538
{
36-
$isNewSession = false;
3739
//保证兼容性
3840
$class = ('work' !== $type) ? 'wechat' : 'work';
3941
$prefix = ('work' !== $type) ? 'official_account' : 'work';
40-
$sessionKey = \sprintf($class . '.oauth_user.%s', $account);
41-
$config = config(\sprintf('wechat.' . $prefix . '.%s', $account), []);
42-
$officialAccount = app(\sprintf('wechat.' . $prefix . '.%s', $account));
42+
$sessionKey = \sprintf('%s.oauth_user.%s', $class, $account);
43+
$service = \sprintf('wechat.%s.%s', $prefix, $account);
44+
$config = config($service, []);
45+
$officialAccount = app($service);
46+
4347
$scope = $scope ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);
4448

4549
if (is_string($scope)) {
4650
$scope = array_map('trim', explode(',', $scope));
4751
}
4852

49-
$session = session($sessionKey, []);
50-
51-
if (!$session) {
52-
// 是否强制使用 HTTPS 跳转
53-
$enforceHttps = Arr::get($config, 'oauth.enforce_https', false);
54-
55-
if ($request->has('code')) {
56-
session([$sessionKey => $officialAccount->oauth->user() ?? []]);
57-
$isNewSession = true;
53+
if (Session::has($sessionKey)) {
54+
event(new WeChatUserAuthorized(session($sessionKey), false, $account));
55+
return $next($request);
56+
}
5857

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

61-
return redirect()->to($this->getTargetUrl($request, $enforceHttps));
61+
if ($request->has('code')) {
62+
if (\is_callable($officialAccount->oauth, 'user')) {
63+
$user = $officialAccount->oauth->user();
64+
} else {
65+
$user = $officialAccount->oauth->userFromCode($request->query('code'));
6266
}
6367

64-
session()->forget($sessionKey);
68+
session([$sessionKey => $user]);
69+
70+
event(new WeChatUserAuthorized(session($sessionKey), true, $account));
6571

66-
// 跳转到微信授权页
67-
return redirect()->away(
68-
$officialAccount->oauth->scopes($scope)
69-
->redirect($this->getRedirectUrl($request, $enforceHttps))
70-
);
72+
return redirect()->to($this->getTargetUrl($request, $enforceHttps));
7173
}
7274

73-
event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));
75+
session()->forget($sessionKey);
7476

75-
return $next($request);
77+
// 跳转到微信授权页
78+
return redirect()->away(
79+
$officialAccount->oauth->scopes($scope)->redirect($this->getRedirectUrl($request, $enforceHttps))
80+
);
7681
}
7782

7883
/**

0 commit comments

Comments
 (0)