aboutsummaryrefslogtreecommitdiff
path: root/mod/openid_api/classes/OpenID_ElggStore.php
blob: 2d4800e9bc48adcfb6715bbb6e5b659f92bc8b62 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
 * Uses Elgg data model to store OpenID associations
 *
 * Written to be compatible with the Elgg store written by Kevin Jardine for
 * the original OpenID client plugin.
 */
class OpenID_ElggStore extends Auth_OpenID_OpenIDStore {

	/**
	 * Store an association
	 *
	 * @param string                  $server_url  The identity server endpoint
	 * @param Auth_OpenID_Association $association The association to store.
	 */
	public function storeAssociation($server_url, $association) {

		$object = new ElggObject();
		$object->subtype = 'openid_client::association';
		$object->owner_guid = 0;
		$object->container_guid = 0;
		$object->title = 'association';
		$object->access_id = ACCESS_PUBLIC;
		$object->server_url = $server_url;
		$object->handle = $association->handle;
		$object->secret = base64_encode($association->secret);
		$object->issued = $association->issued;
		$object->lifetime = $association->lifetime;
		$object->expires = $object->issued + $object->lifetime;
		$object->assoc_type = $association->assoc_type;

		$object->save();
	}

	/**
	 * Get an association
	 * 
	 * @param string $server_url The identity server endpoint
	 * @param string $handle     The association handle
	 * @return Auth_OpenID_Association
	 */
	public function getAssociation($server_url, $handle = null) {

		$assocs = $this->getAssociations($server_url, $handle);
		if (!$assocs || count($assocs) == 0) {
			return null;
		} else {
			$associations = array();

			foreach ($assocs as $object) {
				$assoc = new Auth_OpenID_Association(
								$object->handle,
								base64_decode($object->secret),
								$object->issued,
								$object->lifetime,
								$object->assoc_type);

				if ($assoc->getExpiresIn() == 0) {
					$this->removeAssociation($server_url, $assoc->handle);
				} else {
					$associations[] = array($assoc->issued, $assoc);
				}
			}

			if ($associations) {
				$issued = array();
				$assocs = array();
				foreach ($associations as $key => $assoc) {
					$issued[$key] = $assoc[0];
					$assocs[$key] = $assoc[1];
				}

				array_multisort($issued, SORT_DESC, $assocs, SORT_DESC, $associations);

				// return the most recently issued one.
				list($issued, $assoc) = $associations[0];
				return $assoc;
			} else {
				return null;
			}
		}
	}

	/**
	 * Remove selected associations from storage
	 *
	 * @param string $server_url The identity server endpoint
	 * @param string $handle     The association handle
	 */
	public function removeAssociation($server_url, $handle) {
		$assocs = $this->getAssociations($server_url, $handle);
		foreach ($assocs as $assoc) {
			$assoc->delete();
		}
	}

	/**
	 * Get all associations
	 * 
	 * @param string $server_url The identity server endpoint
	 * @param string $handle     The association handle
	 * @return array
	 */
	protected function getAssociations($server_url, $handle) {
		$options = array(
			'type' => 'object',
			'subtype' => 'openid_client::association',
			'metadata_name_value_pairs' => array(
				array('name' => 'server_url', 'value' => $server_url)
			),
			'limit' => 0,
		);

		if ($handle !== null) {
			$options['metadata_name_value_pairs'][] = array(
				'name' => 'handle',
				'value' => $handle
			);
		}

		return elgg_get_entities_from_metadata($options);
	}

	/**
	 * Cleanup associations that have expired
	 *
	 * @todo I think we need to store expire time ans use that here
	 *
	 * @return int
	 */
	public function cleanupAssociations() {
		$options = array(
			'type' => 'object',
			'subtype' => 'openid_client::association',
			'metadata_name_value_pairs' => array(
				array('name' => 'expires', 'value' => time(), 'operand' => '<')
			),
			'limit' => 0,
		);
		$associations = elgg_get_entities_from_metadata($options);
		$total = count($associations);

		foreach ($associations as $association) {
			$association->delete();
		}

		return $total;
	}

	/**
	 * Can we use this nonce?
	 *
	 * Checks for time skew and replay attacks
	 *
	 * @param type $server_url The identity server endpoint
	 * @param type $timestamp  The timestamp from the nonce
	 * @param type $salt       The salt from the nonce
	 * @return bool
	 */
	public function useNonce($server_url, $timestamp, $salt) {
		global $Auth_OpenID_SKEW;

		if (abs($timestamp - time()) > $Auth_OpenID_SKEW) {
			return false;
		}

		if (!$this->addNonce($server_url, $timestamp, $salt)) {
			return false;
		}

		return true;
	}

	/**
	 * Store the nonce to prevent replay attacks
	 *
	 * @param string $server_url
	 * @param string $timestamp
	 * @param string $salt
	 * @return bool
	 */
	protected function addNonce($server_url, $timestamp, $salt) {
		global $Auth_OpenID_SKEW;

		$identifier = md5($server_url . $timestamp . $salt);

		// was the nonce already used
		$count = elgg_get_entities_from_metadata(array(
			'type' => 'object',
			'subtype' => 'openid_client::nonce',
			'metadata_name' => 'identifier',
			'metadata_value' => $identifier,
			'count' => true,
		));
		if ($count) {
			return false;
		}

		// add it
		$object = new ElggObject();
		$object->subtype = 'openid_client::nonce';
		$object->owner_guid = 0;
		$object->container_guid = 0;
		$object->access_id = ACCESS_PUBLIC;
		$object->server_url = $server_url;
		$object->expires = $timestamp + $Auth_OpenID_SKEW;
		$object->identifier = $identifier;
		$object->save();

		return true;
	}

