Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
<?php
2
/**
3
 * OAuth2 Provider
4
 *
5
 * @package    CodeIgniter/OAuth2
6
 * @category   Provider
7
 * @author     Phil Sturgeon
8
 * @copyright  (c) 2012 HappyNinjas Ltd
9
 * @license    http://philsturgeon.co.uk/code/dbad-license
10
 */
11
 
12
abstract class OAuth2_Provider
13
{
14
	/**
15
	 * @var  string  provider name
16
	 */
17
	public $name;
18
 
19
	/**
20
	 * @var  string  uid key name
21
	 */
22
	public $uid_key = 'uid';
23
 
24
	/**
25
	 * @var  string  additional request parameters to be used for remote requests
26
	 */
27
	public $callback;
28
 
29
	/**
30
	 * @var  array  additional request parameters to be used for remote requests
31
	 */
32
	protected $params = array();
33
 
34
	/**
35
	 * @var  string  the method to use when requesting tokens
36
	 */
37
	protected $method = 'GET';
38
 
39
	/**
40
	 * @var  string  default scope (useful if a scope is required for user info)
41
	 */
42
	protected $scope;
43
 
44
	/**
45
	 * @var  string  scope separator, most use "," but some like Google are spaces
46
	 */
47
	protected $scope_seperator = ',';
48
 
49
	/**
50
	 * Overloads default class properties from the options.
51
	 *
52
	 * Any of the provider options can be set here, such as app_id or secret.
53
	 *
54
	 * @param   array   provider options
55
	 * @return  void
56
	 */
57
	public function __construct(array $options = array())
58
	{
59
		if ( ! $this->name)
60
		{
61
 
62
			// Attempt to guess the name from the class name
63
			$this->name = strtolower(substr(get_class($this), strlen('OAuth2_Provider_')));
64
 
65
		}
66
 
67
		if (empty($options['id']))
68
		{
69
 
70
			throw new Exception('Required option not provided: id');
71
		}
72
 
73
		$this->client_id = $options['id'];
74
 
75
		isset($options['callback']) and $this->callback = $options['callback'];
76
		isset($options['secret']) and $this->client_secret = $options['secret'];
77
		isset($options['scope']) and $this->scope = $options['scope'];
78
 
79
		$this->redirect_uri = base_url(get_instance()->uri->uri_string());
80
		//echo $this->redirect_uri;
81
	}
82
 
83
	/**
84
	 * Return the value of any protected class variable.
85
	 *
86
	 *     // Get the provider signature
87
	 *     $signature = $provider->signature;
88
	 *
89
	 * @param   string  variable name
90
	 * @return  mixed
91
	 */
92
	public function __get($key)
93
	{
94
		return $this->$key;
95
	}
96
 
97
	/**
98
	 * Returns the authorization URL for the provider.
99
	 *
100
	 *     $url = $provider->url_authorize();
101
	 *
102
	 * @return  string
103
	 */
104
	abstract public function url_authorize();
105
 
106
	/**
107
	 * Returns the access token endpoint for the provider.
108
	 *
109
	 *     $url = $provider->url_access_token();
110
	 *
111
	 * @return  string
112
	 */
113
	abstract public function url_access_token();
114
 
115
	/*
116
	* Get an authorization code from Facebook.  Redirects to Facebook, which this redirects back to the app using the redirect address you've set.
117
	*/	
118
	public function authorize($options = array())
119
	{
120
 
121
		$state = md5(uniqid(rand(), TRUE));
122
		get_instance()->session->set_userdata('state', $state);
123
 
124
 
125
		$params = array(
126
			'client_id' 		=> $this->client_id,
127
			'redirect_uri' 		=> isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri,
128
			'state' 			=> $state,
129
			'scope'				=> is_array($this->scope) ? implode($this->scope_seperator, $this->scope) : $this->scope,
130
			'response_type' 	=> 'code',
131
			'approval_prompt'   => 'force' // - google force-recheck
132
		);
133
		//print_r($params);
134
 
135
		return $this->url_authorize().'?'.http_build_query($params);
136
	}
137
 
138
	/*
139
	* Get access to the API
140
	*
141
	* @param	string	The access code
142
	* @return	object	Success or failure along with the response details
143
	*/	
144
	public function access($code, $options = array())
145
	{
146
		$params = array(
147
			'client_id' 	=> $this->client_id,
148
			'client_secret' => $this->client_secret,
149
			'grant_type' 	=> isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code',
150
		);
151
 
152
		switch ($params['grant_type'])
153
		{
154
			case 'authorization_code':
155
				$params['code'] = $code;
156
				$params['redirect_uri'] = isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri;
157
			break;
158
 
159
			case 'refresh_token':
160
				$params['refresh_token'] = $code;
161
			break;
162
		}
163
 
164
 
165
		$response = null;	
166
		$url = $this->url_access_token();
167
 
168
		switch ($this->method)
169
		{
170
			case 'GET':
171
 
172
				// Need to switch to Request library, but need to test it on one that works
173
				$url .= '?'.http_build_query($params);
174
				$ch = curl_init($url);                                                                      
175
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                   
176
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
177
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);                                                                       
178
				$response = curl_exec($ch);
179
				parse_str($response, $return);
180
 
181
			break;
182
 
183
			case 'POST':
184
 
185
				$postdata = http_build_query($params);
186
				// $opts = array(
187
				// 	'http' => array(
188
				// 		'method'  => 'POST',
189
				// 		'header'  => 'Content-type: application/x-www-form-urlencoded',
190
				// 		'content' => $postdata
191
				// 	)
192
				// );
193
				$ch = curl_init($url);                                                                      
194
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
195
				curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);                                                                  
196
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
197
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);                                                                      
198
				curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));                                                                                                                   
199
				$response = curl_exec($ch);
200
				//$context  = stream_context_create($opts);
201
				//$response = file_get_contents($url, false, $context);
202
 
203
				$return = get_object_vars(json_decode($response));
204
 
205
			break;
206
 
207
			default:
208
				throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST");
209
		}
210
 
211
		if ( ! empty($return['error']))
212
		{
213
			throw new OAuth2_Exception($return);
214
		}
215
 
216
		return OAuth2_Token::factory('access', $return);
217
	}
218
 
219
}