Completed
Push — master ( 630443...dd0f05 )
by Yaro
01:43
created

BasicAuth::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Yaro\ApiDocs\Http\Middleware;
4
5
class BasicAuth
6
{
7
    
8
    public function handle($request, $next)
9
    {
10
        if ($this->isAuthorized($request)) {
11
            return response('Unauthorized', 401, [
12
                'WWW-Authenticate' => 'Basic',
13
            ]);
14
        }
15
        
16
        return $next($request);
17
    }
18
    
19
    private function isAuthorized($request) 
20
    {
21
        if (!config('yaro.apidocs.auth.enabled', false)) {
22
            return true;
23
        }
24
        
25
        $authorized = collect(config('yaro.apidocs.auth.credentials', []));
26
        $credentials = [
27
            $request->getUser(), 
28
            $request->getPassword()
29
        ];
30
        
31
        return !$authorized->contains($credentials);
32
    }
33
    
34
}
35