-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/']) | ||
) | ||
; | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bolechen
|
||
$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)) | ||
); | ||
} | ||
|
||
/** | ||
|
是否应该是