PEAR Coding Standards intermediate update for files changed
[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['identifier'])) {
45     $identifier = $_POST['identifier'];
46 } else if (isset($_SESSION['identifier'])) {
47     $identifier = $_SESSION['identifier'];
48 } else {
49     $identifier = null;
50 }
51
52 try {
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';
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['identifier'] = $identifier;
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     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);
94     }
95
96     // AX
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);
105     }
106
107     // UI
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);
113     }
114
115     // OAuth
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'];
121
122         $oauth->set('scope', $_POST['oauth_scope']);
123         $_SESSION['OAuth_scope'] = $_POST['oauth_scope'];
124
125         $_SESSION['OAuth_access_token_url']    = $_POST['oauth_access_token_url'];
126         $_SESSION['OAuth_access_token_method'] = $_POST['oauth_access_token_method'];
127
128         $authRequest->addExtension($oauth);
129     }
130     
131     $url = $authRequest->getAuthorizeURL();
132     
133     if (empty($_POST['debug'])) {
134         header("Location: $url");
135         exit;
136     }
137     
138 } else {
139     if (isset($_SESSION['identifier'])) {
140         $usid = $_SESSION['identifier'];
141         unset($_SESSION['identifier']);
142     } else {
143         $usid = null;
144     }
145
146     unset($_SESSION['disable_associations']);
147
148     if (!count($_POST)) {
149         list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
150     } else {
151         // I hate php sometimes
152         $queryString = file_get_contents('php://input');
153     }
154
155     $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
156     $id      = $message->get('openid.claimed_id');
157     $mode    = $message->get('openid.mode');
158
159     try {
160         $result = $o->verify(new Net_URL2($returnTo . '?' . $queryString), $message);
161
162         if ($result->success()) {
163             $status  = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
164             $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
165         } else {
166             $status  = "<tr><td>Status:</td><td><font color='red'>FAIL!";
167             $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
168         }
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>";
172     }
173
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'])) {
179
180         try {
181             $oauth = new OpenID_Extension_OAuth(OpenID_Extension::RESPONSE,
182                                                 $message);
183
184             // Fix line lengths.
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'];
189
190             $oauthData = $oauth->getAccessToken($consumerKey,
191                                                 $consumerSecret,
192                                                 $tokenURL,
193                                                 array(),
194                                                 $tokenMethod);
195
196         } catch (Exception $e) {
197         }
198     }
199
200     $openid = $message->getArrayFormat();
201
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'];
207
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'];
212
213     $_SESSION['identity'] = $openid['openid.identity'];
214
215     $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SESSION['REQUEST_URI'];
216     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
217     exit;
218 }
219
220 ?>