Auth0TestUser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 35
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTestUser() 0 6 1
A getUserAuth0Token() 0 18 2
A getAuth0Service() 0 4 1
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
33
        return $this->getAuth0Service()->getUserByDecodedJWT($tokenInfo);
34
    }
35
36
    private function getUserAuth0Token()
37
    {
38
        return Cache::remember('testing:http_access_token', 60 * 60, function () {
39
            try {
40
                $httpClient = new Client();
41
                $response = $httpClient->post(env('AUTH0_DOMAIN').'oauth/token', [
42
                    'form_params' => [
43
                        'grant_type' => 'password',
44
                        'client_id' => env('AUTH0_CLIENT_ID'),
45
                        'username' => env('AUTH0_TEST_USER_NAME'),
46
                        'password' => env('AUTH0_TEST_USER_PASS'),
47
                        'scope' => 'openid profile email offline_access',
48
                    ],
49
                ]);
50
51
                return json_decode($response->getBody()->getContents());
52
            } catch (ClientException $exception) {
53
                throw new Exception('Could not obtain token from Auth0 at '.env('AUTH0_DOMAIN').' for testing.');
54
            }
55
        });
56
    }
57
}
58