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['identifier'])) {
45 $identifier = $_POST['identifier'];
46 } else if (isset($_SESSION['identifier'])) {
47 $identifier = $_SESSION['identifier'];
53 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
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['identifier'] = $identifier;
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 if (!empty($_POST['sreg'])) {
90 $sreg = new OpenID_Extension_SREG11(OpenID_Extension::REQUEST);
91 $sreg->set('required', 'email,firstname,lastname,nickname');
92 $sreg->set('optional', 'gender,dob');
93 $authRequest->addExtension($sreg);
97 if (!empty($_POST['ax'])) {
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);
108 if (!empty($_POST['ui'])) {
109 $ui = new OpenID_Extension_UI(OpenID_Extension::REQUEST);
110 $ui->set('mode', 'popup');
111 $ui->set('language', 'en-US');
112 $authRequest->addExtension($ui);
116 if (!empty($_POST['oauth'])) {
117 $oauth = new OpenID_Extension_OAuth(OpenID_Extension::REQUEST);
118 $oauth->set('consumer', $_POST['oauth_consumer_key']);
119 $_SESSION['OAuth_consumer_key'] = $_POST['oauth_consumer_key'];
120 $_SESSION['OAuth_consumer_secret'] = $_POST['oauth_consumer_secret'];
122 $oauth->set('scope', $_POST['oauth_scope']);
123 $_SESSION['OAuth_scope'] = $_POST['oauth_scope'];
125 $_SESSION['OAuth_access_token_url'] = $_POST['oauth_access_token_url'];
126 $_SESSION['OAuth_access_token_method'] = $_POST['oauth_access_token_method'];
128 $authRequest->addExtension($oauth);
131 $url = $authRequest->getAuthorizeURL();
133 if (empty($_POST['debug'])) {
134 header("Location: $url");
139 if (isset($_SESSION['identifier'])) {
140 $usid = $_SESSION['identifier'];
141 unset($_SESSION['identifier']);
146 unset($_SESSION['disable_associations']);
148 if (!count($_POST)) {
149 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
151 // I hate php sometimes
152 $queryString = file_get_contents('php://input');
155 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
156 $id = $message->get('openid.claimed_id');
157 $mode = $message->get('openid.mode');
160 $result = $o->verify(new Net_URL2($returnTo . '?' . $queryString), $message);
162 if ($result->success()) {
163 $status = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
164 $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
166 $status = "<tr><td>Status:</td><td><font color='red'>FAIL!";
167 $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
169 } catch (OpenID_Exception $e) {
170 $status = "<tr><td>Status:</td><td><font color='red'>EXCEPTION!";
171 $status .= " ({$e->getMessage()} : {$e->getCode()})</font></td></tr>";
174 // OAuth hyprid fetching access token
175 if (isset($_SESSION['OAuth_consumer_key'],
176 $_SESSION['OAuth_consumer_secret'],
177 $_SESSION['OAuth_access_token_url'],
178 $_SESSION['OAuth_access_token_method'])) {
181 $oauth = new OpenID_Extension_OAuth(OpenID_Extension::RESPONSE,
185 $consumerKey = $_SESSION['OAuth_consumer_key'];
186 $consumerSecret = $_SESSION['OAuth_consumer_key'];
187 $tokenURL = $_SESSION['OAuth_access_token_url'];
188 $tokenMethod = $_SESSION['OAuth_access_token_method'];
190 $oauthData = $oauth->getAccessToken($consumerKey,
196 } catch (Exception $e) {
200 $openid = $message->getArrayFormat();
202 $email = (isset($openid['openid.ext1.value.email'])) ? $openid['openid.ext1.value.email'] : null;
203 $email = (isset($openid['openid.ext2.value.email']) && !isset($email)) ? $openid['openid.ext2.value.email'] : $email;
204 $email = (isset($openid['openid.sreg.email']) && !isset($email)) ? $openid['openid.sreg.email'] : $email;
205 $email = (isset($openid['openid.ax.value.email']) && !isset($email)) ? $openid['openid.ax.value.email'] : $email;
206 $_SESSION['email'] = (isset($email)) ? $email : $GLOBALS['phorkie']['auth']['anonymousEmail'];
208 $name = (isset($openid['openid.ext1.value.firstname']) && isset($openid['openid.ext1.value.lastname'])) ? $openid['openid.ext1.value.firstname']." ".$openid['openid.ext1.value.lastname'] : null;
209 $name = (isset($openid['openid.sreg.firstname']) && isset($openid['openid.sreg.lastname']) && !isset($name)) ? $openid['openid.sreg.firstname']." ".$openid['openid.sreg.lastname'] : $name;
210 $name = (isset($openid['openid.sreg.nickname']) && !isset($name)) ? $openid['openid.sreg.nickname'] : $name;
211 $_SESSION['name'] = (isset($name)) ? $name : $_SERVER['REMOTE_ADDR'];
213 $_SESSION['identity'] = $openid['openid.identity'];
215 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SESSION['REQUEST_URI'];
216 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));