LTI Integration Library  3.1.0
PHP class library for building LTI integrations
OAuthServer.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ceLTIc\LTI\OAuth;
4 
13 {
14 
15  protected $timestamp_threshold = 300; // in seconds, five minutes
16  protected $version = '1.0'; // hi blaine
17  protected $signature_methods = array();
18  protected $data_store;
19 
21  {
22  $this->data_store = $data_store;
23  }
24 
25  public function add_signature_method($signature_method)
26  {
27  $this->signature_methods[$signature_method->get_name()] = $signature_method;
28  }
29 
30  // high level functions
31 
36  public function fetch_request_token(&$request)
37  {
38  $this->get_version($request);
39 
40  $consumer = $this->get_consumer($request);
41 
42  // no token required for the initial token request
43  $token = NULL;
44 
45  $this->check_signature($request, $consumer, $token);
46 
47  // Rev A change
48  $callback = $request->get_parameter('oauth_callback');
49  $new_token = $this->data_store->new_request_token($consumer, $callback);
50 
51  return $new_token;
52  }
53 
58  public function fetch_access_token(&$request)
59  {
60  $this->get_version($request);
61 
62  $consumer = $this->get_consumer($request);
63 
64  // requires authorized request token
65  $token = $this->get_token($request, $consumer, "request");
66 
67  $this->check_signature($request, $consumer, $token);
68 
69  // Rev A change
70  $verifier = $request->get_parameter('oauth_verifier');
71  $new_token = $this->data_store->new_access_token($token, $consumer, $verifier);
72 
73  return $new_token;
74  }
75 
79  public function verify_request(&$request)
80  {
81  $this->get_version($request);
82  $consumer = $this->get_consumer($request);
83  $token = $this->get_token($request, $consumer, "access");
84  $this->check_signature($request, $consumer, $token);
85 
86  return array($consumer, $token);
87  }
88 
89  // Internals from here
90 
94  private function get_version(&$request)
95  {
96  $version = $request->get_parameter("oauth_version");
97  if (!$version) {
98  // Service Providers MUST assume the protocol version to be 1.0 if this parameter is not present.
99  // Chapter 7.0 ("Accessing Protected Ressources")
100  $version = '1.0';
101  }
102  if ($version !== $this->version) {
103  throw new OAuthException("OAuth version '$version' not supported");
104  }
105 
106  return $version;
107  }
108 
112  private function get_signature_method($request)
113  {
114  $signature_method = $request instanceof OAuthRequest ? $request->get_parameter('oauth_signature_method') : NULL;
115 
116  if (!$signature_method) {
117  // According to chapter 7 ("Accessing Protected Ressources") the signature-method
118  // parameter is required, and we can't just fallback to PLAINTEXT
119  throw new OAuthException('No signature method parameter. This parameter is required');
120  }
121 
122  if (!in_array($signature_method, array_keys($this->signature_methods))) {
123  throw new OAuthException(
124  "Signature method '$signature_method' not supported " .
125  'try one of the following: ' .
126  implode(', ', array_keys($this->signature_methods))
127  );
128  }
129 
130  return $this->signature_methods[$signature_method];
131  }
132 
136  private function get_consumer($request)
137  {
138  $consumer_key = $request instanceof OAuthRequest ? $request->get_parameter('oauth_consumer_key') : NULL;
139 
140  if (is_null($consumer_key) || (strlen($consumer_key) <= 0)) {
141  throw new OAuthException('Invalid consumer key');
142  }
143 
144  $consumer = $this->data_store->lookup_consumer($consumer_key);
145  if (!$consumer) {
146  throw new OAuthException('Invalid consumer');
147  }
148 
149  return $consumer;
150  }
151 
155  private function get_token($request, $consumer, $token_type = "access")
156  {
157  $token_field = $request instanceof OAuthRequest ? $request->get_parameter('oauth_token') : NULL;
158 
159  $token = $this->data_store->lookup_token($consumer, $token_type, $token_field);
160  if (!$token) {
161  throw new OAuthException("Invalid $token_type token: $token_field");
162  }
163 
164  return $token;
165  }
166 
171  private function check_signature($request, $consumer, $token)
172  {
173  // this should probably be in a different method
174  $timestamp = $request instanceof OAuthRequest ? $request->get_parameter('oauth_timestamp') : NULL;
175  $nonce = $request instanceof OAuthRequest ? $request->get_parameter('oauth_nonce') : NULL;
176 
177  $this->check_timestamp($timestamp);
178  $this->check_nonce($consumer, $token, $nonce, $timestamp);
179 
180  $signature_method = $this->get_signature_method($request);
181 
182  $signature = $request->get_parameter('oauth_signature');
183  $valid_sig = $signature_method->check_signature($request, $consumer, $token, $signature);
184 
185  if (!$valid_sig) {
186  throw new OAuthException('Invalid signature');
187  }
188  }
189 
193  private function check_timestamp($timestamp)
194  {
195  if (!$timestamp)
196  throw new OAuthException('Missing timestamp parameter. The parameter is required');
197 
198  // verify that timestamp is recentish
199  $now = time();
200  if (abs($now - $timestamp) > $this->timestamp_threshold) {
201  throw new OAuthException("Expired timestamp, yours $timestamp, ours $now");
202  }
203  }
204 
208  private function check_nonce($consumer, $token, $nonce, $timestamp)
209  {
210  if (!$nonce)
211  throw new OAuthException('Missing nonce parameter. The parameter is required');
212 
213  // verify that the nonce is uniqueish
214  $found = $this->data_store->lookup_nonce($consumer, $token, $nonce, $timestamp);
215  if ($found) {
216  throw new OAuthException("Nonce already used: $nonce");
217  }
218  }
219 
220 }
add_signature_method($signature_method)
Definition: OAuthServer.php:25
Class to represent an OAuth Exception.
fetch_request_token(&$request)
process a request_token request returns the request token on success
Definition: OAuthServer.php:36
verify_request(&$request)
verify an api call, checks all the parameters
Definition: OAuthServer.php:79
fetch_access_token(&$request)
process an access_token request returns the access token on success
Definition: OAuthServer.php:58
Class to represent an OAuth Server.
Definition: OAuthServer.php:12