1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use League\OAuth2\Server\ResourceServer; |
7
|
|
|
use Illuminate\Auth\AuthenticationException; |
8
|
|
|
use Illuminate\Auth\Middleware\Authenticate; |
9
|
|
|
use Illuminate\Contracts\Auth\Factory as Auth; |
10
|
|
|
use SMartins\PassportMultiauth\ProviderRepository; |
11
|
|
|
use SMartins\PassportMultiauth\Guards\GuardChecker; |
12
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
13
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; |
14
|
|
|
|
15
|
|
|
class MultiAuthenticate extends Authenticate |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The authentication factory instance. |
19
|
|
|
* |
20
|
|
|
* @var \Illuminate\Contracts\Auth\Factory |
21
|
|
|
*/ |
22
|
|
|
protected $auth; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \League\OAuth2\Server\ResourceServer |
26
|
|
|
*/ |
27
|
|
|
private $server; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \SMartins\PassportMultiauth\ProviderRepository |
31
|
|
|
*/ |
32
|
|
|
private $providers; |
33
|
|
|
|
34
|
10 |
|
public function __construct(ResourceServer $server, ProviderRepository $providers, Auth $auth) |
35
|
|
|
{ |
36
|
10 |
|
$this->server = $server; |
37
|
10 |
|
$this->providers = $providers; |
38
|
10 |
|
$this->auth = $auth; |
39
|
10 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Handle an incoming request. Authenticates the guard from access token |
43
|
|
|
* used on request. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Http\Request $request |
46
|
|
|
* @param \Closure $next |
47
|
|
|
* @param string[] ...$guards |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
10 |
|
public function handle($request, Closure $next, ...$guards) |
52
|
|
|
{ |
53
|
|
|
// If don't has any guard follow the flow |
54
|
10 |
|
if (count($guards) === 0) { |
55
|
1 |
|
return $next($request); |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
$psrRequest = (new DiactorosFactory())->createRequest($request); |
59
|
|
|
|
60
|
|
|
try { |
61
|
9 |
|
$psrRequest = $this->server->validateAuthenticatedRequest($psrRequest); |
62
|
|
|
|
63
|
8 |
|
$tokenId = $psrRequest->getAttribute('oauth_access_token_id'); |
64
|
|
|
|
65
|
8 |
|
if (! $tokenId) { |
66
|
1 |
|
throw new AuthenticationException('Unauthenticated', $guards); |
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
$accessToken = $this->providers->findForToken($tokenId); |
70
|
|
|
|
71
|
7 |
|
if (! $accessToken) { |
72
|
1 |
|
throw new AuthenticationException('Unauthenticated', $guards); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
$providers = collect($guards)->mapWithKeys(function ($guard) { |
76
|
6 |
|
return [GuardChecker::defaultGuardProvider($guard) => $guard]; |
77
|
6 |
|
}); |
78
|
|
|
|
79
|
|
|
// use only guard associated to access token provider |
80
|
6 |
|
if ($providers->has($accessToken->provider)) { |
81
|
4 |
|
$this->authenticate([$providers->get($accessToken->provider)]); |
82
|
|
|
} else { |
83
|
2 |
|
$this->authenticate([]); |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
return $next($request); |
87
|
5 |
|
} catch (OAuthServerException $e) { |
88
|
|
|
// @todo Check if it's the best way to handle with OAuthServerException |
89
|
1 |
|
throw new AuthenticationException('Unauthenticated', $guards); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|