2a7e0a5a12b4cdb0f37cc265cfc0cb8ed33377ab
[authserver.git] / authorize.php
1 <?php
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 // Called e.g. as /authorize?response_type=code&client_id=testclient&state=f00bar&scope=email&redirect_uri=http%3A%2F%2Ffake.example.com%2F
7 // This either redirects to the redirect URL with errors or success added as GET parameters,
8 // or sends a HTML page asking for login / permission to scope (email is always granted in this system but not always for OAuth2 generically)
9 // or sends errors as a JSOn document (hopefully shouldn't but seen that in testing).
10
11 // Include the common auth system files (including the OAuth2 Server object).
12 require_once(__DIR__.'/authsystem.inc.php');
13
14 // Start HTML document as a DOM object.
15 extract(ExtendedDocument::initHTML5()); // sets $document, $html, $head, $title, $body
16 $document->formatOutput = true; // we want a nice output
17 $style = $head->appendElement('link');
18 $style->setAttribute('rel', 'stylesheet');
19 $style->setAttribute('href', 'authsystem.css');
20 $head->appendJSFile('authsystem.js');
21 $title->appendText('Authorization Request | KaiRo.at');
22 $h1 = $body->appendElement('h1', 'KaiRo.at Authentication Server');
23
24 $errors = $utils->checkForSecureConnection();
25
26 $para = $body->appendElement('p', _('This login system does not work without JavaScript. Please activate JavaScript for this site to log in.'));
27 $para->setAttribute('id', 'jswarning');
28 $para->setAttribute('class', 'warn');
29
30 if (!count($errors)) {
31   $session = $utils->initSession(); // Read session or create new session and set cookie.
32   if (intval($session['user'])) {
33     $result = $db->prepare('SELECT `id`,`email`,`verify_hash` FROM `auth_users` WHERE `id` = :userid;');
34     $result->execute(array(':userid' => $session['user']));
35     $user = $result->fetch(PDO::FETCH_ASSOC);
36     if (!$user['id']) {
37       $utils->log('user_read_failure', 'user: '.$session['user']);
38     }
39   }
40   else {
41     $user = array('id' => 0, 'email' => '');
42   }
43   $pagetype = 'default';
44   if (is_null($session)) {
45     $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.');
46   }
47   elseif ($session['logged_in']) {
48     // We are logged in, process authorization request.
49     $request = OAuth2\Request::createFromGlobals();
50     $response = new OAuth2\Response();
51
52     // Validate the authorize request.
53     if (!$server->validateAuthorizeRequest($request, $response)) {
54       $response->send();
55       exit();
56     }
57
58     // Display an authorization form.
59     if (empty($_POST)) {
60       $para = $body->appendElement('p', sprintf(_('Hi %s!'), $user['email']));
61       $para->setAttribute('class', 'userwelcome');
62
63       $form = $body->appendForm('', 'POST', 'authform');
64       $form->setAttribute('id', 'authform');
65       $form->appendElement('p', sprintf(_('Do you authorize %s to access %s?'), $request->query['client_id'], $request->query['scope']));
66       $submit = $form->appendInputSubmit(_('yes'));
67       $submit->setAttribute('name', 'authorized');
68       $form->appendText(' ');
69       $submit = $form->appendInputSubmit(_('no'));
70       $submit->setAttribute('name', 'authorized');
71     }
72     else {
73       // Handle authorize request, forwarding code in GET parameters if the user has authorized your client.
74       $is_authorized = ($_POST['authorized'] === 'yes');
75       $server->handleAuthorizeRequest($request, $response, $is_authorized);
76       /* For testing only
77       if ($is_authorized) {
78         // 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         $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
80         exit("SUCCESS! Authorization Code: $code");
81       }
82       */
83       $response->send();
84       exit();
85     }
86   }
87   else {
88     // Display login/register form.
89     $para = $body->appendElement('p', _('You need to log in or register to continue.'));
90     $para->setAttribute('class', 'logininfo');
91     $utils->appendLoginForm($body, $session, $user);
92     // Save the request in the session so we can get back to fulfilling it.
93     $result = $db->prepare('UPDATE `auth_sessions` SET `saved_redirect` = :redir WHERE `id` = :sessid;');
94     if (!$result->execute(array(':redir' => $_SERVER['REQUEST_URI'], ':sessid' => $session['id']))) {
95       $utils->log('redir_save_failure', 'session: '.$session['id'].', redirect: '.$_SERVER['REQUEST_URI']);
96     }
97   }
98 }
99
100 if (count($errors)) {
101   $body->appendElement('p', ((count($errors) <= 1)
102                             ?_('The following error was detected')
103                             :_('The following errors were detected')).':');
104   $list = $body->appendElement('ul');
105   $list->setAttribute('class', 'flat warn');
106   foreach ($errors as $msg) {
107     $item = $list->appendElement('li', $msg);
108   }
109   $body->appendButton(_('Back'), 'history.back();');
110 }
111
112 // Send HTML to client.
113 print($document->saveHTML());
114 ?>