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
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function oauthForAccessTokenAsync($oauth_verifier) |
27
|
1 |
|
{ |
28
|
1 |
|
$obj = (yield ResponseYielder::asyncExecDecoded($this->getInternalCurl()->oauthForAccessToken($oauth_verifier))); |
29
|
1 |
|
yield CoInterface::RETURN_WITH => $this->withCredentials([ |
30
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
31
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
32
|
1 |
|
$obj->oauth_token, |
33
|
1 |
|
$obj->oauth_token_secret, |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function xauthForAccessTokenAsync($username, $password) |
38
|
1 |
|
{ |
39
|
1 |
|
$obj = (yield ResponseYielder::asyncExecDecoded($this->getInternalCurl()->xauthForAccessToken($username, $password))); |
40
|
1 |
|
yield CoInterface::RETURN_WITH => $this->withCredentials([ |
41
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
42
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
43
|
1 |
|
$obj->oauth_token, |
44
|
1 |
|
$obj->oauth_token_secret, |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function oauthForRequestToken($oauth_callback = null) |
49
|
1 |
|
{ |
50
|
1 |
|
$obj = ResponseYielder::syncExecDecoded($this->getInternalCurl()->oauthForRequestToken($oauth_callback)); |
51
|
1 |
|
return $this->withCredentials([ |
52
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
53
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
54
|
1 |
|
$obj->oauth_token, |
55
|
1 |
|
$obj->oauth_token_secret, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function oauthForAccessToken($oauth_verifier) |
60
|
1 |
|
{ |
61
|
1 |
|
$obj = ResponseYielder::syncExecDecoded($this->getInternalCurl()->oauthForAccessToken($oauth_verifier)); |
62
|
1 |
|
return $this->withCredentials([ |
63
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
64
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
65
|
1 |
|
$obj->oauth_token, |
66
|
1 |
|
$obj->oauth_token_secret, |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function xauthForAccessToken($username, $password) |
71
|
1 |
|
{ |
72
|
1 |
|
$obj = ResponseYielder::syncExecDecoded($this->getInternalCurl()->xauthForAccessToken($username, $password)); |
73
|
1 |
|
return $this->withCredentials([ |
74
|
1 |
|
$this->getInternalCredential()['consumer_key'], |
75
|
1 |
|
$this->getInternalCredential()['consumer_secret'], |
76
|
1 |
|
$obj->oauth_token, |
77
|
1 |
|
$obj->oauth_token_secret, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function loginAsync($username, $password) |
82
|
|
|
{ |
83
|
|
|
$author = (yield $this->oauthForRequestTokenAsync('oob')); |
84
|
|
|
$scraper = $this->getInternalCurl()->browsing(); |
85
|
|
|
curl_setopt_array($scraper, [ |
86
|
|
|
CURLOPT_HTTPGET => true, |
87
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
88
|
|
|
]); |
89
|
|
|
$authenticity_token = RegexParser::parseAuthenticityToken((yield ResponseYielder::asyncExec($scraper)), $scraper); |
90
|
|
|
curl_setopt_array($scraper, [ |
91
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
92
|
|
|
CURLOPT_POST => true, |
93
|
|
|
CURLOPT_POSTFIELDS => http_build_query([ |
94
|
|
|
'session[username_or_email]' => $username, |
95
|
|
|
'session[password]' => $password, |
96
|
|
|
'authenticity_token' => $authenticity_token, |
97
|
|
|
], '', '&'), |
98
|
|
|
]); |
99
|
|
|
$verifier = RegexParser::parseVerifier((yield ResponseYielder::asyncExec($scraper)), $scraper); |
100
|
|
|
yield CoInterface::RETURN_WITH => $author->oauthForAccessTokenAsync($verifier); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function login($username, $password) |
104
|
|
|
{ |
105
|
|
|
$author = $this->oauthForRequestToken('oob'); |
106
|
|
|
$scraper = $this->getInternalCurl()->browsing(); |
107
|
|
|
curl_setopt_array($scraper, [ |
108
|
|
|
CURLOPT_HTTPGET => true, |
109
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
110
|
|
|
]); |
111
|
|
|
$authenticity_token = RegexParser::parseAuthenticityToken(ResponseYielder::syncExec($scraper), $scraper); |
112
|
|
|
curl_setopt_array($scraper, [ |
113
|
|
|
CURLOPT_URL => $author->getAuthorizeUrl(true), |
114
|
|
|
CURLOPT_POST => true, |
115
|
|
|
CURLOPT_POSTFIELDS => http_build_query([ |
116
|
|
|
'session[username_or_email]' => $username, |
117
|
|
|
'session[password]' => $password, |
118
|
|
|
'authenticity_token' => $authenticity_token, |
119
|
|
|
], '', '&'), |
120
|
|
|
]); |
121
|
|
|
$verifier = RegexParser::parseVerifier(ResponseYielder::syncExec($scraper), $scraper); |
122
|
|
|
return $author->oauthForAccessToken($verifier); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|