3 * IndieAuth to OpenID proxy.
4 * Proxies IndieAuth authorization requests to one's OpenID server
8 * @package indieauth-openid
9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
11 * @link http://indiewebcamp.com/login-brainstorming
12 * @link http://indiewebcamp.com/authorization-endpoint
13 * @link http://indiewebcamp.com/auth-brainstorming
14 * @link https://indieauth.com/developers
16 header('IndieAuth: authorization_endpoint');
17 if (($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD')
24 require_once 'Net/URL2.php';
25 require_once 'OpenID.php';
26 require_once 'OpenID/RelyingParty.php';
27 require_once 'OpenID/Message.php';
28 require_once 'OpenID/Exception.php';
32 $db = new PDO('sqlite:' . __DIR__ . '/../data/tokens.sq3');
33 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
34 $db->exec("CREATE TABLE IF NOT EXISTS authtokens(
43 $stmt = $db->prepare('DELETE FROM authtokens WHERE created < :created');
44 $stmt->execute(array(':created' => date('c', time() - 60)));
49 function create_token($me, $redirect_uri, $client_id, $state)
51 $code = base64_encode(openssl_random_pseudo_bytes(32));
54 'INSERT INTO authtokens (code, me, redirect_uri, client_id, state, created)'
55 . ' VALUES(:code, :me, :redirect_uri, :client_id, :state, :created)'
60 ':redirect_uri' => $redirect_uri,
61 ':client_id' => $client_id,
62 ':state' => (string) $state,
63 ':created' => date('c')
69 function validate_token($code, $redirect_uri, $client_id, $state)
73 'SELECT me FROM authtokens WHERE'
75 . ' AND redirect_uri = :redirect_uri'
76 . ' AND client_id = :client_id'
77 . ' AND state = :state'
78 . ' AND created >= :created'
83 ':redirect_uri' => $redirect_uri,
84 ':client_id' => $client_id,
85 ':state' => (string) $state,
86 ':created' => date('c', time() - 60)
89 $row = $stmt->fetch(PDO::FETCH_ASSOC);
91 $stmt = $db->prepare('DELETE FROM authtokens WHERE code = :code');
92 $stmt->execute(array(':code' => $code));
102 header('HTTP/1.0 400 Bad Request');
103 header('Content-type: text/plain; charset=utf-8');
108 function verifyUrlParameter($givenParams, $paramName)
110 if (!isset($givenParams[$paramName])) {
111 error('"' . $paramName . '" parameter missing');
113 $url = parse_url($givenParams[$paramName]);
114 if (!isset($url['scheme'])) {
115 error('Invalid URL in "' . $paramName . '" parameter: scheme missing');
117 if (!isset($url['host'])) {
118 error('Invalid URL in "' . $paramName . '" parameter: host missing');
121 return $givenParams[$paramName];
124 function getBaseUrl()
126 if (!isset($_SERVER['REQUEST_SCHEME'])) {
127 $_SERVER['REQUEST_SCHEME'] = 'http';
129 $file = preg_replace('/[?#].*$/', '', $_SERVER['REQUEST_URI']);
130 return $_SERVER['REQUEST_SCHEME'] . '://'
131 . $_SERVER['HTTP_HOST']
136 $returnTo = getBaseUrl();
137 $realm = getBaseUrl();
139 if (isset($_GET['openid_mode']) && $_GET['openid_mode'] != '') {
140 //verify openid response
141 if (!count($_POST)) {
142 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
144 $queryString = file_get_contents('php://input');
147 $message = new \OpenID_Message($queryString, \OpenID_Message::FORMAT_HTTP);
148 $id = $message->get('openid.claimed_id');
149 if (OpenID::normalizeIdentifier($id) != OpenID::normalizeIdentifier($_SESSION['me'])) {
152 'Given identity URL "%s" and claimed OpenID "%s" do not match',
158 $o = new \OpenID_RelyingParty($returnTo, $realm, $_SESSION['me']);
159 $result = $o->verify(new \Net_URL2($returnTo . '?' . $queryString), $message);
161 if ($result->success()) {
162 $token = create_token(
163 $_SESSION['me'], $_SESSION['redirect_uri'],
164 $_SESSION['client_id'], $_SESSION['state']
166 //redirect to indieauth
167 $url = new Net_URL2($_SESSION['redirect_uri']);
168 $url->setQueryVariable('code', $token);
169 $url->setQueryVariable('me', $_SESSION['me']);
170 $url->setQueryVariable('state', $_SESSION['state']);
171 header('Location: ' . $url->getURL());
174 error('Error verifying OpenID login: ' . $result->getAssertionMethod());
176 } catch (OpenID_Exception $e) {
177 error('Error verifying OpenID login: ' . $e->getMessage());
181 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
182 $me = verifyUrlParameter($_GET, 'me');
183 $redirect_uri = verifyUrlParameter($_GET, 'redirect_uri');
184 $client_id = verifyUrlParameter($_GET, 'client_id');
186 if (isset($_GET['state'])) {
187 $state = $_GET['state'];
189 $response_type = 'id';
190 if (isset($_GET['response_type'])) {
191 $response_type = $_GET['response_type'];
193 if ($response_type != 'id') {
194 error('unsupported response_type: ' . $response_type);
197 $_SESSION['me'] = $me;
198 $_SESSION['redirect_uri'] = $redirect_uri;
199 $_SESSION['client_id'] = $client_id;
200 $_SESSION['state'] = $state;
203 $o = new \OpenID_RelyingParty($returnTo, $realm, $me);
204 //if you get timeouts (errors like
205 // OpenID error: Request timed out after 3 second(s)
206 //) then uncomment the following line which disables
208 //$o->setRequestOptions(array('follow_redirects' => true));
209 $authRequest = $o->prepare();
210 $url = $authRequest->getAuthorizeURL();
211 header("Location: $url");
213 } catch (OpenID_Exception $e) {
214 error('OpenID error: ' . $e->getMessage());
216 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
217 $redirect_uri = verifyUrlParameter($_POST, 'redirect_uri');
218 $client_id = verifyUrlParameter($_POST, 'client_id');
220 if (isset($_POST['state'])) {
221 $state = $_POST['state'];
223 if (!isset($_POST['code'])) {
224 error('"code" parameter missing');
226 $token = $_POST['code'];
228 $me = validate_token($token, $redirect_uri, $client_id, $state);
230 header('HTTP/1.0 400 Bad Request');
231 echo "Validating token failed\n";
234 header('Content-type: application/x-www-form-urlencoded');
235 echo 'me=' . urlencode($me);