Issues (37)

JwtAccessToken/LocalIntrospectionEndPoint.php (4 issues)

1
<?php
2
3
namespace Parroauth2\Client\Extension\JwtAccessToken;
4
5
use Parroauth2\Client\ClientInterface;
6
use Parroauth2\Client\EndPoint\Introspection\IntrospectionEndPoint;
7
use Parroauth2\Client\EndPoint\Introspection\IntrospectionResponse;
8
9
/**
10
 * Try to perform a local introspection by deserialize a JWT access token
11
 * If the deserialization failed, the base introspection endpoint will be called
12
 */
13
class LocalIntrospectionEndPoint extends IntrospectionEndPoint
14
{
15
    /**
16
     * @var JwtParserInterface
17
     */
18
    private $parser;
19
20
    /**
21
     * @var ClientInterface
22
     */
23
    private $client;
24
25
26
    /**
27
     * LocalIntrospectionEndPoint constructor.
28
     *
29
     * @param ClientInterface $client
30
     * @param JwtParserInterface $parser
31
     */
32 20
    public function __construct(ClientInterface $client, JwtParserInterface $parser)
33
    {
34 20
        parent::__construct($client);
35
36 20
        $this->parser = $parser;
37 20
        $this->client = $client;
38 20
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 10
    public function call(): IntrospectionResponse
44
    {
45 10
        if ($this->get('token_type_hint') !== null && $this->get('token_type_hint') !== self::TYPE_ACCESS_TOKEN) {
46 1
            return $this->networkCall();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->networkCall() returns the type Parroauth2\Client\EndPoi...n\IntrospectionResponse which is incompatible with the return type mandated by Parroauth2\Client\EndPoi...dPointInterface::call() of Parroauth2\Client\EndPoint\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
47
        }
48
49
        try {
50 9
            $claims = $this->parser->parse($this->get('token'), $this->client);
0 ignored issues
show
It seems like $this->get('token') can also be of type null; however, parameter $jwt of Parroauth2\Client\Extens...arserInterface::parse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            $claims = $this->parser->parse(/** @scrutinizer ignore-type */ $this->get('token'), $this->client);
Loading history...
51 3
        } catch (\InvalidArgumentException $e) {
52 3
            return $this->networkCall();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->networkCall() returns the type Parroauth2\Client\EndPoi...n\IntrospectionResponse which is incompatible with the return type mandated by Parroauth2\Client\EndPoi...dPointInterface::call() of Parroauth2\Client\EndPoint\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
53
        }
54
55 6
        $expired = $claims['exp'] ?? 0;
56
57 6
        $claims += ['token_type' => 'bearer', 'iss' => null];
58
59 6
        if (!isset($claims['client_id']) && !empty($claims['aud'])) {
60 5
            $claims['client_id'] = is_array($claims['aud']) ? $claims['aud'][0] : $claims['aud'];
61
        }
62
63
        // We check here if the issuer is right and if the token is not expired
64 6
        if ($claims['iss'] !== $this->client->provider()->issuer() || ($expired >= 0 && $expired < time())) {
65 2
            $response = new IntrospectionResponse(['active' => false]);
66
        } else {
67 4
            $response = new IntrospectionResponse(['active' => true] + $claims);
68
        }
69
70 6
        $this->callResponseListeners($response);
71
72 6
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Parroauth2\Client\EndPoi...n\IntrospectionResponse which is incompatible with the return type mandated by Parroauth2\Client\EndPoi...dPointInterface::call() of Parroauth2\Client\EndPoint\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
73
    }
74
75
    /**
76
     * Try to call the endpoint, if configured
77
     * If not, an inactive introspection response is returned
78
     */
79 4
    private function networkCall(): IntrospectionResponse
80
    {
81 4
        if ($this->client->provider()->supportsEndpoint(self::NAME)) {
82 3
            return parent::call();
83
        }
84
85 1
        $response = new IntrospectionResponse(['active' => false]);
86 1
        $this->callResponseListeners($response);
87
88 1
        return $response;
89
    }
90
}
91