forked from mintbridge/kohana-oauth
/
provider.php
203 lines (180 loc) · 5.31 KB
/
provider.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* OAuth Provider
*
* @package Kohana/OAuth
* @category Provider
* @author Kohana Team
* @copyright (c) 2010 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class OAuth_Provider {
/**
* Create a new provider.
*
* // Load the Twitter provider
* $provider = OAuth_Provider::factory('twitter');
*
* @param string provider name
* @param array provider options
* @return OAuth_Provider
*/
public static function factory($name, array $options = NULL)
{
$class = __CLASS__.'_'.$name;
return new $class($options);
}
/**
* @var array additional request parameters to be used for remote requests
*/
protected $params = array();
/**
* Overloads default class properties from the options.
*
* Any of the provider options can be set here:
*
* Type | Option | Description | Default Value
* ----------|---------------|------------------------------------------------|-----------------
* mixed | signature | Signature method name or object | provider default
*
* @param array provider options
* @return void
*/
public function __construct(array $options = NULL)
{
if (isset($options['signature']))
{
// Set the signature method name or object
$this->signature = $options['signature'];
}
if ( ! is_object($this->signature))
{
// Convert the signature name into an object
$this->signature = OAuth_Signature::factory($this->signature);
}
}
/**
* Return the value of any protected class variable.
*
* // Get the provider signature
* $signature = $provider->signature;
*
* @param string variable name
* @return mixed
*/
public function __get($key)
{
return $this->$key;
}
/**
* Returns the request token URL for the provider.
*
* $url = $provider->url_request_token();
*
* @return string
*/
abstract public function url_request_token();
/**
* Returns the authorization URL for the provider.
*
* $url = $provider->url_authorize();
*
* @return string
*/
abstract public function url_authorize();
/**
* Returns the access token endpoint for the provider.
*
* $url = $provider->url_access_token();
*
* @return string
*/
abstract public function url_access_token();
/**
* Ask for a request token from the OAuth provider.
*
* $token = $provider->request_token($consumer);
*
* @param OAuth_Consumer consumer
* @param array additional request parameters
* @return OAuth_Token_Request
* @uses OAuth_Request_Token
*/
public function request_token(OAuth_Consumer $consumer, array $params = NULL)
{
// Create a new GET request for a request token with the required parameters
$request = OAuth_Request::factory('token', 'GET', $this->url_request_token(), array(
'oauth_consumer_key' => $consumer->key,
'oauth_callback' => $consumer->callback,
));
if ($params)
{
// Load user parameters
$request->params($params);
}
// Sign the request using only the consumer, no token is available yet
$request->sign($this->signature, $consumer);
// Create a response from the request
$response = $request->execute();
// Store this token somewhere useful
return OAuth_Token::factory('request', array(
'token' => $response->param('oauth_token'),
'secret' => $response->param('oauth_token_secret'),
));
}
/**
* Get the authorization URL for the request token.
*
* $this->request->redirect($provider->authorize_url($token));
*
* @param OAuth_Token_Request token
* @param array additional request parameters
* @return string
*/
public function authorize_url(OAuth_Token_Request $token, array $params = NULL)
{
// Create a new GET request for a request token with the required parameters
$request = OAuth_Request::factory('authorize', 'GET', $this->url_authorize(), array(
'oauth_token' => $token->token,
));
if ($params)
{
// Load user parameters
$request->params($params);
}
return $request->as_url();
}
/**
* Exchange the request token for an access token.
*
* $token = $provider->access_token($consumer, $token);
*
* @param OAuth_Consumer consumer
* @param OAuth_Token_Request token
* @param array additional request parameters
* @return OAuth_Token_Access
*/
public function access_token(OAuth_Consumer $consumer, OAuth_Token_Request $token, array $params = NULL)
{
// Create a new GET request for a request token with the required parameters
$request = OAuth_Request::factory('access', 'GET', $this->url_access_token(), array(
'oauth_consumer_key' => $consumer->key,
'oauth_token' => $token->token,
'oauth_verifier' => $token->verifier,
));
if ($params)
{
// Load user parameters
$request->params($params);
}
// Sign the request using only the consumer, no token is available yet
$request->sign($this->signature, $consumer, $token);
// Create a response from the request
$response = $request->execute();
// Store this token somewhere useful
return OAuth_Token::factory('access', array(
'token' => $response->param('oauth_token'),
'secret' => $response->param('oauth_token_secret'),
));
}
} // End OAuth_Signature