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 require_once 'www-header.php';
22 require_once 'openid/config.php';
25 if (isset($_REQUEST['logout'])) {
28 $redirect = 'http://' . $_SERVER['HTTP_HOST'];
29 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
33 if (!count($_GET) && !count($_POST)) {
34 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . "/login";
35 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
39 // Hackaround Non-Javascript Login Page
40 if (!count($_POST) && isset($_GET['start'])) {
44 if (isset($_POST['openid_url'])) {
45 $openid_url = $_POST['openid_url'];
46 } else if (isset($_SESSION['openid_url'])) {
47 $openid_url = $_SESSION['openid_url'];
53 $o = new OpenID_RelyingParty($returnTo, $realm, $openid_url);
54 } catch (OpenID_Exception $e) {
55 $contents = "<div class='openid_results'>\n";
56 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
57 $contents .= "</div class='openid_results'>";
58 include_once 'openid/wrapper.php';
62 if (!empty($_POST['disable_associations']) || !empty($_SESSION['disable_associations'])) {
63 $o->disableAssociations();
64 $_SESSION['disable_associations'] = true;
67 $log = new OpenID_Observer_Log;
70 if (isset($_POST['start'])) {
72 $_SESSION['openid_url'] = $openid_url;
74 $authRequest = $o->prepare();
75 } catch (OpenID_Exception $e) {
76 $contents = "<div class='openid_results'>\n";
77 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
78 $contents .= "</div class='openid_results'>";
79 include_once 'openid/wrapper.php';
84 if (!empty($_POST['checkid_immediate'])) {
85 $authRequest->setMode('checkid_immediate');
89 $sreg = new OpenID_Extension_SREG11(OpenID_Extension::REQUEST);
90 $sreg->set('required', 'email,firstname,lastname,nickname');
91 $sreg->set('optional', 'gender,dob');
92 $authRequest->addExtension($sreg);
95 $ax = new OpenID_Extension_AX(OpenID_Extension::REQUEST);
96 $ax->set('type.email', 'http://axschema.org/contact/email');
97 $ax->set('type.firstname', 'http://axschema.org/namePerson/first');
98 $ax->set('type.lastname', 'http://axschema.org/namePerson/last');
99 $ax->set('mode', 'fetch_request');
100 $ax->set('required', 'email,firstname,lastname');
101 $authRequest->addExtension($ax);
104 if (!empty($_POST['ui'])) {
105 $ui = new OpenID_Extension_UI(OpenID_Extension::REQUEST);
106 $ui->set('mode', 'popup');
107 $ui->set('language', 'en-US');
108 $authRequest->addExtension($ui);
112 if (!empty($_POST['oauth'])) {
113 $oauth = new OpenID_Extension_OAuth(OpenID_Extension::REQUEST);
114 $oauth->set('consumer', $_POST['oauth_consumer_key']);
115 $_SESSION['OAuth_consumer_key'] = $_POST['oauth_consumer_key'];
116 $_SESSION['OAuth_consumer_secret'] = $_POST['oauth_consumer_secret'];
118 $oauth->set('scope', $_POST['oauth_scope']);
119 $_SESSION['OAuth_scope'] = $_POST['oauth_scope'];
121 $_SESSION['OAuth_access_token_url'] = $_POST['oauth_access_token_url'];
122 $_SESSION['OAuth_access_token_method'] = $_POST['oauth_access_token_method'];
124 $authRequest->addExtension($oauth);
127 $url = $authRequest->getAuthorizeURL();
129 if (empty($_POST['debug'])) {
130 header("Location: $url");
135 if (isset($_SESSION['openid_url'])) {
136 $usid = $_SESSION['openid_url'];
137 unset($_SESSION['openid_url']);
142 unset($_SESSION['disable_associations']);
144 if (!count($_POST)) {
145 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
147 // I hate php sometimes
148 $queryString = file_get_contents('php://input');
151 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
152 $id = $message->get('openid.claimed_id');
153 $mode = $message->get('openid.mode');
156 $result = $o->verify(new Net_URL2($returnTo . '?' . $queryString), $message);
158 if ($result->success()) {
159 $status = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
160 $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
162 $status = "<tr><td>Status:</td><td><font color='red'>FAIL!";
163 $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
165 } catch (OpenID_Exception $e) {
166 $status = "<tr><td>Status:</td><td><font color='red'>EXCEPTION!";
167 $status .= " ({$e->getMessage()} : {$e->getCode()})</font></td></tr>";
170 // OAuth hyprid fetching access token
171 if (isset($_SESSION['OAuth_consumer_key'],
172 $_SESSION['OAuth_consumer_secret'],
173 $_SESSION['OAuth_access_token_url'],
174 $_SESSION['OAuth_access_token_method'])) {
177 $oauth = new OpenID_Extension_OAuth(OpenID_Extension::RESPONSE,
181 $consumerKey = $_SESSION['OAuth_consumer_key'];
182 $consumerSecret = $_SESSION['OAuth_consumer_key'];
183 $tokenURL = $_SESSION['OAuth_access_token_url'];
184 $tokenMethod = $_SESSION['OAuth_access_token_method'];
186 $oauthData = $oauth->getAccessToken($consumerKey,
192 } catch (Exception $e) {
196 $openid = $message->getArrayFormat();
198 $email = (isset($openid['openid.ext1.value.email'])) ? $openid['openid.ext1.value.email'] : null;
199 $email = (isset($openid['openid.ext2.value.email']) && !isset($email)) ? $openid['openid.ext2.value.email'] : $email;
200 $email = (isset($openid['openid.sreg.email']) && !isset($email)) ? $openid['openid.sreg.email'] : $email;
201 $email = (isset($openid['openid.ax.value.email']) && !isset($email)) ? $openid['openid.ax.value.email'] : $email;
202 $_SESSION['email'] = (isset($email)) ? $email : $GLOBALS['phorkie']['auth']['anonymousEmail'];
204 $name = (isset($openid['openid.ext1.value.firstname']) && isset($openid['openid.ext1.value.lastname'])) ? $openid['openid.ext1.value.firstname']." ".$openid['openid.ext1.value.lastname'] : null;
205 $name = (isset($openid['openid.sreg.firstname']) && isset($openid['openid.sreg.lastname']) && !isset($name)) ? $openid['openid.sreg.firstname']." ".$openid['openid.sreg.lastname'] : $name;
206 $name = (isset($openid['openid.sreg.nickname']) && !isset($name)) ? $openid['openid.sreg.nickname'] : $name;
207 $_SESSION['name'] = (isset($name)) ? $name : $_SERVER['REMOTE_ADDR'];
209 $_SESSION['identity'] = $openid['openid.identity'];
211 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SESSION['REQUEST_URI'];
212 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));