Completed
Push — master ( d7a0e6...027db9 )
by Michal
22:22 queued 07:24
created

src/Misc/StaticTokenRepository.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Misc;
6
7
/**
8
 * @todo change implements to TokenRepositoryInterface after BearerTokenRepositoryInterface will be removed in 3.0.0
9
 */
10
class StaticTokenRepository implements BearerTokenRepositoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Tomaj\NetteApi\Misc\BearerTokenRepositoryInterface has been deprecated with message: use TokenRepositoryInterface instead
will be removed in 3.0.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
11
{
12
    /**
13
     * array
14
     */
15
    private $validTokens = [];
16
17
    /**
18
     * Create static bearer token repository.
19
     * You can pass multiple tokens that will be available for your api.
20
     * Format is associtive array where key is token string and value is IP range
21
     *
22
     * Example:
23
     * ['ef0p9iwehjgoihrgrsdgfoihw4t' => '*']
24
     *
25
     * Or:
26
     * ['asfoihegoihregoihrhgrehg' => '127.0.0.1', 'asfo9uyewtoiyewgt4ty4r' => '*']
27
     *
28
     * @see BearerTokenAuthorization#isValidIp for all available Ip range formats
29
     *
30
     * @param array $validTokens
31
     */
32
    public function __construct($validTokens = [])
33
    {
34
        $this->validTokens = $validTokens;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function validToken(string $token): bool
41
    {
42
        return in_array($token, array_keys($this->validTokens));
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function ipRestrictions(string $token): ?string
49
    {
50
        if (isset($this->validTokens[$token])) {
51
            return $this->validTokens[$token];
52
        }
53
        return null;
54
    }
55
}
56