aboutsummaryrefslogtreecommitdiff
path: root/classes/ElggOpenIDConsumer.php
blob: 09820134368a648cf0592f8c7d199e2f64700858 (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
<?php
/**
 * Consumer for OpenID
 */

class ElggOpenIDConsumer {

	protected $openIdUrl;
	protected $returnURL;

	protected $store;
	protected $consumer;
	protected $request;

	/**
	 * Constructor
	 *
	 * @param Auth_OpenID_OpenIDStore $store Optional persistence store
	 */
	public function __construct(Auth_OpenID_OpenIDStore $store = null) {
		if ($store) {
			$this->store = $store;
		} else {
			// use the default store
			$this->store = new OpenID_ElggStore();
		}
	}

	/**
	 * Set the OpenID username
	 *
	 * @param string $username
	 */
	public function setURL($url) {
		$this->openIdUrl = $url;
	}

	/**
	 * Set the return URL
	 *
	 * @param string $url The URL the OpenID provider returns the user to
	 */
	public function setReturnURL($url) {
		$this->returnURL = $url;
	}

	/**
	 * Send a request to the provider for authentication
	 *
	 * @return mixed HTMl form on success and false for failure
	 */
	public function requestAuthentication() {

		if (!$this->store) {
			return false;
		}

		$this->consumer = new Auth_OpenID_Consumer($this->store);
		if (!$this->consumer) {
			return false;
		}

		$url = $this->openIdUrl;
		if (!$url) {
			return false;
		}

		// discovers the identity server
		$this->request = $this->consumer->begin($url);
		if (!$this->request) {
			return false;
		}

		// request user information
		if (!$this->addAttributeRequests()) {
			return false;
		}

		// send browser for authentication
		return $this->getForm();
	}

	/**
	 * Complete the OpenID authentication by parsing the response
	 *
	 * This returns an array of key value pairs about the user.
	 * 
	 * @return array
	 */
	public function completeAuthentication() {

		if (!$this->store) {
			return false;
		}

		$this->consumer = new Auth_OpenID_Consumer($this->store);
		if (!$this->consumer) {
			return false;
		}

		$response = $this->consumer->complete($this->returnURL);
		switch ($response->status) {
			case Auth_OpenID_SUCCESS:
				$data = $this->getUserData($response);
				break;
			case Auth_OpenID_FAILURE:
			case Auth_OpenID_CANCEL:
				$data = array();
				break;
		}

		return $data;
	}

	/**
	 * Add attribute requests to the OpenID authentication request
	 * 
	 * @return bool
	 */
	protected function addAttributeRequests() {

		// Simple Registration
		$required = array();
		$optional = array('email', 'nickname', 'fullname', 'language');
		$sregRequest = Auth_OpenID_SRegRequest::build($required, $optional);
		if (!$sregRequest) {
			return false;
		}
		$this->request->addExtension($sregRequest);

		// Attribute Exchange
		$axRequest = new Auth_OpenID_AX_FetchRequest();
		$attributes[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/contact/email', 1, true, 'email');
		$attributes[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/first', 1, true, 'firstname');
		$attributes[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/last', 1, true, 'lastname');
		foreach ($attributes as $attribute) {
			$axRequest->add($attribute);
		}
		$this->request->addExtension($axRequest);

		return true;
	}

	/**
	 * Gets the form to send the user to the provider to authenticate
	 *
	 * This implements OpenID 2.0 by submitting a form through JavaScript against
	 * the provider. If JavaScript is not enabled, a plain html form with a
	 * continue button is displayed.
	 *
	 * This also supports OpenID 1.x but has not been tested as thoroughly.
	 *
	 * @return mixed
	 */
	protected function getForm() {
		if (!$this->request->shouldSendRedirect()) {
			// OpenID 2.0
			$html = $this->request->htmlMarkup(elgg_get_site_url(), $this->returnURL, false);
			return $html;
		} else {
			// OpenID 1.x
			$redirect_url = $this->request->redirectURL(elgg_get_site_url(), $this->returnURL);

			if (Auth_OpenID::isFailure($redirect_url)) {
				return false;
			} else {
				forward($redirect_url);
			}
		}
	}

	/**
	 * Get user data from the OpenID response
	 * 
	 * @param Auth_OpenID_ConsumerResponse $response
	 * @return array
	 */
	protected function getUserData($response) {
		if (!$response) {
			return array();
		}

		$sregResponse = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
		$sreg = $sregResponse->contents();

		$axResponse = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($response);
		$ax = $axResponse->data;

		$data = $this->extractUserData($sreg, $ax);
		$data['openid_identifier'] = $response->getDisplayIdentifier();

		return $data;
	}

	/**
	 * Extract user data from the extensions in the response
	 *
	 * @param array $sreg Simple Registration data
	 * @param array $ax   Attribute Exchange data
	 * @return array
	 */
	protected function extractUserData($sreg, $ax) {
		$data = array();

		// email
		if (isset($sreg['email'])) {
			$data['email'] = $sreg['email'];
		}
		if (isset($ax['http://axschema.org/contact/email'])) {
			$data['email'] = $ax['http://axschema.org/contact/email'][0];
		}

		// display name
		if (isset($sreg['fullname'])) {
			$data['name'] = $sreg['fullname'];
		}
		if (isset($ax['http://axschema.org/namePerson/first'])) {
			$data['name'] = $ax['http://axschema.org/namePerson/first'][0];
		}
		if (isset($ax['http://axschema.org/namePerson/last'])) {
			$data['name'] .= ' ' . $ax['http://axschema.org/namePerson/last'][0];
			$data['name'] = trim($data['name']);
		}

		// username
		if (isset($sreg['nickname'])) {
			$data['username'] = $sreg['nickname'];
		}

		// language
		if (isset($sreg['language'])) {
			$languages = get_installed_translations();
			// @todo - find out format
		}

		return $data;
	}
}