|
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
|
|
|
public function __construct(ResourceServer $server, ProviderRepository $providers, Auth $auth) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->server = $server; |
|
37
|
|
|
$this->providers = $providers; |
|
38
|
|
|
$this->auth = $auth; |
|
39
|
|
|
} |
|
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
|
|
|
public function handle($request, Closure $next, ...$guards) |
|
52
|
|
|
{ |
|
53
|
|
|
// Get the auth guard if has to check the default guard |
|
54
|
|
|
$guards = GuardChecker::getAuthGuards($request); |
|
55
|
|
|
|
|
56
|
|
|
// If don't has any guard follow the flow |
|
57
|
|
|
if (0 === count($guards)) { |
|
58
|
|
|
return $next($request); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$psr = (new DiactorosFactory())->createRequest($request); |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$psr = $this->server->validateAuthenticatedRequest($psr); |
|
65
|
|
|
|
|
66
|
|
|
$tokenId = $psr->getAttribute('oauth_access_token_id'); |
|
67
|
|
|
|
|
68
|
|
|
if (! $tokenId) { |
|
69
|
|
|
throw new AuthenticationException('Unauthenticated', $guards); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$accessToken = $this->providers->findForToken($tokenId); |
|
73
|
|
|
|
|
74
|
|
|
if (! $accessToken) { |
|
75
|
|
|
throw new AuthenticationException('Unauthenticated', $guards); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$providers = collect($guards)->mapWithKeys(function ($guard) { |
|
79
|
|
|
return [GuardChecker::defaultGuardProvider($guard) => $guard]; |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
// use only guard associated to access token provider |
|
83
|
|
|
if ($providers->has($accessToken->provider)) { |
|
84
|
|
|
$this->authenticate([$providers->get($accessToken->provider)]); |
|
85
|
|
|
} else { |
|
86
|
|
|
$this->authenticate([]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $next($request); |
|
90
|
|
|
} catch (OAuthServerException $e) { |
|
91
|
|
|
// @todo Check if it's the best way to handle with OAuthServerException |
|
92
|
|
|
throw new AuthenticationException('Unauthenticated', $guards); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|