Authenticator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 42
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B oauthAuthenticate() 0 31 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Conrad
5
 * Date: 23/01/2018
6
 * Time: 9:35 AM
7
 */
8
9
namespace AdvancedLearning\Oauth2Server\Utilities;
10
11
12
use function min;
13
use SilverStripe\Control\HTTPRequest;
14
15
trait Authenticator
16
{
17
    /**
18
     * Check request has oauth headers, and optionally check for scopes.
19
     *
20
     * @param HTTPRequest $request
21
     * @param array       $scopes
22
     *
23
     * @return bool
24
     */
25
    public function oauthAuthenticate(HTTPRequest $request, $scopes = [])
26
    {
27
        $headers = $request->getHeaders();
28
29
        // must have a client
30
        if (empty($headers['oauth_client_id'])) {
31
            return false;
32
        }
33
34
        // if scopes passed, check request contains all the scopes
35
        if (!empty($scopes)) {
36
            $matchedScopes = [];
37
38
            $requestScopes = !empty($headers['oauth_scopes']) ?
39
                explode(',', $headers['oauth_scopes']) :
40
                [];
41
42
            // if request has no scopes then authentication failed
43
            if (empty($requestScopes)) {
44
                return false;
45
            }
46
47
            foreach ($scopes as $scope) {
48
                $matchedScopes[] = in_array($scope, $requestScopes);
49
            }
50
51
            return (bool)min($matchedScopes);
52
        }
53
54
        return true;
55
    }
56
}