Completed
Push — master ( 011735...e954f0 )
by Tomas
09:56
created

StaticBearerTokenRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Tomaj\NetteApi\Misc;
4
5
use Exception;
6
7
class StaticBearerTokenRepository implements BearerTokenRepositoryInterface
8
{
9
    /**
10
     * array
11
     */
12
    private $validTokens = [];
13
14
    /**
15
     * Create static bearer token repository.
16
     * You can pass multiple tokens that will be available for your api.
17
     * Format is associtive array where key is token string and value is IP range
18
     *
19
     * Example:
20
     * ['ef0p9iwehjgoihrgrsdgfoihw4t' => '*']
21
     *
22
     * Or:
23
     * ['asfoihegoihregoihrhgrehg' => '127.0.0.1', 'asfo9uyewtoiyewgt4ty4r' => '*']
24
     *
25
     * @see BearerTokenAuthorization#isValidIp for all available Ip range formats
26
     *
27
     * @param array $validTokens
28
     */
29
    public function __construct($validTokens = [])
30
    {
31
        $this->validTokens = $validTokens;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function validToken($token)
38
    {
39
        return in_array($token, array_keys($this->validTokens));
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function ipRestrictions($token)
46
    {
47
        if (isset($this->validTokens[$token])) {
48
            return $this->validTokens[$token];
49
        }
50
        throw new Exception('Token not found - this should never happend because token validation is before IP validation');
51
    }
52
}
53