29d7b37c26d9aaba4def0f5e766bd716899cbcbf
[phorkie.git] / www / auth.php
1 <?php
2 /**
3  * OpenID 
4  * 
5  * PHP Version 5.2.0+
6  * 
7  * @category  Auth
8  * @package   OpenID
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
13  */
14
15 // A tool for testing Relying Party functionality
16 set_include_path(
17     __DIR__ . '/../../src/'
18     . PATH_SEPARATOR . get_include_path()
19 );
20
21 require_once 'www-header.php';
22 require_once 'openid/config.php';
23
24
25 if (isset($_REQUEST['logout'])) {
26     unset($_SESSION);
27     session_destroy();
28     $redirect = 'http://' . $_SERVER['HTTP_HOST'];
29     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
30     exit;
31 }
32
33 if (!count($_GET) && !count($_POST)) {
34     $redirect = 'http://' . $_SERVER['HTTP_HOST'] . "/login";
35     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
36     exit;
37 }
38
39 // Hackaround Non-Javascript Login Page
40 if (!count($_POST) && isset($_GET['start'])) {
41     $_POST = $_GET;
42 }
43
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'];
48 } else {
49     $openid_url = null;
50 }
51
52 try {
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';
59     exit;
60 }
61
62 if (!empty($_POST['disable_associations']) || !empty($_SESSION['disable_associations'])) {
63     $o->disableAssociations();
64     $_SESSION['disable_associations'] = true;
65 }
66
67 $log = new OpenID_Observer_Log;
68 OpenID::attach($log);
69
70 if (isset($_POST['start'])) {
71
72     $_SESSION['openid_url'] = $openid_url;
73     try {
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';
80         exit;
81     }
82
83     // checkid_immediate
84     if (!empty($_POST['checkid_immediate'])) {
85         $authRequest->setMode('checkid_immediate');
86     }
87
88     // SREG
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);
93
94     // AX
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);
102
103     // UI
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);
109     }
110
111     // OAuth
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'];
117
118         $oauth->set('scope', $_POST['oauth_scope']);
119         $_SESSION['OAuth_scope'] = $_POST['oauth_scope'];
120
121         $_SESSION['OAuth_access_token_url']    = $_POST['oauth_access_token_url'];
122         $_SESSION['OAuth_access_token_method'] = $_POST['oauth_access_token_method'];
123
124         $authRequest->addExtension($oauth);
125     }
126     
127     $url = $authRequest->getAuthorizeURL();
128     
129     if (empty($_POST['debug'])) {
130         header("Location: $url");
131         exit;
132     }
133     
134 } else {
135     if (isset($_SESSION['openid_url'])) {
136         $usid = $_SESSION['openid_url'];
137         unset($_SESSION['openid_url']);
138     } else {
139         $usid = null;
140     }
141
142     unset($_SESSION['disable_associations']);
143
144     if (!count($_POST)) {
145         list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
146     } else {
147         // I hate php sometimes
148         $queryString = file_get_contents('php://input');
149     }
150
151     $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
152     $id      = $message->get('openid.claimed_id');
153     $mode    = $message->get('openid.mode');
154
155     try {
156         $result = $o->verify(new Net_URL2($returnTo . '?' . $queryString), $message);
157
158         if ($result->success()) {
159             $status  = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
160             $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
161         } else {
162             $status  = "<tr><td>Status:</td><td><font color='red'>FAIL!";
163             $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
164         }
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>";
168     }
169
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'])) {
175
176         try {
177             $oauth = new OpenID_Extension_OAuth(OpenID_Extension::RESPONSE,
178                                                 $message);
179
180             // Fix line lengths.
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'];
185
186             $oauthData = $oauth->getAccessToken($consumerKey,
187                                                 $consumerSecret,
188                                                 $tokenURL,
189                                                 array(),
190                                                 $tokenMethod);
191
192         } catch (Exception $e) {
193         }
194     }
195
196     $openid = $message->getArrayFormat();
197
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'];
203
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'];
208
209     $_SESSION['identity'] = $openid['openid.identity'];
210
211     $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SESSION['REQUEST_URI'];
212     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
213     exit;
214 }
215
216 ?>