6 * @author Christian Weiske <cweiske@cweiske.de>
8 header('HTTP/1.0 500 Internal Server Error');
9 require 'www-header.php';
11 function getOrCreateUser($mode, $name, $imageurl, $email)
13 if ($mode == 'anonymous') {
22 if ($imageurl == '') {
23 $imageurl = getImageUrl($email);
26 $storage = new Storage();
27 $id = $storage->findUser($name, $imageurl);
31 $id = $storage->createUser($name, $imageurl);
35 function getImageUrl($email)
38 return Urls::userImg((object)['user_imageurl' => '']);
41 header('IndieAuth: authorization_endpoint');
42 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
43 if (count($_GET) == 0) {
44 //no parameters - show the index page
45 header('HTTP/1.0 200 OK');
46 header('Content-Type: text/html');
47 render('auth-index', ['baseurl' => Urls::full('/')]);
51 $me = verifyUrlParameter($_GET, 'me');
52 $redirect_uri = verifyUrlParameter($_GET, 'redirect_uri');
53 $client_id = verifyUrlParameter($_GET, 'client_id');
54 $state = getOptionalParameter($_GET, 'state', null);
55 $response_type = getOptionalParameter($_GET, 'response_type', 'id');
56 $scope = getOptionalParameter($_GET, 'scope', null);
59 'mode' => 'anonymous',
63 $userId = Urls::userId($me);
64 if ($userId !== null) {
65 $storage = new Storage();
66 $rowUser = $storage->getUser($userId);
67 if ($rowUser !== null) {
69 $id['name'] = $rowUser->user_name;
70 $id['imageurl'] = $rowUser->user_imageurl;
71 if ($id['imageurl'] == Urls::userImg()) {
77 //let the user choose his identity
78 header('HTTP/1.0 200 OK');
83 'redirect_uri' => $redirect_uri,
84 'client_id' => $client_id,
86 'response_type' => $response_type,
90 'formaction' => '/auth.php?action=login',
94 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
95 if (isset($_GET['action']) && $_GET['action'] == 'login') {
97 $auth = $_POST['auth'];
98 $redirect_uri = verifyUrlParameter($auth, 'redirect_uri');
99 $client_id = verifyUrlParameter($auth, 'client_id');
100 $state = getOptionalParameter($auth, 'state', null);
101 $response_type = getOptionalParameter($auth, 'response_type', 'id');
102 $scope = getOptionalParameter($auth, 'scope', null);
105 verifyParameter($id, 'mode');
107 $userId = getOrCreateUser(
108 $id['mode'], trim($id['name']), trim($id['imageurl']),
111 $me = Urls::full(Urls::user($userId));
113 $code = base64_encode(
116 'emoji' => '\360\237\222\251',
119 'signature' => 'FIXME',
124 //redirect back to client
125 $url = new \Net_URL2($redirect_uri);
126 $url->setQueryVariable('code', $code);
127 $url->setQueryVariable('me', $me);
128 $url->setQueryVariable('state', $state);
129 header('Location: ' . $url->getURL());
132 //auth code verification
133 $code = base64_decode(verifyParameter($_POST, 'code'));
134 $redirect_uri = verifyUrlParameter($_POST, 'redirect_uri');
135 $client_id = verifyUrlParameter($_POST, 'client_id');
136 $state = getOptionalParameter($_POST, 'state', null);
138 parse_str($code, $codeParts);
139 $emoji = verifyParameter($codeParts, 'emoji');
140 $signature = verifyParameter($codeParts, 'signature');
141 $me = verifyUrlParameter($codeParts, 'me');
142 if ($emoji != '\360\237\222\251') {
143 error('Dog poo missing');
145 if ($signature != 'FIXME') {
146 error('Invalid signature');
148 header('HTTP/1.0 200 OK');
149 header('Content-type: application/x-www-form-urlencoded');
150 echo http_build_query(['me' => $me]);
153 } else if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
154 //indieauth.com makes a HEAD request at first
155 header('HTTP/1.0 200 OK');
158 error('Unsupported HTTP request method');