Passed
Pull Request — 4.0 (#103)
by
unknown
05:41
created

AddCustomProvider::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SMartins\PassportMultiauth\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use League\OAuth2\Server\Exception\OAuthServerException;
8
9
class AddCustomProvider
10
{
11
    /**
12
     * The default provider of api guard.
13
     *
14
     * @var string
15
     */
16
    protected $defaultApiProvider;
17
18
    /**
19
     * Handle an incoming request. Set the `provider` from `api` guard using a
20
     * parameter `provider` coming from request. The provider on `apì` guard
21
     * is used by Laravel Passport to get the correct model on access token
22
     * creation.
23
     *
24
     * @param  \Illuminate\Http\Request $request
25
     * @param  \Closure $next
26
     *
27
     * @return mixed
28
     *
29
     * @throws OAuthServerException
30
     */
31 5
    public function handle(Request $request, Closure $next)
32
    {
33 5
        $this->defaultApiProvider = config('auth.guards.api.provider');
34
35 5
        $provider = $request->get('provider');
36
37 5
        if ($this->invalidProvider($provider) || $this->clientGrantType($request)) {
38 4
            throw OAuthServerException::invalidRequest('provider');
39
        }
40
41 1
        config(['auth.guards.api.provider' => $provider]);
42
43 1
        return $next($request);
44
    }
45
46
    /**
47
     * Reset config provider to default after complete request. If necessary
48
     * can receive $request and $response params. To be used the attribute
49
     * $this->defaultApiProvider the middleware was registered on ServiceProvider
50
     * as a singleton.
51
     * Read more in https://laravel.com/docs/5.6/middleware#terminable-middleware.
52
     *
53
     * @return void
54
     */
55 1
    public function terminate()
56
    {
57 1
        config(['auth.guards.api.provider' => $this->defaultApiProvider]);
58 1
    }
59
60
    /**
61
     * Check if the given provider is not registered in the auth configuration file.
62
     *
63
     * @param $provider
64
     *
65
     * @return bool
66
     */
67 5
    protected function invalidProvider($provider)
68
    {
69 5
        if (is_null($provider)) {
70 3
            return true;
71
        }
72
73 2
        foreach (config('auth.guards') as $guardsConfiguration) {
74 2
            if ($guardsConfiguration['provider'] === $provider) {
75 2
                return false;
76
            }
77
        }
78
79 1
        return true;
80
    }
81
82 1
    protected function clientGrantType(Request $request)
83
    {
84 1
        if (!$request->has('client_credentials')) {
85 1
            return false;
86
        }
87
88
        return true;
89
    }
90
}
91