aboutsummaryrefslogtreecommitdiff
path: root/mod/oauth_api/vendors/oauth/library/OAuthDiscovery.php
blob: d097756dd242dee87e20d5f14d5b963fcfee1e29 (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

/**
 * Handle the discovery of OAuth service provider endpoints and static consumer identity.
 * 
 * @version $Id$
 * @author Marc Worrell <marcw@pobox.com>
 * @date  Sep 4, 2008 5:05:19 PM
 * 
 * The MIT License
 * 
 * Copyright (c) 2007-2008 Mediamatic Lab
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

require_once dirname(__FILE__).'/discovery/xrds_parse.php';

require_once dirname(__FILE__).'/OAuthException.php';
require_once dirname(__FILE__).'/OAuthRequestLogger.php';


class OAuthDiscovery
{
	/**
	 * Return a description how we can do a consumer allocation.  Prefers static allocation if
	 * possible.  If static allocation is possible
	 * 
	 * See also: http://oauth.net/discovery/#consumer_identity_types
	 * 
	 * @param string uri
	 * @return array		provider description
	 */
	static function discover ( $uri )
	{
		// See what kind of consumer allocations are available
		$xrds_file = self::discoverXRDS($uri);
		if (!empty($xrds_file))
		{
			$xrds = xrds_parse($xrds_file);
			if (empty($xrds))
			{
				throw new OAuthException('Could not discover OAuth information for '.$uri);
			}
		}
		else
		{
			throw new OAuthException('Could not discover XRDS file at '.$uri);
		}

		// Fill an OAuthServer record for the uri found
		$ps			= parse_url($uri);
		$host		= isset($ps['host']) ? $ps['host'] : 'localhost';
		$server_uri = $ps['scheme'].'://'.$host.'/';

		$p = array(
				'user_id'			=> null,
				'consumer_key'		=> '',
				'consumer_secret'	=> '',
				'signature_methods'	=> '',
				'server_uri'		=> $server_uri,
				'request_token_uri'	=> '',
				'authorize_uri'		=> '',
				'access_token_uri'	=> ''
			);


		// Consumer identity (out of bounds or static)
		if (isset($xrds['consumer_identity']))
		{
			// Try to find a static consumer allocation, we like those :)
			foreach ($xrds['consumer_identity'] as $ci)
			{
				if ($ci['method'] == 'static' && !empty($ci['consumer_key']))
				{
					$p['consumer_key']    = $ci['consumer_key'];
					$p['consumer_secret'] = '';
				}
				else if ($ci['method'] == 'oob' && !empty($ci['uri']))
				{
					// TODO: Keep this uri somewhere for the user?
					$p['consumer_oob_uri'] = $ci['uri'];
				}
			}
		}

		// The token uris
		if (isset($xrds['request'][0]['uri']))
		{
			$p['request_token_uri'] = $xrds['request'][0]['uri'];
			if (!empty($xrds['request'][0]['signature_method']))
			{
				$p['signature_methods'] = $xrds['request'][0]['signature_method'];
			}
		}
		if (isset($xrds['authorize'][0]['uri']))
		{
			$p['authorize_uri'] = $xrds['authorize'][0]['uri'];
			if (!empty($xrds['authorize'][0]['signature_method']))
			{
				$p['signature_methods'] = $xrds['authorize'][0]['signature_method'];
			}
		}
		if (isset($xrds['access'][0]['uri']))
		{
			$p['access_token_uri'] = $xrds['access'][0]['uri'];
			if (!empty($xrds['access'][0]['signature_method']))
			{
				$p['signature_methods'] = $xrds['access'][0]['signature_method'];
			}
		}
		return $p;
	}
	
	
	/**
	 * Discover the XRDS file at the uri.  This is a bit primitive, you should overrule
	 * this function so that the XRDS file can be cached for later referral.
	 * 
	 * @param string uri
	 * @return string		false when no XRDS file found
	 */
	static protected function discoverXRDS ( $uri, $recur = 0 )
	{
		// Bail out when we are following redirects
		if ($recur > 10)
		{
			return false;
		}
		
		$data = self::curl($uri);

		// Check what we got back, could be:
		// 1. The XRDS discovery file itself (check content-type)
		// 2. The X-XRDS-Location header
		
		if (is_string($data) && !empty($data))
		{
			list($head,$body) = explode("\r\n\r\n", $data);
			$body = trim($body);
			$m	  = false;

			// See if we got the XRDS file itself or we have to follow a location header
			if (	preg_match('/^Content-Type:\s*application\/xrds+xml/im', $head)
				||	preg_match('/^<\?xml[^>]*\?>\s*<xrds\s/i', $body)
				||	preg_match('/^<xrds\s/i', $body)
				)
			{
				$xrds = $body;
			}
			else if (	preg_match('/^X-XRDS-Location:\s*(.*)$/im', $head, $m)
					||	preg_match('/^Location:\s*(.*)$/im', $head, $m))
			{
				// Recurse to the given location
				if ($uri != $m[1])
				{
					$xrds = self::discoverXRDS($m[1], $recur+1);
				}
				else
				{
					// Referring to the same uri, bail out
					$xrds = false;
				}
			}
			else
			{
				// Not an XRDS file an nowhere else to check
				$xrds = false;
			}
		}
		else
		{
			$xrds = false;
		}
		return $xrds;
	}
	
	
	/**
	 * Try to fetch an XRDS file at the given location.  Sends an accept header preferring the xrds file.
	 * 
	 * @param string uri
	 * @return array	(head,body), false on an error
	 */
	static protected function curl ( $uri )
	{
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_HTTPHEADER,	 array('Accept: application/xrds+xml, */*;q=0.1'));
		curl_setopt($ch, CURLOPT_USERAGENT,		 'anyMeta/OAuth 1.0 - (OAuth Discovery $LastChangedRevision: 45 $)');
		curl_setopt($ch, CURLOPT_URL, 			 $uri);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, 		 true);

		$txt = curl_exec($ch);
		curl_close($ch);

		// Tell the logger what we requested and what we received back
		$data = "GET $uri";
		OAuthRequestLogger::setSent($data, "");
		OAuthRequestLogger::setReceived($txt);

		return $txt;
	}
}


/* vi:set ts=4 sts=4 sw=4 binary noeol: */

?>