Completed
Push — master ( 2c2349...eac962 )
by Dan Michael O.
02:31
created

IpWhitelist::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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
        foreach ($whitelist as $range) {
29
            list($start, $end) = explode('-', $range);
30
            if ($ip >= ip2long($start) && $ip < ip2long($end)) {
31
                return true;
32
            }
33
        }
34
35
        return false;
36
    }
37
38
    /**
39
     * Handle an incoming request.
40
     *
41
     * @param \Illuminate\Http\Request $request
42
     * @param \Closure                 $next
43
     *
44
     * @return mixed
45
     */
46
    public function handle($request, Closure $next)
47
    {
48
        if (!$this->isWhitelisted($request->ip())) {
49
            return response('Du må redigere fra UiO-nettet.', 401);
50
        }
51
52
        return $next($request);
53
    }
54
}
55