summaryrefslogtreecommitdiff
blob: 49d2aac60004041f8c494d5c26fe22cc6efb1f63 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
// vim: foldmethod=marker
/**
 * The MIT License
 *
 * Copyright (c) 2007 Andy Smith
 *
 * 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.
 */

namespace MediaWiki\Extensions\OAuth\Lib;

use MediaWiki\Extensions\OAuth\Lib\OAuthDataStore;
use MediaWiki\Extensions\OAuth\Lib\OAuthException;
use MediaWiki\Extensions\OAuth\Lib\OAuthRequest;
use MediaWiki\Logger\LoggerFactory;

class OAuthServer {
	protected $timestamp_threshold = 300; // in seconds, five minutes
	protected $version = '1.0'; // hi blaine
	protected $signature_methods = array();

	/** @var OAuthDataStore */
	protected $data_store;

	/** @var \\Psr\\Log\\LoggerInterface */
	protected $logger;

	function __construct( $data_store ) {
		$this->data_store = $data_store;
		$this->logger = LoggerFactory::getInstance( 'OAuth' );
	}

	public function add_signature_method( $signature_method ) {
		$this->signature_methods[$signature_method->get_name()] = $signature_method;
	}

	// high level functions

	/**
	 * process a request_token request
	 * returns the request token on success
	 */
	public function fetch_request_token( &$request ) {
		$this->get_version( $request );

		$consumer = $this->get_consumer( $request );

		// no token required for the initial token request
		$token = null;

		$this->check_signature( $request, $consumer, $token );

		// Rev A change
		$callback = $request->get_parameter( 'oauth_callback' );
		$new_token = $this->data_store->new_request_token( $consumer, $callback );

		return $new_token;
	}

	/**
	 * process an access_token request
	 * returns the access token on success
	 */
	public function fetch_access_token( &$request ) {
		$this->get_version( $request );

		$consumer = $this->get_consumer( $request );

		// requires authorized request token
		$token = $this->get_token( $request, $consumer, "request" );

		$this->check_signature( $request, $consumer, $token );

		// Rev A change
		$verifier = $request->get_parameter( 'oauth_verifier' );
		$new_token = $this->data_store->new_access_token( $token, $consumer, $verifier );

		return $new_token;
	}

	/**
	 * verify an api call, checks all the parameters
	 */
	public function verify_request( &$request ) {
		$this->get_version( $request );
		$consumer = $this->get_consumer( $request );
		$token = $this->get_token( $request, $consumer, "access" );
		$this->check_signature( $request, $consumer, $token );

		return array(
			$consumer,
			$token
		);
	}

	// Internals from here

	/**
	 * version 1
	 */
	protected function get_version( &$request ) {
		$version = $request->get_parameter( "oauth_version" );
		if ( !$version ) {
			// Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present.
			// Chapter 7.0 ( "Accessing Protected Ressources" )
			$version = '1.0';
		}
		if ( $version !== $this->version ) {
			throw new OAuthException( "OAuth version '$version' not supported" );
		}

		return $version;
	}

	/**
	 * figure out the signature with some defaults
	 */
	private function get_signature_method( $request ) {
		$signature_method = $request instanceof OAuthRequest ? $request->get_parameter(
			"oauth_signature_method"
		) : null;

		if ( !$signature_method ) {
			// According to chapter 7 ( "Accessing Protected Ressources" ) the signature-method
			// parameter is required, and we can't just fallback to PLAINTEXT
			throw new OAuthException( 'No signature method parameter. This parameter is required' );
		}

		if ( !in_array( $signature_method, array_keys( $this->signature_methods ) ) ) {
			throw new OAuthException(
				"Signature method '$signature_method' not supported " . "try one of the following: " . implode(
					", ",
					array_keys( $this->signature_methods )
				)
			);
		}

		return $this->signature_methods[$signature_method];
	}

	/**
	 * try to find the consumer for the provided request's consumer key
	 */
	protected function get_consumer( $request ) {
		$consumer_key = $request instanceof OAuthRequest ? $request->get_parameter(
			"oauth_consumer_key"
		) : null;

		if ( !$consumer_key ) {
			throw new OAuthException( "Invalid consumer key" );
		}
		$this->logger->debug( __METHOD__ . ": getting consumer for '$consumer_key'" );
		$consumer = $this->data_store->lookup_consumer( $consumer_key );
		if ( !$consumer ) {
			throw new OAuthException( "Invalid consumer" );
		}

		return $consumer;
	}

	/**
	 * try to find the token for the provided request's token key
	 */
	protected function get_token( $request, $consumer, $token_type = "access" ) {
		$token_field = $request instanceof OAuthRequest ? $request->get_parameter(
			'oauth_token'
		) : null;

		$token = $this->data_store->lookup_token(
			$consumer,
			$token_type,
			$token_field
		);
		if ( !$token ) {
			throw new OAuthException( "Invalid $token_type token: $token_field" );
		}

		return $token;
	}

	/**
	 * all-in-one function to check the signature on a request
	 * should guess the signature method appropriately
	 */
	protected function check_signature( $request, $consumer, $token ) {
		// this should probably be in a different method
		$timestamp = $request instanceof OAuthRequest ? $request->get_parameter(
			'oauth_timestamp'
		) : null;
		$nonce = $request instanceof OAuthRequest ? $request->get_parameter( 'oauth_nonce' ) : null;

		$this->check_timestamp( $timestamp );
		$this->check_nonce( $consumer, $token, $nonce, $timestamp );

		$signature_method = $this->get_signature_method( $request );
		$signature = $request->get_parameter( 'oauth_signature' );
		$valid_sig = $signature_method->check_signature(
			$request,
			$consumer,
			$token,
			$signature
		);

		if ( !$valid_sig ) {
			$this->logger->info(
				__METHOD__ . ': Signature check (' . get_class( $signature_method ) . ') failed'
			);
			throw new OAuthException( "Invalid signature" );
		}
	}

	/**
	 * check that the timestamp is new enough
	 */
	private function check_timestamp( $timestamp ) {
		if ( !$timestamp ) {
			throw new OAuthException(
				'Missing timestamp parameter. The parameter is required'
			);
		}

		// verify that timestamp is recentish
		$now = time();
		if ( abs( $now - $timestamp ) > $this->timestamp_threshold ) {
			throw new OAuthException(
				"Expired timestamp, yours $timestamp, ours $now"
			);
		}
	}

	/**
	 * check that the nonce is not repeated
	 */
	private function check_nonce( $consumer, $token, $nonce, $timestamp ) {
		if ( !$nonce ) {
			throw new OAuthException(
				'Missing nonce parameter. The parameter is required'
			);
		}

		// verify that the nonce is uniqueish
		$found = $this->data_store->lookup_nonce(
			$consumer,
			$token,
			$nonce,
			$timestamp
		);
		if ( $found ) {
			throw new OAuthException( "Nonce already used: $nonce" );
		}
	}

}