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

MultiauthActions::multiauthActingAs()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 27
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 6
nop 2
crap 20
1
<?php
2
3
namespace SMartins\PassportMultiauth\Testing;
4
5
use Laravel\Passport\Client;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
8
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
9
10
trait MultiauthActions
11
{
12
    use MakesHttpRequests, InteractsWithConsole;
13
14
    /**
15
     * The route to generate the access token. The default value is the standard
16
     * route from Laravel\Passport.
17
     *
18
     * @var string
19
     */
20
    protected $oauthTokenRoute = 'oauth/token';
21
22
    public function setUp()
23
    {
24
        parent::setUp();
25
26
        $this->artisan('passport:install');
27
    }
28
29
    /**
30
     * Set the the Authorization header with an access token created using
31
     * Laravel Passport.
32
     *
33
     * @todo Change way to issue token from $this->json() to creating accessing
34
     *       AccessTokenController@issueToken directly.
35
     * @todo Pass this method to PassportMultiauth::actingAs().
36
     *
37
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
38
     * @param  string $scope
39
     * @return $this
40
     */
41
    public function multiauthActingAs(Authenticatable $user, $scope = '')
42
    {
43
        $client = Client::where('personal_access_client', false)
44
                        ->where('revoked', false)
45
                        ->first();
46
47
        $provider = '';
48
        foreach (config('auth.providers') as $p => $config) {
49
            if ($user instanceof $config['model']) {
50
                $provider = $p;
51
            }
52
        }
53
54
        $params = [
55
            'grant_type' => 'password',
56
            'client_id' => $client->id,
57
            'client_secret' => $client->secret,
58
            '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...
59
            'password' => 'secret',
60
            'scope' => $scope,
61
        ];
62
63
        // If model to be authenticated don't has the default provider
64
        if (config('auth.guards.api.provider') !== $provider) {
65
            $params = array_merge($params, ['provider' => $provider]);
66
        }
67
68
        $response = $this->json('POST', $this->oauthTokenRoute, $params);
69
        $accessToken = json_decode($response->original)->access_token;
70
71
        $this->withHeader('Authorization', 'Bearer '.$accessToken);
72
73
        return $this;
74
    }
75
}
76