Commit | Line | Data |
---|---|---|
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). |
12 | require_once(__DIR__.'/authsystem.inc.php'); | |
e6624d81 | 13 | |
b680cefd RK |
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'); | |
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 |
31 | if (!count($errors)) { |
32 | $session = $utils->initSession(); // Read session or create new session and set cookie. | |
409b55f4 RK |
33 | if (intval($session['user'])) { |
34 | $result = $db->prepare('SELECT `id`,`email`,`verify_hash` FROM `auth_users` WHERE `id` = :userid;'); | |
35 | $result->execute(array(':userid' => $session['user'])); | |
36 | $user = $result->fetch(PDO::FETCH_ASSOC); | |
37 | if (!$user['id']) { | |
38 | $utils->log('user_read_failure', 'user: '.$session['user']); | |
39 | } | |
40 | } | |
41 | else { | |
42 | $user = array('id' => 0, 'email' => ''); | |
43 | } | |
b680cefd RK |
44 | $pagetype = 'default'; |
45 | if (is_null($session)) { | |
46 | $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.'); | |
47 | } | |
48 | elseif ($session['logged_in']) { | |
49 | // We are logged in, process authorization request. | |
50 | $request = OAuth2\Request::createFromGlobals(); | |
51 | $response = new OAuth2\Response(); | |
e6624d81 | 52 | |
b680cefd RK |
53 | // Validate the authorize request. |
54 | if (!$server->validateAuthorizeRequest($request, $response)) { | |
55 | $response->send(); | |
56 | exit(); | |
57 | } | |
77f0f9ff | 58 | |
3ae47861 RK |
59 | // Display an authorization form (unless the scope is email, which we always grant in this system). |
60 | if (empty($_POST) && (@$request->query['scope'] != 'email')) { | |
b680cefd RK |
61 | $para = $body->appendElement('p', sprintf(_('Hi %s!'), $user['email'])); |
62 | $para->setAttribute('class', 'userwelcome'); | |
63 | ||
64 | $form = $body->appendForm('', 'POST', 'authform'); | |
65 | $form->setAttribute('id', 'authform'); | |
66 | $form->appendElement('p', sprintf(_('Do you authorize %s to access %s?'), $request->query['client_id'], $request->query['scope'])); | |
67 | $submit = $form->appendInputSubmit(_('yes')); | |
68 | $submit->setAttribute('name', 'authorized'); | |
69 | $form->appendText(' '); | |
70 | $submit = $form->appendInputSubmit(_('no')); | |
71 | $submit->setAttribute('name', 'authorized'); | |
72 | } | |
73 | else { | |
74 | // Handle authorize request, forwarding code in GET parameters if the user has authorized your client. | |
80ca2a2f | 75 | $is_authorized = ((@$_POST['authorized'] === 'yes') || ($request->query['scope'] == 'email')); |
ea0452ad | 76 | $server->handleAuthorizeRequest($request, $response, $is_authorized, $user['id']); |
b680cefd RK |
77 | /* For testing only |
78 | if ($is_authorized) { | |
79 | // this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client | |
80 | $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40); | |
81 | exit("SUCCESS! Authorization Code: $code"); | |
82 | } | |
83 | */ | |
84 | $response->send(); | |
85 | exit(); | |
86 | } | |
87 | } | |
88 | else { | |
89 | // Display login/register form. | |
90 | $para = $body->appendElement('p', _('You need to log in or register to continue.')); | |
91 | $para->setAttribute('class', 'logininfo'); | |
409b55f4 RK |
92 | $utils->appendLoginForm($body, $session, $user); |
93 | // Save the request in the session so we can get back to fulfilling it. | |
94 | $result = $db->prepare('UPDATE `auth_sessions` SET `saved_redirect` = :redir WHERE `id` = :sessid;'); | |
95 | if (!$result->execute(array(':redir' => $_SERVER['REQUEST_URI'], ':sessid' => $session['id']))) { | |
96 | $utils->log('redir_save_failure', 'session: '.$session['id'].', redirect: '.$_SERVER['REQUEST_URI']); | |
97 | } | |
b680cefd | 98 | } |
e6624d81 RK |
99 | } |
100 | ||
b680cefd RK |
101 | if (count($errors)) { |
102 | $body->appendElement('p', ((count($errors) <= 1) | |
103 | ?_('The following error was detected') | |
104 | :_('The following errors were detected')).':'); | |
105 | $list = $body->appendElement('ul'); | |
106 | $list->setAttribute('class', 'flat warn'); | |
107 | foreach ($errors as $msg) { | |
108 | $item = $list->appendElement('li', $msg); | |
109 | } | |
110 | $body->appendButton(_('Back'), 'history.back();'); | |
e6624d81 | 111 | } |
b680cefd RK |
112 | |
113 | // Send HTML to client. | |
114 | print($document->saveHTML()); | |
e6624d81 | 115 | ?> |