1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Services\Trello; |
4
|
|
|
|
5
|
|
|
use League\OAuth1\Client\Credentials\TemporaryCredentials; |
6
|
|
|
use League\OAuth1\Client\Server\Trello as OAuthServer; |
7
|
|
|
|
8
|
|
|
class Authorization |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* OAuth client |
12
|
|
|
* |
13
|
|
|
* @var \League\OAuth1\Client\Server\Trello |
14
|
|
|
*/ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Creates new authorization broker. |
19
|
12 |
|
*/ |
20
|
|
|
public function __construct() |
21
|
12 |
|
{ |
22
|
12 |
|
$this->createClient(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates a new OAuth server client and attaches to authorization broker. |
27
|
|
|
* |
28
|
|
|
* @return Authorization |
29
|
12 |
|
*/ |
30
|
|
|
protected function createClient() |
31
|
12 |
|
{ |
32
|
12 |
|
$this->client = new OAuthServer([ |
33
|
12 |
|
'identifier' => Configuration::get('key'), |
34
|
12 |
|
'secret' => Configuration::get('secret'), |
35
|
12 |
|
'callback_uri' => Configuration::get('callbackUrl'), |
36
|
12 |
|
'name' => Configuration::get('name'), |
37
|
12 |
|
'expiration' => Configuration::get('expiration'), |
38
|
6 |
|
'scope' => Configuration::get('scope'), |
39
|
|
|
]); |
40
|
12 |
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Authorize application |
46
|
|
|
* |
47
|
|
|
* First part of OAuth 1.0 authentication is retrieving temporary |
48
|
|
|
* credentials. These identify you as a client to the server. Store the |
49
|
|
|
* credentials in the session. Return authorization url. |
50
|
|
|
* |
51
|
|
|
* @param TemporaryCredentials $temporaryCredentials |
52
|
|
|
* |
53
|
|
|
* @return string Authorization url |
54
|
4 |
|
*/ |
55
|
|
|
public function getAuthorizationUrl(TemporaryCredentials $temporaryCredentials = null) |
56
|
4 |
|
{ |
57
|
2 |
|
if (is_null($temporaryCredentials)) { |
58
|
2 |
|
$sessionKey = self::getCredentialSessionKey(); |
59
|
2 |
|
$temporaryCredentials = $this->getTemporaryCredentials(); |
60
|
2 |
|
$_SESSION[$sessionKey] = serialize($temporaryCredentials); |
61
|
1 |
|
session_write_close(); |
62
|
|
|
} |
63
|
4 |
|
|
64
|
|
|
return $this->client->getAuthorizationUrl($temporaryCredentials); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the key for temporary credentials stored in session |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
4 |
|
*/ |
72
|
|
|
private static function getCredentialSessionKey() |
73
|
4 |
|
{ |
74
|
|
|
return get_class().':temporary_credentials'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates and returns new temporary credentials instance. |
79
|
|
|
* |
80
|
|
|
* @return TemporaryCredentials |
81
|
4 |
|
*/ |
82
|
|
|
public function getTemporaryCredentials() |
83
|
4 |
|
{ |
84
|
|
|
return $this->client->getTemporaryCredentials(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Verify and fetch token |
89
|
|
|
* |
90
|
|
|
* Retrieve the temporary credentials from step 2. Third and final part to |
91
|
|
|
* OAuth 1.0 authentication is to retrieve token credentials (formally |
92
|
|
|
* known as access tokens in earlier OAuth 1.0 specs). Now, we'll store |
93
|
|
|
* the token credentials and discard the temporary ones - they're |
94
|
|
|
* irrelevant at this stage. |
95
|
|
|
* |
96
|
|
|
* @param string $oauthToken |
97
|
|
|
* @param string $oauthVerifier |
98
|
|
|
* @param TemporaryCredentials $temporaryCredentials |
99
|
|
|
* |
100
|
|
|
* @return \League\OAuth1\Client\Credentials\CredentialsInterface |
101
|
4 |
|
*/ |
102
|
|
|
public function getToken($oauthToken, $oauthVerifier, TemporaryCredentials $temporaryCredentials = null) |
103
|
4 |
|
{ |
104
|
2 |
|
if (is_null($temporaryCredentials)) { |
105
|
2 |
|
$sessionKey = self::getCredentialSessionKey(); |
106
|
2 |
|
$temporaryCredentials = unserialize($_SESSION[$sessionKey]); |
107
|
2 |
|
unset($_SESSION[$sessionKey]); |
108
|
1 |
|
session_write_close(); |
109
|
|
|
} |
110
|
4 |
|
|
111
|
4 |
|
return $this->client->getTokenCredentials( |
112
|
4 |
|
$temporaryCredentials, |
113
|
2 |
|
$oauthToken, |
114
|
2 |
|
$oauthVerifier |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Updates the OAuth client. |
120
|
|
|
* |
121
|
|
|
* @param \League\OAuth1\Client\Server\Trello $client |
122
|
|
|
* |
123
|
|
|
* @return Authorization |
124
|
10 |
|
*/ |
125
|
|
|
public function setClient(OAuthServer $client) |
126
|
10 |
|
{ |
127
|
|
|
$this->client = $client; |
128
|
10 |
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|