|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace LORIS\login; |
| 4 | + |
| 5 | +use \Psr\Http\Message\ServerRequestInterface; |
| 6 | +use \Psr\Http\Message\ResponseInterface; |
| 7 | +use \LORIS\Middleware\ETagCalculator; |
| 8 | + |
| 9 | +/** |
| 10 | + * POST request for authentication. |
| 11 | + * |
| 12 | + * Used to request account. |
| 13 | + * |
| 14 | + * @category Loris |
| 15 | + * @package Login |
| 16 | + * @author Alizée Wickenheiser <[email protected]> |
| 17 | + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 |
| 18 | + * @link https://www.github.com/aces/Loris/ |
| 19 | + */ |
| 20 | +class MFA extends \NDB_Page |
| 21 | +{ |
| 22 | + public $skipTemplate = true; |
| 23 | + |
| 24 | + /** |
| 25 | + * Include additional JS files |
| 26 | + * |
| 27 | + * @return array of javascript to be inserted |
| 28 | + */ |
| 29 | + function getJSDependencies() |
| 30 | + { |
| 31 | + $factory = \NDB_Factory::singleton(); |
| 32 | + $baseURL = $factory->settings()->getBaseURL(); |
| 33 | + $deps = parent::getJSDependencies(); |
| 34 | + return array_merge( |
| 35 | + $deps, |
| 36 | + [ |
| 37 | + $baseURL . '/login/js/mfaPrompt.js', |
| 38 | + ] |
| 39 | + ); |
| 40 | + } |
| 41 | + /** |
| 42 | + * Include additional CSS files: |
| 43 | + * |
| 44 | + * @return array of javascript to be inserted |
| 45 | + */ |
| 46 | + function getCSSDependencies() |
| 47 | + { |
| 48 | + $factory = \NDB_Factory::singleton(); |
| 49 | + $baseURL = $factory->settings()->getBaseURL(); |
| 50 | + $deps = parent::getCSSDependencies(); |
| 51 | + return array_merge( |
| 52 | + $deps, |
| 53 | + [$baseURL . '/login/css/login.css'] |
| 54 | + ); |
| 55 | + } |
| 56 | + /** |
| 57 | + * This function will return a json object for login module. |
| 58 | + * |
| 59 | + * @param ServerRequestInterface $request The incoming PSR7 request |
| 60 | + * |
| 61 | + * @return ResponseInterface The outgoing PSR7 response |
| 62 | + */ |
| 63 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
| 64 | + { |
| 65 | + // Ensure POST request. |
| 66 | + switch ($request->getMethod()) { |
| 67 | + case 'GET': |
| 68 | + return parent::handle($request); |
| 69 | + case 'POST': |
| 70 | + return $this->_handlePOST($request); |
| 71 | + default: |
| 72 | + return new \LORIS\Http\Response\JSON\MethodNotAllowed( |
| 73 | + $this->allowedMethods() |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Processes the values & saves to database and return a json response. |
| 80 | + * |
| 81 | + * @param ServerRequestInterface $request The incoming PSR7 request. |
| 82 | + * |
| 83 | + * @return ResponseInterface The outgoing PSR7 response |
| 84 | + */ |
| 85 | + private function _handlePOST(ServerRequestInterface $request) : ResponseInterface |
| 86 | + { |
| 87 | + $requestdata = json_decode((string )$request->getBody(), true); |
| 88 | + $user = $request->getAttribute("user"); |
| 89 | + if(!isset($requestdata['code'])) { |
| 90 | + return new \LORIS\Http\Response\JSON\Unauthorized("missing code"); |
| 91 | + } |
| 92 | + |
| 93 | + $validator = $user->getTOTPValidator(); |
| 94 | + $counter = $validator->getTimeCounter(); |
| 95 | + $wantCode = $validator->getCode($counter, 6); |
| 96 | + if($wantCode === $requestdata['code']) { |
| 97 | + $login = $_SESSION['State']->getProperty('login'); |
| 98 | + $login->setPassedMFA(); |
| 99 | + return new \LORIS\Http\Response\JSON\OK(["success" => "validated code"]); |
| 100 | + } else { |
| 101 | + return new \LORIS\Http\Response\JSON\Unauthorized("invalid code"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Return an array of valid HTTP methods for this endpoint |
| 107 | + * |
| 108 | + * @return string[] Valid versions |
| 109 | + */ |
| 110 | + protected function allowedMethods(): array |
| 111 | + { |
| 112 | + return ['GET', 'POST']; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns true if the user has permission to access |
| 117 | + * the Login module |
| 118 | + * |
| 119 | + * @param \User $user The user whose access is being checked |
| 120 | + * |
| 121 | + * @return bool true if user has permission |
| 122 | + */ |
| 123 | + function _hasAccess(\User $user) : bool |
| 124 | + { |
| 125 | + return true; |
| 126 | + } |
| 127 | +} |
0 commit comments