-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuthenticator.php
137 lines (119 loc) · 3.19 KB
/
Authenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* This file is part of the Netrium Framework
*
* Copyright (c) 2015 Martin Sadovy (http://sodae.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/
namespace Netrium\Addons\Twitter;
use Nette;
use Nette\Http\Request;
use Nette\Http\Response;
/**
* Twitter web authenticator
*
* @author Martin Sadovy
*/
class Authenticator
{
/**
* @var IStorage
*/
private $storage;
/**
* @var \TwitterOAuth
*/
private $twitterOAuth;
/**
* @var Request
*/
private $httpRequest;
/**
* @var Response
*/
private $httpResponse;
public function __construct(\TwitterOAuth $twitterOAuth, IStorage $storage, Request $httpRequest, Response $httpResponse)
{
$this->storage = $storage;
$this->twitterOAuth = clone $twitterOAuth;
$this->httpRequest = $httpRequest;
$this->httpResponse = $httpResponse;
}
/**
* Try authenticate to twitter account
* @return FALSE|array
* @throws AuthenticationException
*/
public function tryAuthenticate()
{
try {
if (!$this->storage->isAuthorized()) {
$this->authorize();
} else {
$accessToken = $this->verify();
if ($user = $this->twitterOAuth->get('account/verify_credentials')) {
$this->resetAuthorize();
return array(
'user' => $user,
'accessToken' => array(
'key' => $accessToken['oauth_token'],
'secret' => $accessToken['oauth_token_secret'],
));
} else {
throw new AuthenticationException("Could not authenticate you.");
}
}
} catch (AuthenticationException $e) {
$this->resetAuthorize();
throw $e;
}
}
/**
* First step in oAuth authorization
* @throws AuthenticationException
* @throws Nette\Application\AbortException
*/
private function authorize()
{
$this->resetAuthorize();
$requestToken = $this->twitterOAuth->getRequestToken($this->httpRequest->url->getAbsoluteUrl());
$this->storage->setOAuthTokens($requestToken['oauth_token'], $requestToken['oauth_token_secret']);
if ($this->twitterOAuth->http_code === 200) {
$this->storage->setAuthorized();
$url = $this->twitterOAuth->getAuthorizeURL($this->storage->getOAuthTokenKey());
$this->httpResponse->redirect($url);
throw new Nette\Application\AbortException; // stop!
} else {
throw new AuthenticationException("Could not connect to Twitter. Refresh the page or try again later.");
}
}
/**
* Second step in authorization
* @return boolean
* @throws AuthenticationException
*/
private function verify()
{
$this->twitterOAuth->setOAuthToken($this->storage->getOAuthTokenKey(), $this->storage->getOAuthTokenSecret());
$verifier = $this->httpRequest->getQuery('oauth_verifier');
if (!$verifier) {
throw new AuthenticationException("Missing request parametr 'oauth_verifier'");
}
$accessToken = $this->twitterOAuth->getAccessToken($verifier);
if ($this->twitterOAuth->http_code === 200) {
return $accessToken;
} else {
throw new AuthenticationException("Could not authenticate you.");
}
}
/**
* Restart storage and oAuth-er for reauthorization
*/
public function resetAuthorize()
{
$this->twitterOAuth->cleanOAuthToken();
$this->storage->clean();
}
}