fix CS
[phancap.git] / src / phancap / Authenticator.php
1 <?php
2 /**
3  * Part of phancap
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Authenticator
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/phancap.htm
13  */
14 namespace phancap;
15
16 /**
17  * Authentication helper methods
18  *
19  * @category  Tools
20  * @package   Authenticator
21  * @author    Christian Weiske <cweiske@cweiske.de>
22  * @copyright 2014 Christian Weiske
23  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24  * @version   Release: @package_version@
25  * @link      http://cweiske.de/phancap.htm
26  */
27 class Authenticator
28 {
29     /**
30      * Validate the authentication signature.
31      *
32      * @param object $config Phancap configuration
33      *
34      * @return void
35      * @throws \Exception When a parameter is missing, or authentication fails
36      */
37     public function authenticate(Config $config)
38     {
39         if ($config->access === false) {
40             throw new \Exception('Authentication not setup');
41         }
42         if ($config->access === true) {
43             //Access without restrictions allowed
44             return;
45         }
46
47         if (!isset($_GET['atoken'])) {
48             throw new \Exception('Parameter missing: atoken');
49         }
50         if (!isset($_GET['asignature'])) {
51             throw new \Exception('Parameter missing: asignature');
52         }
53         if (!isset($_GET['atimestamp'])) {
54             throw new \Exception('Parameter missing: atimestamp');
55         }
56
57         $token = $_GET['atoken'];
58         if (!array_key_exists($token, $config->access)) {
59             throw new \Exception('Unknown atoken');
60         }
61
62         $timestamp = (int) $_GET['atimestamp'];
63         if ($timestamp + $config->timestampMaxAge < time()) {
64             throw new \Exception('atimestamp too old');
65         }
66
67         $signature = $_GET['asignature'];
68
69         $params = $_GET;
70         unset($params['asignature']);
71         $sigdata = $this->getSignatureData($params);
72
73         $verifiedSignature = hash_hmac('sha1', $sigdata, $config->access[$token]);
74         if ($signature !== $verifiedSignature) {
75             throw new \Exception('Invalid signature');
76         }
77     }
78
79     /**
80      * Convert a list of parameters into a string that can be hashed.
81      *
82      * @param array $params Parameters, key-value pairs
83      *
84      * @return string Line of encoded parameters
85      */
86     protected function getSignatureData($params)
87     {
88         ksort($params);
89         $encparams = array();
90         foreach ($params as $key => $value) {
91             $encparams[] = $key . '=' . rawurlencode($value);
92         }
93         return implode('&', $encparams);
94     }
95 }
96 ?>