Skip to content

Commit a32abe9

Browse files
lonquan龙权
andauthored
fix: 修复在 Laravel 7.28 版本直接返回字符串链接造成的 StartSession::addCookieToResponse 参数错误; 增加授权链接跳转时强制使用 HTTPS 配置 (overtrue#395)
Co-authored-by: 龙权 <[email protected]>
1 parent 62d5677 commit a32abe9

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

src/Middleware/OAuthAuthenticate.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ class OAuthAuthenticate
2424
/**
2525
* Handle an incoming request.
2626
*
27-
* @param \Illuminate\Http\Request $request
28-
* @param \Closure $next
29-
* @param string|null $scope
30-
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
31-
*
27+
* @param \Illuminate\Http\Request $request
28+
* @param \Closure $next
29+
* @param string|null $scope
30+
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
3231
* @return mixed
3332
*/
3433
public function handle($request, Closure $next, $account = 'default', $scope = null, $type = 'service')
@@ -37,9 +36,9 @@ public function handle($request, Closure $next, $account = 'default', $scope = n
3736
//保证兼容性
3837
$class = ('work' !== $type) ? 'wechat' : 'work';
3938
$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));
39+
$sessionKey = \sprintf($class . '.oauth_user.%s', $account);
40+
$config = config(\sprintf('wechat.' . $prefix . '.%s', $account), []);
41+
$officialAccount = app(\sprintf('wechat.' . $prefix . '.%s', $account));
4342
$scope = $scope ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);
4443

4544
if (is_string($scope)) {
@@ -49,18 +48,25 @@ public function handle($request, Closure $next, $account = 'default', $scope = n
4948
$session = session($sessionKey, []);
5049

5150
if (!$session) {
51+
// 是否强制使用 HTTPS 跳转
52+
$enforceHttps = Arr::get($config, 'oauth.enforce_https', false);
53+
5254
if ($request->has('code')) {
5355
session([$sessionKey => $officialAccount->oauth->user() ?? []]);
5456
$isNewSession = true;
5557

5658
event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));
5759

58-
return redirect()->to($this->getTargetUrl($request));
60+
return redirect()->to($this->getTargetUrl($request, $enforceHttps));
5961
}
6062

6163
session()->forget($sessionKey);
6264

63-
return $officialAccount->oauth->scopes($scope)->redirect($request->fullUrl());
65+
// 跳转到微信授权页
66+
return redirect()->away(
67+
$officialAccount->oauth->scopes($scope)
68+
->redirect($this->getRedirectUrl($request, $enforceHttps))
69+
);
6470
}
6571

6672
event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));
@@ -71,14 +77,37 @@ public function handle($request, Closure $next, $account = 'default', $scope = n
7177
/**
7278
* Build the target business url.
7379
*
74-
* @param Request $request
75-
*
80+
* @param \Illuminate\Http\Request $request
81+
* @param bool $https
7682
* @return string
7783
*/
78-
protected function getTargetUrl($request)
84+
protected function getTargetUrl($request, $https = false)
7985
{
8086
$queries = Arr::except($request->query(), ['code', 'state']);
87+
$url = $request->url();
88+
89+
if ($https && Str::startsWith($url, 'http://')) {
90+
$url = Str::replaceFirst('http', 'https', $url);
91+
}
92+
93+
return $url . (empty($queries) ? '' : '?' . http_build_query($queries));
94+
}
95+
96+
/**
97+
* generate the redirect url
98+
*
99+
* @param \Illuminate\Http\Request $request
100+
* @param bool $https
101+
* @return string
102+
*/
103+
protected function getRedirectUrl($request, $https = false)
104+
{
105+
if (!$https) {
106+
return $request->fullUrl();
107+
}
81108

82-
return $request->url().(empty($queries) ? '' : '?'.http_build_query($queries));
109+
return Str::startsWith($request->fullUrl(), 'http://')
110+
? Str::replaceFirst('http', 'https', $request->fullUrl())
111+
: $request->fullUrl();
83112
}
84113
}

src/config.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
/*
1414
* 默认配置,将会合并到各模块中
1515
*/
16-
'defaults' => [
16+
'defaults' => [
1717
/*
1818
* 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
1919
*/
20-
'response_type' => 'array',
20+
'response_type' => 'array',
2121

2222
/*
2323
* 使用 Laravel 的缓存系统
@@ -31,16 +31,16 @@
3131
* debug/info/notice/warning/error/critical/alert/emergency
3232
* file:日志文件位置(绝对路径!!!),要求可写权限
3333
*/
34-
'log' => [
34+
'log' => [
3535
'level' => env('WECHAT_LOG_LEVEL', 'debug'),
36-
'file' => env('WECHAT_LOG_FILE', storage_path('logs/wechat.log')),
36+
'file' => env('WECHAT_LOG_FILE', storage_path('logs/wechat.log')),
3737
],
3838
],
3939

4040
/*
4141
* 路由配置
4242
*/
43-
'route' => [
43+
'route' => [
4444
/*
4545
* 开放平台第三方平台路由配置
4646
*/
@@ -59,20 +59,22 @@
5959
*/
6060
'official_account' => [
6161
'default' => [
62-
'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID', 'your-app-id'), // AppID
63-
'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET', 'your-app-secret'), // AppSecret
64-
'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN', 'your-token'), // Token
62+
'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID', 'your-app-id'), // AppID
63+
'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET', 'your-app-secret'), // AppSecret
64+
'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN', 'your-token'), // Token
6565
'aes_key' => env('WECHAT_OFFICIAL_ACCOUNT_AES_KEY', ''), // EncodingAESKey
6666

6767
/*
6868
* OAuth 配置
6969
*
7070
* scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
7171
* callback:OAuth授权完成后的回调页地址(如果使用中间件,则随便填写。。。)
72+
* enforce_https:是否强制使用 HTTPS 跳转
7273
*/
73-
// 'oauth' => [
74-
// 'scopes' => array_map('trim', explode(',', env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES', 'snsapi_userinfo'))),
75-
// 'callback' => env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK', '/examples/oauth_callback.php'),
74+
// 'oauth' => [
75+
// 'scopes' => array_map('trim', explode(',', env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES', 'snsapi_userinfo'))),
76+
// 'callback' => env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK', '/examples/oauth_callback.php'),
77+
// 'enforce_https' => true,
7678
// ],
7779
],
7880
],

0 commit comments

Comments
 (0)