|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* OAuthServiceProvider.php. |
|
14
|
|
|
* |
|
15
|
|
|
* This file is part of the wechat. |
|
16
|
|
|
* |
|
17
|
|
|
* (c) overtrue <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* This source file is subject to the MIT license that is bundled |
|
20
|
|
|
* with this source code in the file LICENSE. |
|
21
|
|
|
*/ |
|
22
|
|
|
namespace EasyWeChat\Foundation\ServiceProviders; |
|
23
|
|
|
|
|
24
|
|
|
use Overtrue\Socialite\SocialiteManager as Socialite; |
|
25
|
|
|
use Pimple\Container; |
|
26
|
|
|
use Pimple\ServiceProviderInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class OAuthServiceProvider. |
|
30
|
|
|
*/ |
|
31
|
|
|
class OAuthServiceProvider implements ServiceProviderInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Registers services on the given container. |
|
35
|
|
|
* |
|
36
|
|
|
* This method should only be used to configure services and parameters. |
|
37
|
|
|
* It should not get services. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Container $pimple A container instance |
|
40
|
|
|
*/ |
|
41
|
|
|
public function register(Container $pimple) |
|
42
|
|
|
{ |
|
43
|
|
|
$pimple['oauth'] = function ($pimple) { |
|
44
|
|
|
$callback = $this->prepareCallbackUrl($pimple); |
|
45
|
|
|
$scopes = $pimple['config']->get('oauth.scopes', []); |
|
46
|
|
|
$socialite = (new Socialite( |
|
47
|
|
|
[ |
|
48
|
|
|
'wechat' => [ |
|
49
|
|
|
'client_id' => $pimple['config']['app_id'], |
|
50
|
|
|
'client_secret' => $pimple['config']['secret'], |
|
51
|
|
|
'redirect' => $callback, |
|
52
|
|
|
], |
|
53
|
|
|
] |
|
54
|
|
|
))->driver('wechat'); |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($scopes)) { |
|
57
|
|
|
$socialite->scopes($scopes); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $socialite; |
|
61
|
|
|
}; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Prepare the OAuth callback url for wechat. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Container $pimple |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
private function prepareCallbackUrl($pimple) |
|
72
|
|
|
{ |
|
73
|
|
|
$callback = $pimple['config']->get('oauth.callback'); |
|
74
|
|
|
$baseUrl = $pimple['request']->getSchemeAndHttpHost(); |
|
75
|
|
|
$callback = stripos($callback, 'http') === 0 ? $callback : $baseUrl.'/'.ltrim($callback, '/'); |
|
76
|
|
|
|
|
77
|
|
|
return $callback; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|