IpWhitelist::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Colligator\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Auth\Guard;
7
8
class IpWhitelist
9
{
10
    /**
11
     * Create a new filter instance.
12
     */
13
    public function __construct()
14
    {
15
    }
16
17
    protected function isWhitelisted($ip)
18
    {
19
        // https://www.uio.no/english/services/it/security/cert/about-cert/constituency.html
20
        $whitelist = [
21
            '193.157.108.0-193.157.255.255',
22
            '129.240.0.0-129.240.255.255',
23
            '158.36.184.0-158.36.191.255',
24
            '193.156.90.0-193.156.90.255',
25
            '193.156.120.0-193.156.120.255',
26
        ];
27
28
        $ip = ip2long($ip);
29
30
        foreach ($whitelist as $range) {
31
            list($start, $end) = explode('-', $range);
32
            if ($ip >= ip2long($start) && $ip < ip2long($end)) {
33
                return true;
34
            }
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * Handle an incoming request.
42
     *
43
     * @param \Illuminate\Http\Request $request
44
     * @param \Closure                 $next
45
     *
46
     * @return mixed
47
     */
48
    public function handle($request, Closure $next)
49
    {
50
        if (!$this->isWhitelisted($request->ip())) {
51
            return response('Du må redigere fra UiO-nettet.', 401);
52
        }
53
54
        return $next($request);
55
    }
56
}
57