ignore teporary file copies
[authserver.git] / app / authorize.php
CommitLineData
e6624d81
RK
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
77f0f9ff
RK
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)
ea0452ad 9// or sends errors as a JSON document (hopefully shouldn't but seen that in testing).
e6624d81 10
133aecbe
RK
11// Include the common auth system files (including the OAuth2 Server object).
12require_once(__DIR__.'/authsystem.inc.php');
e6624d81 13
b680cefd
RK
14// Start HTML document as a DOM object.
15extract(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');
409b55f4
RK
21$title->appendText('Authorization Request | KaiRo.at');
22$h1 = $body->appendElement('h1', 'KaiRo.at Authentication Server');
b680cefd 23
77f0f9ff 24$errors = $utils->checkForSecureConnection();
b0e48c35 25$utils->sendSecurityHeaders();
77f0f9ff 26
409b55f4
RK
27$para = $body->appendElement('p', _('This login system does not work without JavaScript. Please activate JavaScript for this site to log in.'));
28$para->setAttribute('id', 'jswarning');
29$para->setAttribute('class', 'warn');
30
b680cefd
RK
31if (!count($errors)) {
32 $session = $utils->initSession(); // Read session or create new session and set cookie.
b217e836
RK
33 if ($session['logged_in'] && (@$_GET['logout'] == 1)) {
34 $result = $db->prepare('UPDATE `auth_sessions` SET `logged_in` = FALSE WHERE `id` = :sessid;');
35 if (!$result->execute(array(':sessid' => $session['id']))) {
36 $utils->log('logout_failure', 'session: '.$session['id']);
37 $errors[] = _('Unexpected error while logging out.');
38 }
39 $session['logged_in'] = 0;
40 }
409b55f4 41 if (intval($session['user'])) {
60e46184 42 $result = $db->prepare('SELECT `id`,`email`,`verify_hash`,`group_id` FROM `auth_users` WHERE `id` = :userid;');
409b55f4
RK
43 $result->execute(array(':userid' => $session['user']));
44 $user = $result->fetch(PDO::FETCH_ASSOC);
45 if (!$user['id']) {
46 $utils->log('user_read_failure', 'user: '.$session['user']);
47 }
48 }
49 else {
50 $user = array('id' => 0, 'email' => '');
51 }
b680cefd 52 if (is_null($session)) {
9cab985c 53 $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.');
b680cefd
RK
54 }
55 elseif ($session['logged_in']) {
56 // We are logged in, process authorization request.
57 $request = OAuth2\Request::createFromGlobals();
58 $response = new OAuth2\Response();
e6624d81 59
b680cefd
RK
60 // Validate the authorize request.
61 if (!$server->validateAuthorizeRequest($request, $response)) {
62 $response->send();
63 exit();
64 }
77f0f9ff 65
3ae47861 66 if (empty($_POST) && (@$request->query['scope'] != 'email')) {
b217e836 67 // Display an authorization form (unless the scope is email, which we handle as a login request below).
b680cefd
RK
68 $para = $body->appendElement('p', sprintf(_('Hi %s!'), $user['email']));
69 $para->setAttribute('class', 'userwelcome');
70
71 $form = $body->appendForm('', 'POST', 'authform');
72 $form->setAttribute('id', 'authform');
c4e0aceb
RK
73 $domain_name = parse_url($request->query['redirect_uri'], PHP_URL_HOST);
74 if (!strlen($domain_name)) { $domain_name = $request->query['client_id']; }
75 $form->appendElement('p', sprintf(_('Do you authorize %s to access %s?'), $domain_name, $request->query['scope']));
9cab985c
RK
76 $authinput = $form->appendInputHidden('authorized', 'yes');
77 $authinput->setAttribute('id', 'isauthorized');
78 $submit = $form->appendInputSubmit(_('Yes'));
b680cefd 79 $form->appendText(' ');
9cab985c
RK
80 $button = $form->appendButton(_('No'));
81 $button->setAttribute('id', 'cancelauth');
b680cefd 82 }
b217e836
RK
83 elseif (empty($_POST) && (@$request->query['scope'] == 'email')) {
84 // Display an interstitial page for a login when we have email scope (verified email for logging in).
c4e0aceb
RK
85 $domain_name = parse_url($request->query['redirect_uri'], PHP_URL_HOST);
86 if (!strlen($domain_name)) { $domain_name = $request->query['client_id']; }
87 $para = $body->appendElement('p', sprintf(_('Sign in to %s using…'), $domain_name));
b217e836 88 $para->setAttribute('class', 'signinwelcome');
9cab985c
RK
89 $form = $body->appendForm('', 'POST', 'authform');
90 $form->setAttribute('id', 'authform');
b217e836 91 $form->setAttribute('class', 'loginarea');
60e46184
RK
92 $ulist = $form->appendElement('ul');
93 $ulist->setAttribute('class', 'flat emaillist');
94 $emails = $utils->getGroupedEmails($user['group_id']);
95 if (!count($emails)) { $emails = array($user['email']); }
96 foreach ($emails as $email) {
97 $litem = $ulist->appendElement('li');
98 $litem->appendInputRadio('user_email', 'uemail_'.md5($email), $email, $email == $user['email']);
99 $litem->appendLabel('uemail_'.md5($email), $email);
100 }
b217e836
RK
101 $para = $form->appendElement('p');
102 $para->setAttribute('class', 'small otheremaillinks');
103 $link = $para->appendLink('#', _('Add another email address'));
104 $link->setAttribute('id', 'addanotheremail'); // Makes the JS put the right functionality onto the link.
105 $para->appendText(' ');
106 $link = $para->appendLink('#', _('This is not me'));
107 $link->setAttribute('id', 'isnotme'); // Makes the JS put the right functionality onto the link.
108 $authinput = $form->appendInputHidden('authorized', 'yes');
109 $authinput->setAttribute('id', 'isauthorized');
110 $submit = $form->appendInputSubmit(_('Sign in'));
111 $para = $form->appendElement('p');
112 $para->setAttribute('class', 'small');
113 $link = $para->appendLink('#', _('Cancel'));
114 $link->setAttribute('id', 'cancelauth'); // Makes the JS put the right functionality onto the link.
60e46184 115 $utils->setRedirect($session, $_SERVER['REQUEST_URI']);
b217e836 116 }
b680cefd 117 else {
60e46184
RK
118 // Switch to different user if we selected a different email within the group.
119 if (strlen(@$_POST['user_email']) && ($_POST['user_email'] != $user['email'])) {
120 $result = $db->prepare('SELECT `id`, `pwdhash`, `email`, `status`, `verify_hash`,`group_id` FROM `auth_users` WHERE `group_id` = :groupid AND `email` = :email;');
121 $result->execute(array(':groupid' => $user['group_id'], ':email' => $_POST['user_email']));
122 $newuser = $result->fetch(PDO::FETCH_ASSOC);
123 if ($newuser) {
124 $user = $newuser;
125 $session = $utils->getLoginSession($user['id'], $session);
126 }
127 }
b680cefd 128 // Handle authorize request, forwarding code in GET parameters if the user has authorized your client.
b217e836 129 $is_authorized = (@$_POST['authorized'] === 'yes');
ea0452ad 130 $server->handleAuthorizeRequest($request, $response, $is_authorized, $user['id']);
b680cefd
RK
131 /* For testing only
132 if ($is_authorized) {
133 // this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
134 $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
135 exit("SUCCESS! Authorization Code: $code");
136 }
137 */
60e46184 138 $utils->resetRedirect($session);
b680cefd
RK
139 $response->send();
140 exit();
141 }
142 }
143 else {
144 // Display login/register form.
145 $para = $body->appendElement('p', _('You need to log in or register to continue.'));
146 $para->setAttribute('class', 'logininfo');
409b55f4 147 $utils->appendLoginForm($body, $session, $user);
60e46184 148 $utils->setRedirect($session, str_replace('&logout=1', '', $_SERVER['REQUEST_URI'])); // Make sure to strip a logout to not get into a loop.
b680cefd 149 }
e6624d81
RK
150}
151
b680cefd
RK
152if (count($errors)) {
153 $body->appendElement('p', ((count($errors) <= 1)
154 ?_('The following error was detected')
155 :_('The following errors were detected')).':');
156 $list = $body->appendElement('ul');
157 $list->setAttribute('class', 'flat warn');
158 foreach ($errors as $msg) {
159 $item = $list->appendElement('li', $msg);
160 }
161 $body->appendButton(_('Back'), 'history.back();');
e6624d81 162}
b680cefd
RK
163
164// Send HTML to client.
165print($document->saveHTML());
e6624d81 166?>