BasicAuth   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 2
A isAuthorized() 0 14 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