aboutsummaryrefslogtreecommitdiff
path: root/classes/OpenID_ElggStore.php
blob: 35f4797a1122c94a5db15b2957cbbe5181d18be7 (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
<?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();
		}
	}
}