|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSAML\Module\perun; |
| 4 | + |
| 5 | +use SimpleSAML\Logger; |
| 6 | +use SimpleSAML\Module\perun\databaseCommand\ChallengesDbCmd; |
| 7 | + |
| 8 | +class ChallengeManager |
| 9 | +{ |
| 10 | + const LOG_PREFIX = 'Perun:ChallengeManager: '; |
| 11 | + private $challengeDbCmd; |
| 12 | + |
| 13 | + public function __construct() |
| 14 | + { |
| 15 | + $this->challengeDbCmd = new ChallengesDbCmd(); |
| 16 | + } |
| 17 | + |
| 18 | + public function insertChallenge($challenge, $id, $scriptName): bool |
| 19 | + { |
| 20 | + if (empty($challenge) || |
| 21 | + empty($id) || |
| 22 | + empty($scriptName) || |
| 23 | + !$this->challengeDbCmd->insertChallenge($challenge, $id, $scriptName)) { |
| 24 | + Logger::error(self::LOG_PREFIX . 'Error while creating a challenge'); |
| 25 | + http_response_code(500); |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + return true; |
| 30 | + } |
| 31 | + |
| 32 | + public function readChallengeFromDb($id) |
| 33 | + { |
| 34 | + if (empty($id)) { |
| 35 | + http_response_code(400); |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + $result = $this->challengeDbCmd->readChallenge($id); |
| 40 | + |
| 41 | + if ($result === null) { |
| 42 | + http_response_code(500); |
| 43 | + } |
| 44 | + |
| 45 | + return $result; |
| 46 | + } |
| 47 | + |
| 48 | + public static function checkAccess($challenge, $challengeDb): bool |
| 49 | + { |
| 50 | + if (empty($challenge) || empty($challengeDb)) { |
| 51 | + http_response_code(400); |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + if (!hash_equals($challengeDb, $challenge)) { |
| 56 | + Logger::error(self::LOG_PREFIX . 'Hashes are not equal.'); |
| 57 | + http_response_code(401); |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + public function deleteChallengeFromDb($id): bool |
| 65 | + { |
| 66 | + if (empty($id)) { |
| 67 | + http_response_code(400); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + if (!$this->challengeDbCmd->deleteChallenge($id)) { |
| 72 | + Logger::error(self::LOG_PREFIX . 'Error while deleting challenge from the database.'); |
| 73 | + http_response_code(500); |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | +} |
0 commit comments