3namespace ceLTIc\LTI\OAuth;
60 $this->signature_methods[$signature_method->get_name()] = $signature_method;
76 $this->get_version($request);
78 $consumer = $this->get_consumer($request);
83 $this->check_signature($request, $consumer, $token);
86 $callback = $request->get_parameter(
'oauth_callback');
87 $new_token = $this->data_store->new_request_token($consumer, $callback);
103 $this->get_version($request);
105 $consumer = $this->get_consumer($request);
108 $token = $this->get_token($request, $consumer,
"request");
110 $this->check_signature($request, $consumer, $token);
113 $verifier = $request->get_parameter(
'oauth_verifier');
114 $new_token = $this->data_store->new_access_token($token, $consumer, $verifier);
128 $this->get_version($request);
129 $consumer = $this->get_consumer($request);
130 $token = $this->get_token($request, $consumer,
"access");
131 $this->check_signature($request, $consumer, $token);
133 return array($consumer, $token);
148 private function get_version(&$request)
150 $version = $request->get_parameter(
"oauth_version");
157 throw new OAuthException(
"OAuth version '$version' not supported");
171 private function get_signature_method($request)
173 $signature_method = $request instanceof OAuthRequest ? $request->get_parameter(
'oauth_signature_method') :
null;
175 if (!$signature_method) {
178 throw new OAuthException(
'No signature method parameter. This parameter is required');
181 if (!in_array($signature_method, array_keys($this->signature_methods))) {
182 throw new OAuthException(
183 "Signature method '$signature_method' not supported " .
184 'try one of the following: ' .
185 implode(
', ', array_keys($this->signature_methods))
189 return $this->signature_methods[$signature_method];
200 private function get_consumer($request)
202 $consumer_key = $request instanceof OAuthRequest ? $request->get_parameter(
'oauth_consumer_key') :
null;
204 if (is_null($consumer_key) || (strlen($consumer_key) <= 0)) {
205 throw new OAuthException(
'Invalid consumer key');
208 $consumer = $this->data_store->lookup_consumer($consumer_key);
210 throw new OAuthException(
'Invalid consumer');
226 private function get_token($request, $consumer, $token_type =
"access")
228 $token_field = $request instanceof OAuthRequest ? $request->get_parameter(
'oauth_token') :
null;
230 $token = $this->data_store->lookup_token($consumer, $token_type, $token_field);
232 throw new OAuthException(
"Invalid $token_type token: $token_field");
246 private function check_signature($request, $consumer, $token)
249 $timestamp = $request instanceof OAuthRequest ? $request->get_parameter(
'oauth_timestamp') :
null;
250 $nonce = $request instanceof OAuthRequest ? $request->get_parameter(
'oauth_nonce') :
null;
252 $this->check_timestamp($timestamp);
253 $this->check_nonce($consumer, $token, $nonce, $timestamp);
255 $signature_method = $this->get_signature_method($request);
257 $signature = $request->get_parameter(
'oauth_signature');
258 $valid_sig = $signature_method->check_signature($request, $consumer, $token, $signature);
261 throw new OAuthException(
'Invalid signature');
271 private function check_timestamp($timestamp)
274 throw new OAuthException(
'Missing timestamp parameter. The parameter is required');
278 if (abs($now - $timestamp) > $this->timestamp_threshold) {
279 throw new OAuthException(
"Expired timestamp, yours $timestamp, ours $now");
292 private function check_nonce($consumer, $token, $nonce, $timestamp)
295 throw new OAuthException(
'Missing nonce parameter. The parameter is required');
298 $found = $this->data_store->lookup_nonce($consumer, $token, $nonce, $timestamp);
300 throw new OAuthException(
"Nonce already used: $nonce");
Class to represent an OAuth Exception.
Class to represent an OAuth server.
fetch_access_token(&$request)
Process an access_token request.
$timestamp_threshold
Timestamp threshhold.
verify_request(&$request)
Verify an API call, checks all the parameters.
add_signature_method($signature_method)
Add a signature method.
__construct($data_store)
Class constructor.
$signature_methods
Signature methods.
fetch_request_token(&$request)
Process a request_token request.