Passed
Pull Request — master (#19)
by Samuel
03:03
created

MultiauthActions::multiauthActingAs()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 2
crap 3
1
<?php
2
3
namespace SMartins\PassportMultiauth\Testing;
4
5
use Laravel\Passport\Client;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
9
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
10
11
trait MultiauthActions
12
{
13
    use MakesHttpRequests, InteractsWithConsole;
14
15
    /**
16
     * The route to generate the access token. The default value is the standard
17
     * route from Laravel\Passport.
18
     *
19
     * @var string
20
     */
21
    protected $oauthTokenRoute = 'oauth/token';
22
23
    /**
24
     * @codeCoverageIgnore
25
     */
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        $this->artisan('passport:install');
31
    }
32
33
    /**
34
     * Set the the Authorization header with an access token created using
35
     * Laravel Passport.
36
     *
37
     * @todo Change way to issue token from $this->json() to creating accessing
38
     *       AccessTokenController@issueToken directly.
39
     * @todo Pass this method to PassportMultiauth::actingAs().
40
     *
41
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
42
     * @param  string $scope
43
     * @return $this
44
     */
45 9
    public function multiauthActingAs(Authenticatable $user, $scope = '')
46
    {
47
        // @todo Change to specific repository
48 9
        $client = Client::where('personal_access_client', false)
49 9
                        ->where('revoked', false)
50 9
                        ->first();
51
52 9
        if (!$client) {
53 1
            throw new ModelNotFoundException('Laravel\Passport password grant not found. Please run `passport:install` to generate client.');
54
        }
55
56 8
        $provider = $this->getUserProvider($user);
57
58
        $params = [
59 8
            'grant_type' => 'password',
60 8
            'client_id' => $client->id,
61 8
            'client_secret' => $client->secret,
62 8
            'username' => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
63 8
            'password' => 'secret',
64 8
            'scope' => $scope,
65
        ];
66
67
        // If model to be authenticated don't is the default provider
68 8
        if (! $this->isDefaultProvider($provider)) {
69 4
            $params = array_merge($params, ['provider' => $provider]);
70
        }
71
72 8
        $response = $this->json('POST', $this->oauthTokenRoute, $params);
73
74 8
        $accessToken = json_decode($response->getContent())->access_token;
75
76 8
        $this->withHeader('Authorization', 'Bearer '.$accessToken);
77
78 8
        return $this;
79
    }
80
81
    /**
82
     * Get the user provider on configs.
83
     *
84
     * @todo Move to class specialized in check auth configs.
85
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
86
     * @return string
87
     */
88 8
    protected function getUserProvider(Authenticatable $user)
89
    {
90 8
        $provider = '';
91 8
        foreach (config('auth.providers') as $p => $config) {
92 8
            if ($user instanceof $config['model']) {
93 8
                $provider = $p;
94
            }
95
        }
96
97 8
        return $provider;
98
    }
99
100
    /**
101
     * Check if provider is the default provider used by Laravel\Passport.
102
     *
103
     * @todo Move to class specialized in check auth configs.
104
     * @param string $provider
105
     * @return boolean
106
     */
107 8
    protected function isDefaultProvider(string $provider)
108
    {
109 8
        return config('auth.guards.api.provider') === $provider;
110
    }
111
}
112