KaiRo bug 392 - Create an interstitial page to confirm the user to log in
[authserver.git] / authorize.php
index 45f0b032e07d2fc2bbe0f7865be485dadba03916..114064fb2ef396906bad35afefd7453fea7375a4 100644 (file)
@@ -6,7 +6,7 @@
 // Called e.g. as /authorize?response_type=code&client_id=testclient&state=f00bar&scope=email&redirect_uri=http%3A%2F%2Ffake.example.com%2F
 // This either redirects to the redirect URL with errors or success added as GET parameters,
 // or sends a HTML page asking for login / permission to scope (email is always granted in this system but not always for OAuth2 generically)
-// or sends errors as a JSOn document (hopefully shouldn't but seen that in testing).
+// or sends errors as a JSON document (hopefully shouldn't but seen that in testing).
 
 // Include the common auth system files (including the OAuth2 Server object).
 require_once(__DIR__.'/authsystem.inc.php');
@@ -18,13 +18,37 @@ $style = $head->appendElement('link');
 $style->setAttribute('rel', 'stylesheet');
 $style->setAttribute('href', 'authsystem.css');
 $head->appendJSFile('authsystem.js');
+$title->appendText('Authorization Request | KaiRo.at');
+$h1 = $body->appendElement('h1', 'KaiRo.at Authentication Server');
 
 $errors = $utils->checkForSecureConnection();
+$utils->sendSecurityHeaders();
+
+$para = $body->appendElement('p', _('This login system does not work without JavaScript. Please activate JavaScript for this site to log in.'));
+$para->setAttribute('id', 'jswarning');
+$para->setAttribute('class', 'warn');
 
 if (!count($errors)) {
   $session = $utils->initSession(); // Read session or create new session and set cookie.
-  $user = array('id' => 0, 'email' => '');
-  $pagetype = 'default';
+  if ($session['logged_in'] && (@$_GET['logout'] == 1)) {
+    $result = $db->prepare('UPDATE `auth_sessions` SET `logged_in` = FALSE WHERE `id` = :sessid;');
+    if (!$result->execute(array(':sessid' => $session['id']))) {
+      $utils->log('logout_failure', 'session: '.$session['id']);
+      $errors[] = _('Unexpected error while logging out.');
+    }
+    $session['logged_in'] = 0;
+  }
+  if (intval($session['user'])) {
+    $result = $db->prepare('SELECT `id`,`email`,`verify_hash` FROM `auth_users` WHERE `id` = :userid;');
+    $result->execute(array(':userid' => $session['user']));
+    $user = $result->fetch(PDO::FETCH_ASSOC);
+    if (!$user['id']) {
+      $utils->log('user_read_failure', 'user: '.$session['user']);
+    }
+  }
+  else {
+    $user = array('id' => 0, 'email' => '');
+  }
   if (is_null($session)) {
     $errors[] = _('The session system is not working. Please <a href="https://www.kairo.at/contact">contact KaiRo.at</a> and tell the team about this.');
   }
@@ -39,15 +63,8 @@ if (!count($errors)) {
       exit();
     }
 
-    // Display an authorization form.
-    if (empty($_POST)) {
-      $title->appendText('Authorization Request | KaiRo.at');
-      $h1 = $body->appendElement('h1', 'KaiRo.at Authentication Server');
-
-      $para = $body->appendElement('p', _('This login system does not work without JavaScript. Please activate JavaScript for this site to log in.'));
-      $para->setAttribute('id', 'jswarning');
-      $para->setAttribute('class', 'warn');
-
+    if (empty($_POST) && (@$request->query['scope'] != 'email')) {
+      // Display an authorization form (unless the scope is email, which we handle as a login request below).
       $para = $body->appendElement('p', sprintf(_('Hi %s!'), $user['email']));
       $para->setAttribute('class', 'userwelcome');
 
@@ -60,10 +77,34 @@ if (!count($errors)) {
       $submit = $form->appendInputSubmit(_('no'));
       $submit->setAttribute('name', 'authorized');
     }
+    elseif (empty($_POST) && (@$request->query['scope'] == 'email')) {
+      // Display an interstitial page for a login  when we have email scope (verified email for logging in).
+      $para = $body->appendElement('p', sprintf(_('Sign in to %s using…'), $request->query['client_id'])); // XXX: put domain name from redirect URI on there instead
+      $para->setAttribute('class', 'signinwelcome');
+      $form = $body->appendForm('', 'POST', 'loginauthform');
+      $form->setAttribute('id', 'loginauthform');
+      $form->setAttribute('class', 'loginarea');
+      $form->appendInputRadio('user_email', 'uemail_'.md5($user['email']), $user['email'], $user['email'] == $user['email']);
+      $form->appendLabel('uemail_'.md5($user['email']), $user['email']);
+      $para = $form->appendElement('p');
+      $para->setAttribute('class', 'small otheremaillinks');
+      $link = $para->appendLink('#', _('Add another email address'));
+      $link->setAttribute('id', 'addanotheremail'); // Makes the JS put the right functionality onto the link.
+      $para->appendText(' ');
+      $link = $para->appendLink('#', _('This is not me'));
+      $link->setAttribute('id', 'isnotme'); // Makes the JS put the right functionality onto the link.
+      $authinput = $form->appendInputHidden('authorized', 'yes');
+      $authinput->setAttribute('id', 'isauthorized');
+      $submit = $form->appendInputSubmit(_('Sign in'));
+      $para = $form->appendElement('p');
+      $para->setAttribute('class', 'small');
+      $link = $para->appendLink('#', _('Cancel'));
+      $link->setAttribute('id', 'cancelauth'); // Makes the JS put the right functionality onto the link.
+    }
     else {
       // Handle authorize request, forwarding code in GET parameters if the user has authorized your client.
-      $is_authorized = ($_POST['authorized'] === 'yes');
-      $server->handleAuthorizeRequest($request, $response, $is_authorized);
+      $is_authorized = (@$_POST['authorized'] === 'yes');
+      $server->handleAuthorizeRequest($request, $response, $is_authorized, $user['id']);
       /* For testing only
       if ($is_authorized) {
         // this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
@@ -79,6 +120,13 @@ if (!count($errors)) {
     // Display login/register form.
     $para = $body->appendElement('p', _('You need to log in or register to continue.'));
     $para->setAttribute('class', 'logininfo');
+    $utils->appendLoginForm($body, $session, $user);
+    // Save the request in the session so we can get back to fulfilling it.
+    $result = $db->prepare('UPDATE `auth_sessions` SET `saved_redirect` = :redir WHERE `id` = :sessid;');
+    $saved_request = str_replace('&logout=1', '', $_SERVER['REQUEST_URI']); // Make sure to strip a logout to not get into a loop.
+    if (!$result->execute(array(':redir' => $saved_request, ':sessid' => $session['id']))) {
+      $utils->log('redir_save_failure', 'session: '.$session['id'].', redirect: '.$saved_request);
+    }
   }
 }