1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter\Traits; |
4
|
|
|
|
5
|
|
|
use mpyw\Cowitter\Helpers\ResponseYielder; |
6
|
|
|
use mpyw\Cowitter\Helpers\RegexParser; |
7
|
|
|
use mpyw\Co\CoInterface; |
8
|
|
|
|
9
|
|
|
trait AuthenticatorTrait |
10
|
|
|
{ |
11
|
|
|
abstract public function withCredentials(array $credentails); |
12
|
|
|
abstract protected function getInternalCurl(); |
13
|
|
|
abstract protected function getInternalCredential(); |
14
|
|
|
|
15
|
1 |
|
public function oauthForRequestTokenAsync($oauth_callback = null) |
16
|
1 |
|
{ |
17
|
1 |
|
$obj = (yield ResponseYielder::asyncExecDecoded($this->getInternalCurl()->oauthForRequestToken($oauth_callback))); |
18
|
1 |
|
yield CoInterface::RETURN_WITH => $this->withCredentials([ |
19
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
20
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
21
|
1 |
|
$obj->oauth_token, |
22
|
1 |
|
$obj->oauth_token_secret, |
23
|
|
|
]); |
24
|
|
|
// @codeCoverageIgnoreStart |
25
|
|
|
} |
26
|
|
|
// @codeCoverageIgnoreEnd |
27
|
|
|
|
28
|
1 |
|
public function oauthForAccessTokenAsync($oauth_verifier) |
29
|
1 |
|
{ |
30
|
1 |
|
$obj = (yield ResponseYielder::asyncExecDecoded($this->getInternalCurl()->oauthForAccessToken($oauth_verifier))); |
31
|
1 |
|
yield CoInterface::RETURN_WITH => $this->withCredentials([ |
32
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
33
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
34
|
1 |
|
$obj->oauth_token, |
35
|
1 |
|
$obj->oauth_token_secret, |
36
|
|
|
]); |
37
|
|
|
// @codeCoverageIgnoreStart |
38
|
|
|
} |
39
|
|
|
// @codeCoverageIgnoreEnds |
40
|
|
|
|
41
|
|
|
public function xauthForAccessTokenAsync($username, $password) |
42
|
|
|
{ |
43
|
|
|
$obj = (yield ResponseYielder::asyncExecDecoded($this->getInternalCurl()->xauthForAccessToken($username, $password))); |
44
|
|
|
yield CoInterface::RETURN_WITH => $this->withCredentials([ |
45
|
|
|
$this->getInternalCredential()['consumer_key'], |
46
|
|
|
$this->getInternalCredential()['consumer_secret'], |
47
|
|
|
$obj->oauth_token, |
48
|
|
|
$obj->oauth_token_secret, |
49
|
|
|
]); |
50
|
|
|
// @codeCoverageIgnoreStart |
51
|
|
|
} |
52
|
|
|
// @codeCoverageIgnoreEnd |
53
|
|
|
|
54
|
1 |
|
public function oauthForRequestToken($oauth_callback = null) |
55
|
1 |
|
{ |
56
|
1 |
|
$obj = ResponseYielder::syncExecDecoded($this->getInternalCurl()->oauthForRequestToken($oauth_callback)); |
57
|
1 |
|
return $this->withCredentials([ |
58
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
59
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
60
|
1 |
|
$obj->oauth_token, |
61
|
1 |
|
$obj->oauth_token_secret, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function oauthForAccessToken($oauth_verifier) |
66
|
1 |
|
{ |
67
|
1 |
|
$obj = ResponseYielder::syncExecDecoded($this->getInternalCurl()->oauthForAccessToken($oauth_verifier)); |
68
|
1 |
|
return $this->withCredentials([ |
69
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
70
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
71
|
1 |
|
$obj->oauth_token, |
72
|
1 |
|
$obj->oauth_token_secret, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function xauthForAccessToken($username, $password) |
77
|
1 |
|
{ |
78
|
1 |
|
$obj = ResponseYielder::syncExecDecoded($this->getInternalCurl()->xauthForAccessToken($username, $password)); |
79
|
1 |
|
return $this->withCredentials([ |
80
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
81
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
82
|
1 |
|
$obj->oauth_token, |
83
|
1 |
|
$obj->oauth_token_secret, |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* This method is untestable because of libcurl issue |
89
|
|
|
* @codeCoverageIgnore |
90
|
|
|
*/ |
91
|
|
|
public function loginAsync($username, $password) |
92
|
|
|
{ |
93
|
|
|
$author = (yield $this->oauthForRequestTokenAsync('oob')); |
94
|
|
|
$scraper = $this->getInternalCurl()->browsing(); |
95
|
|
|
curl_setopt_array($scraper, [ |
96
|
|
|
CURLOPT_HTTPGET => true, |
97
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
98
|
|
|
]); |
99
|
|
|
$authenticity_token = RegexParser::parseAuthenticityToken((yield ResponseYielder::asyncExec($scraper)), $scraper); |
100
|
|
|
curl_setopt_array($scraper, [ |
101
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
102
|
|
|
CURLOPT_POST => true, |
103
|
|
|
CURLOPT_POSTFIELDS => http_build_query([ |
104
|
|
|
'session[username_or_email]' => $username, |
105
|
|
|
'session[password]' => $password, |
106
|
|
|
'authenticity_token' => $authenticity_token, |
107
|
|
|
], '', '&'), |
108
|
|
|
]); |
109
|
|
|
$verifier = RegexParser::parseVerifier((yield ResponseYielder::asyncExec($scraper)), $scraper); |
110
|
|
|
yield CoInterface::RETURN_WITH => $author->oauthForAccessTokenAsync($verifier); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* This method is untestable because of libcurl issue |
115
|
|
|
* @codeCoverageIgnore |
116
|
|
|
*/ |
117
|
|
|
public function login($username, $password) |
118
|
|
|
{ |
119
|
|
|
$author = $this->oauthForRequestToken('oob'); |
120
|
|
|
$scraper = $this->getInternalCurl()->browsing(); |
121
|
|
|
curl_setopt_array($scraper, [ |
122
|
|
|
CURLOPT_HTTPGET => true, |
123
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
124
|
|
|
]); |
125
|
|
|
$authenticity_token = RegexParser::parseAuthenticityToken(ResponseYielder::syncExec($scraper), $scraper); |
126
|
|
|
curl_setopt_array($scraper, [ |
127
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
128
|
|
|
CURLOPT_POST => true, |
129
|
|
|
CURLOPT_POSTFIELDS => http_build_query([ |
130
|
|
|
'session[username_or_email]' => $username, |
131
|
|
|
'session[password]' => $password, |
132
|
|
|
'authenticity_token' => $authenticity_token, |
133
|
|
|
], '', '&'), |
134
|
|
|
]); |
135
|
|
|
$verifier = RegexParser::parseVerifier(ResponseYielder::syncExec($scraper), $scraper); |
136
|
|
|
return $author->oauthForAccessToken($verifier); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|