9 * @author Bill Shupp <hostmaster@shupp.org>
10 * @copyright 2009 Bill Shupp
11 * @license http://www.opensource.org/licenses/bsd-license.php FreeBSD
12 * @link http://github.com/shupp/openid
15 // A tool for testing Relying Party functionality
17 __DIR__ . '/../../src/'
18 . PATH_SEPARATOR . get_include_path()
21 $pageRequiresLogin = false;
22 require_once 'www-header.php';
23 require_once 'openid/config.php';
26 if (isset($_REQUEST['logout'])) {
29 $redirect = 'http://' . $_SERVER['HTTP_HOST'];
30 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
34 if (!count($_GET) && !count($_POST)) {
42 // Hackaround Non-Javascript Login Page
43 if (!count($_POST) && isset($_GET['start'])) {
47 if (isset($_POST['openid_url'])) {
48 $openid_url = $_POST['openid_url'];
49 } else if (isset($_SESSION['openid_url'])) {
50 $openid_url = $_SESSION['openid_url'];
56 $o = new \OpenID_RelyingParty($returnTo, $realm, $openid_url);
57 } catch (OpenID_Exception $e) {
58 $contents = "<div class='openid_results'>\n";
59 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
60 $contents .= "</div class='openid_results'>";
61 include_once 'openid/wrapper.php';
65 if (!empty($_POST['disable_associations']) || !empty($_SESSION['disable_associations'])) {
66 $o->disableAssociations();
67 $_SESSION['disable_associations'] = true;
70 $log = new \OpenID_Observer_Log;
71 \OpenID::attach($log);
73 if (isset($_POST['start'])) {
75 $_SESSION['openid_url'] = $openid_url;
77 $authRequest = $o->prepare();
78 } catch (OpenID_Exception $e) {
79 $contents = "<div class='openid_results'>\n";
80 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
81 $contents .= "</div class='openid_results'>";
82 include_once 'openid/wrapper.php';
87 if (!empty($_POST['checkid_immediate'])) {
88 $authRequest->setMode('checkid_immediate');
92 $sreg = new \OpenID_Extension_SREG11(\OpenID_Extension::REQUEST);
93 $sreg->set('required', 'email,firstname,lastname,nickname');
94 $sreg->set('optional', 'gender,dob');
95 $authRequest->addExtension($sreg);
98 $ax = new \OpenID_Extension_AX(\OpenID_Extension::REQUEST);
99 $ax->set('type.email', 'http://axschema.org/contact/email');
100 $ax->set('type.firstname', 'http://axschema.org/namePerson/first');
101 $ax->set('type.lastname', 'http://axschema.org/namePerson/last');
102 $ax->set('mode', 'fetch_request');
103 $ax->set('required', 'email,firstname,lastname');
104 $authRequest->addExtension($ax);
107 if (!empty($_POST['ui'])) {
108 $ui = new \OpenID_Extension_UI(\OpenID_Extension::REQUEST);
109 $ui->set('mode', 'popup');
110 $ui->set('language', 'en-US');
111 $authRequest->addExtension($ui);
115 if (!empty($_POST['oauth'])) {
116 $oauth = new \OpenID_Extension_OAuth(\OpenID_Extension::REQUEST);
117 $oauth->set('consumer', $_POST['oauth_consumer_key']);
118 $_SESSION['OAuth_consumer_key'] = $_POST['oauth_consumer_key'];
119 $_SESSION['OAuth_consumer_secret'] = $_POST['oauth_consumer_secret'];
121 $oauth->set('scope', $_POST['oauth_scope']);
122 $_SESSION['OAuth_scope'] = $_POST['oauth_scope'];
124 $_SESSION['OAuth_access_token_url'] = $_POST['oauth_access_token_url'];
125 $_SESSION['OAuth_access_token_method'] = $_POST['oauth_access_token_method'];
127 $authRequest->addExtension($oauth);
130 $url = $authRequest->getAuthorizeURL();
132 if (empty($_POST['debug'])) {
133 header("Location: $url");
138 if (isset($_SESSION['openid_url'])) {
139 $usid = $_SESSION['openid_url'];
140 unset($_SESSION['openid_url']);
145 unset($_SESSION['disable_associations']);
147 if (!count($_POST)) {
148 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
150 // I hate php sometimes
151 $queryString = file_get_contents('php://input');
154 $message = new \OpenID_Message($queryString, \OpenID_Message::FORMAT_HTTP);
155 $id = $message->get('openid.claimed_id');
156 $mode = $message->get('openid.mode');
159 $result = $o->verify(new \Net_URL2($returnTo . '?' . $queryString), $message);
161 if ($result->success()) {
162 $status = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
163 $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
165 $status = "<tr><td>Status:</td><td><font color='red'>FAIL!";
166 $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
168 } catch (OpenID_Exception $e) {
169 $status = "<tr><td>Status:</td><td><font color='red'>EXCEPTION!";
170 $status .= " ({$e->getMessage()} : {$e->getCode()})</font></td></tr>";
173 // OAuth hyprid fetching access token
174 if (isset($_SESSION['OAuth_consumer_key'],
175 $_SESSION['OAuth_consumer_secret'],
176 $_SESSION['OAuth_access_token_url'],
177 $_SESSION['OAuth_access_token_method'])) {
180 $oauth = new \OpenID_Extension_OAuth(\OpenID_Extension::RESPONSE,
184 $consumerKey = $_SESSION['OAuth_consumer_key'];
185 $consumerSecret = $_SESSION['OAuth_consumer_key'];
186 $tokenURL = $_SESSION['OAuth_access_token_url'];
187 $tokenMethod = $_SESSION['OAuth_access_token_method'];
189 $oauthData = $oauth->getAccessToken($consumerKey,
195 } catch (Exception $e) {
199 $openid = $message->getArrayFormat();
201 $email = (isset($openid['openid.ext1.value.email'])) ? $openid['openid.ext1.value.email'] : null;
202 $email = (isset($openid['openid.ext2.value.email']) && !isset($email)) ? $openid['openid.ext2.value.email'] : $email;
203 $email = (isset($openid['openid.sreg.email']) && !isset($email)) ? $openid['openid.sreg.email'] : $email;
204 $email = (isset($openid['openid.ax.value.email']) && !isset($email)) ? $openid['openid.ax.value.email'] : $email;
205 $_SESSION['email'] = (isset($email)) ? $email : $GLOBALS['phorkie']['auth']['anonymousEmail'];
207 $name = (isset($openid['openid.ext1.value.firstname']) && isset($openid['openid.ext1.value.lastname'])) ? $openid['openid.ext1.value.firstname']." ".$openid['openid.ext1.value.lastname'] : null;
208 $name = (isset($openid['openid.sreg.firstname']) && isset($openid['openid.sreg.lastname']) && !isset($name)) ? $openid['openid.sreg.firstname']." ".$openid['openid.sreg.lastname'] : $name;
209 $name = (isset($openid['openid.sreg.nickname']) && !isset($name)) ? $openid['openid.sreg.nickname'] : $name;
210 $_SESSION['name'] = (isset($name)) ? $name : $_SERVER['REMOTE_ADDR'];
212 $_SESSION['identity'] = $openid['openid.identity'];
214 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SESSION['REQUEST_URI'];
215 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));