|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arthur |
|
5
|
|
|
* Date: 09.03.19 |
|
6
|
|
|
* Time: 20:08 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Modules\Auth0\Traits; |
|
10
|
|
|
|
|
11
|
|
|
use Cache; |
|
12
|
|
|
use Foundation\Exceptions\Exception; |
|
13
|
|
|
use GuzzleHttp\Client; |
|
14
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
15
|
|
|
use Modules\Auth0\Contracts\Auth0ServiceContract; |
|
16
|
|
|
use Modules\Auth0\Services\Auth0Service; |
|
17
|
|
|
use Modules\User\Entities\User; |
|
18
|
|
|
|
|
19
|
|
|
trait Auth0TestUser |
|
20
|
|
|
{ |
|
21
|
|
|
private function getAuth0Service() : Auth0Service |
|
22
|
|
|
{ |
|
23
|
|
|
return once(function (){ |
|
24
|
|
|
return app()->make(Auth0ServiceContract::class); |
|
25
|
|
|
}); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function getTestUser(): User |
|
29
|
|
|
{ |
|
30
|
|
|
$auth0 = \App::make('auth0'); |
|
31
|
|
|
$tokenInfo = $auth0->decodeJWT($this->getUserAuth0Token()->id_token); |
|
32
|
|
|
return $this->getAuth0Service()->getUserByDecodedJWT($tokenInfo); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function getUserAuth0Token() |
|
36
|
|
|
{ |
|
37
|
|
|
return Cache::remember('testing:http_access_token', 60 * 60, function () { |
|
38
|
|
|
try { |
|
39
|
|
|
$httpClient = new Client(); |
|
40
|
|
|
$response = $httpClient->post(env('AUTH0_DOMAIN') . 'oauth/token', [ |
|
41
|
|
|
'form_params' => [ |
|
42
|
|
|
'grant_type' => 'password', |
|
43
|
|
|
'client_id' => env('AUTH0_CLIENT_ID'), |
|
44
|
|
|
'username' => env('AUTH0_TEST_USER_NAME'), |
|
45
|
|
|
'password' => env('AUTH0_TEST_USER_PASS'), |
|
46
|
|
|
'scope' => 'openid profile email offline_access', |
|
47
|
|
|
], |
|
48
|
|
|
]); |
|
49
|
|
|
|
|
50
|
|
|
return json_decode($response->getBody()->getContents()); |
|
51
|
|
|
} catch (ClientException $exception) { |
|
52
|
|
|
throw new Exception('Could not obtain token from Auth0 at ' . env('AUTH0_DOMAIN') . ' for testing.'); |
|
53
|
|
|
} |
|
54
|
|
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|