1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EntWeChat\Foundation\ServiceProviders; |
4
|
|
|
|
5
|
|
|
use EntWeChat\Auth\App; |
6
|
|
|
use EntWeChat\Encryption\Encryptor; |
7
|
|
|
use EntWeChat\Foundation\Application; |
8
|
|
|
use EntWeChat\Suite\AccessToken; |
9
|
|
|
use EntWeChat\Suite\Api\BaseApi; |
10
|
|
|
use EntWeChat\Suite\Api\PreAuthorization; |
11
|
|
|
use EntWeChat\Suite\Authorization; |
12
|
|
|
use EntWeChat\Suite\AuthorizerAccessToken; |
13
|
|
|
use EntWeChat\Suite\EventHandlers; |
14
|
|
|
use EntWeChat\Suite\Guard; |
15
|
|
|
use EntWeChat\Suite\Suite; |
16
|
|
|
use EntWeChat\Suite\Ticket; |
17
|
|
|
use Pimple\Container; |
18
|
|
|
use Pimple\ServiceProviderInterface; |
19
|
|
|
|
20
|
|
|
class SuiteServiceProvider implements ServiceProviderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Registers services on the given container. |
24
|
|
|
* |
25
|
|
|
* This method should only be used to configure services and parameters. |
26
|
|
|
* It should not get services. |
27
|
|
|
* |
28
|
|
|
* @param Container $pimple A container instance |
29
|
|
|
*/ |
30
|
|
|
public function register(Container $pimple) |
31
|
|
|
{ |
32
|
|
|
$pimple['suite.ticket'] = function ($pimple) { |
33
|
|
|
return new Ticket( |
34
|
|
|
$pimple['config']['suite']['suite_id'], |
35
|
|
|
$pimple['cache'] |
36
|
|
|
); |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
$pimple['suite.access_token'] = function ($pimple) { |
40
|
|
|
$accessToken = new AccessToken( |
41
|
|
|
$pimple['config']['suite']['suite_id'], |
42
|
|
|
$pimple['config']['suite']['secret'], |
43
|
|
|
$pimple['cache'] |
44
|
|
|
); |
45
|
|
|
$accessToken->setTicket($pimple['suite.ticket']); |
46
|
|
|
|
47
|
|
|
return $accessToken; |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
$pimple['suite.encryptor'] = function ($pimple) { |
51
|
|
|
return new Encryptor( |
52
|
|
|
$pimple['config']['suite']['suite_id'], |
53
|
|
|
$pimple['config']['suite']['token'], |
54
|
|
|
$pimple['config']['suite']['aes_key'] |
55
|
|
|
); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$pimple['suite'] = function ($pimple) { |
59
|
|
|
return new Suite($pimple); |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
$pimple['suite.server'] = function ($pimple) { |
63
|
|
|
$server = new Guard($pimple['config']['suite']['token']); |
|
|
|
|
64
|
|
|
$server->debug($pimple['config']['debug']); |
65
|
|
|
$server->setEncryptor($pimple['suite.encryptor']); |
66
|
|
|
$server->setHandlers([ |
67
|
|
|
Guard::EVENT_CREATE_AUTH => $pimple['suite.handlers.create_auth'], |
68
|
|
|
Guard::EVENT_CANCEL_AUTH => $pimple['suite.handlers.cancel_auth'], |
69
|
|
|
Guard::EVENT_CHANGE_AUTH => $pimple['suite.handlers.change_auth'], |
70
|
|
|
Guard::EVENT_SUITE_TICKET => $pimple['suite.handlers.suite_ticket'], |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
return $server; |
74
|
|
|
}; |
75
|
|
|
|
76
|
|
|
$pimple['suite.pre_auth'] = $pimple['suite.pre_authorization'] = function ($pimple) { |
77
|
|
|
return new PreAuthorization( |
78
|
|
|
$pimple['suite.access_token'], |
79
|
|
|
$pimple['request'] |
80
|
|
|
); |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
$pimple['suite.api'] = function ($pimple) { |
84
|
|
|
return new BaseApi( |
85
|
|
|
$pimple['suite.access_token'], |
86
|
|
|
$pimple['request'] |
87
|
|
|
); |
88
|
|
|
}; |
89
|
|
|
|
90
|
|
|
$pimple['suite.authorization'] = function ($pimple) { |
91
|
|
|
return new Authorization( |
92
|
|
|
$pimple['suite.api'], |
93
|
|
|
$pimple['config']['suite']['corp_id'], |
94
|
|
|
$pimple['cache'] |
95
|
|
|
); |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
$pimple['suite.authorizer_access_token'] = function ($pimple) { |
99
|
|
|
return new AuthorizerAccessToken( |
100
|
|
|
$pimple['config']['suite']['corp_id'], |
101
|
|
|
$pimple['suite.authorization'] |
102
|
|
|
); |
103
|
|
|
}; |
104
|
|
|
|
105
|
|
|
// Authorization events handlers. |
106
|
|
|
$pimple['suite.handlers.suite_ticket'] = function ($pimple) { |
107
|
|
|
return new EventHandlers\SuiteTicket($pimple['suite.ticket']); |
108
|
|
|
}; |
109
|
|
|
$pimple['suite.handlers.create_auth'] = function () { |
110
|
|
|
return new EventHandlers\CreateAuth(); |
111
|
|
|
}; |
112
|
|
|
$pimple['suite.handlers.change_auth'] = function () { |
113
|
|
|
return new EventHandlers\ChangeAuth(); |
114
|
|
|
}; |
115
|
|
|
$pimple['suite.handlers.cancel_auth'] = function () { |
116
|
|
|
return new EventHandlers\CancelAuth(); |
117
|
|
|
}; |
118
|
|
|
|
119
|
|
|
$pimple['suite.app'] = function ($pimple) { |
120
|
|
|
return new Application($pimple['config']->toArray()); |
121
|
|
|
}; |
122
|
|
|
|
123
|
|
|
// OAuth for Suite. |
124
|
|
|
$pimple['suite.oauth'] = function ($pimple) { |
125
|
|
|
return new App($pimple['suite.authorizer_access_token']->getToken()); |
126
|
|
|
}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Prepare the OAuth callback url for wechat. |
131
|
|
|
* |
132
|
|
|
* @param Container $pimple |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
private function prepareCallbackUrl($pimple) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$callback = $pimple['config']->get('oauth.callback'); |
139
|
|
|
if (0 === stripos($callback, 'http')) { |
140
|
|
|
return $callback; |
141
|
|
|
} |
142
|
|
|
$baseUrl = $pimple['request']->getSchemeAndHttpHost(); |
143
|
|
|
|
144
|
|
|
return $baseUrl.'/'.ltrim($callback, '/'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for function calls that miss required arguments.