	/**
	 * Cleanup nonces that would not pass the time skew test
	 *
	 * @return int
	 */
	public function cleanupNonces() {
		$options = array(
			'type' => 'object',
			'subtype' => 'openid_client::nonce',
			'metadata_name_value_pairs' => array(
				array('name' => 'expires', 'value' => time(), 'operand' => '<')
			),
			'limit' => 0,
		);
		$nonces = elgg_get_entities_from_metadata($options);
		$total = count($nonces);

		foreach ($nonces as $nonce) {
			$nonce->delete();
		}

		return $total;
	}

	/**
	 * Does this storage class support cleanup?
	 *
	 * @return bool
	 */
	public function supportsCleanup() {
		return true;
	}

	/**
	 * Cleanup expired data in storage
	 *
	 * @return array
	 */
	public function cleanup() {
		return array($this->cleanupNonces(), $this->cleanupAssociations());
	}

	/**
	 * Remove everything from storage
	 *
	 * @return void
	 */
	public function reset() {
		$associations = elgg_get_entities(array(
			'type' => 'object',
			'subtype' => 'openid_client::association',
			'limit' => 0,
		));
		foreach ($associations as $association) {
			$association->delete();
		}

		$nonces = elgg_get_entities(array(
			'type' => 'object',
			'subtype' => 'openid_client::nonce',
			'limit' => 0,
		));

		foreach ($nonces as $nonce) {
			$nonce->delete();
		}
	}

	protected function getTrustedSites($user_guid = true) {
		
		if ($user_guid === true) {
			$user_guid = elgg_get_logged_in_user_guid();
		} elseif (!$user_guid) {
			$user_guid = ELGG_ENTITIES_ANY_VALUE;
		}

		$results = elgg_get_entities(array(
			'type' => 'object',
			'subtype' => 'openid_server::trust_root',
			'owner_guid' => $user_guid,
		));
		
		return $results;
	}
	
	public function getTrustedRoots ($user_guid = true) {
		
		$results = $this->getTrustedSites($user_guid);
		
		$sites = array();
		if ($results) {
			foreach ($results as $site) {
				$sites[] = $site->trust_root;
			}
		}
		return $sites;
	}

	/**
	* Returns the autologin URLs for every trusted site
	*/ 	

	public function getAutoLoginSites() {
		
		$default_trusted_sites = $this->getTrustedSites(false);

		$sites = array();
		if ($default_trusted_sites) {
			foreach ($default_trusted_sites as $site) {
				if ($site->auto_login_url) {
					$sites[] = $site;
				}
			}
		}
		return $sites;
	}

	/**
	* Returns the autologout URLs for every trusted site
	*/ 	

	public function getAutoLogoutSites() {

		$default_trusted_sites = $this->getTrustedSites(false);
		
		$sites = array();
		if ($default_trusted_sites) {
			foreach ($default_trusted_sites as $site) {
				if ($site->auto_logout_url) {
					$sites[] = $site;
				}
			}
		}
		return $sites;
	}

	static function isTrustedRoot($trust_root) {
		$results = elgg_get_entities_from_metadata(array(
			'type' => 'object',
			'subtype' => 'openid_server::trust_root',
			'owner_guid' => elgg_get_logged_in_user_guid(),
			'metadata_name' => 'trust_root',
			'metadata_value' => $trust_root,
		));
		
		return !! $results;
	}

	public function setTrustedSite($trust_root) {
		
		if (self::isTrustedRoot($trust_root)) {
			return true;
		}
		
		$site = new ElggObject();
		$site->subtype = 'openid_server::trust_root';
		$site->owner_guid = elgg_get_logged_in_user_guid();
		$site->title = 'association';
		$site->access_id = ACCESS_PUBLIC;

		if ($site->save()) {
			$site->trust_root = $trust_root->trust_root;
			$site->site_name = $trust_root->site_name;
			$site->autologin = $trust_root->autologin;
			$site->autologout = $trust_root->autologout;
			$site->width = $trust_root->width;
			$site->height = $trust_root->height;
			return true;
		}
		return false; 	
	}

	public function removeUserTrustedSites($user = true) {

		$results = $this->getTrustedSites($user);

		if ($results) {
			foreach($results as $trust_root) {
				$trust_root->delete();
			}
		}
		return true;
	}

	public function removeTrustedSite($trust_root, $user_guid = true) {
		
		if ($user_guid === true) {
			$user_guid = elgg_get_logged_in_user_guid();
		} elseif (!$user_guid) {
			$user_guid = ELGG_ENTITIES_ANY_VALUE;
		}

		$results = elgg_get_entities_from_metadata(array(
			'type' => 'object',
			'subtype' => 'openid_server::trust_root',
			'owner_guid' => $user_guid,
			'metadata_name' => 'trust_root',
			'metadata_value' => $trust_root,
		));

		if ($results) {
			foreach($results as $trust_root) {
				$trust_root->delete();
			}
		}
		return true;
	}
}