Completed
Pull Request — master (#48)
by
unknown
02:05
created

JwtAuthenticator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 99
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getJwsLifetime() 0 4 1
A setJwsLifetime() 0 4 1
A authenticate() 0 11 3
B createJwsContent() 0 24 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the AppleApnPush package
7
 *
8
 * (c) Vitaliy Zhuk <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace Apple\ApnPush\Protocol\Http\Authenticator;
15
16
use Apple\ApnPush\Jwt\JwtInterface;
17
use Apple\ApnPush\Protocol\Http\Request;
18
use Jose\Factory\JWKFactory;
19
use Jose\Factory\JWSFactory;
20
21
/**
22
 * Authenticate request via Json Web Token
23
 */
24
class JwtAuthenticator implements AuthenticatorInterface
25
{
26
    private const ALGORITHM = 'ES256';
27
28
    /**
29
     * @var JwtInterface
30
     */
31
    private $jwt;
32
33
    /**
34
     * @var string
35
     */
36
    private $jws = '';
37
38
    /**
39
     * @var integer
40
     */
41
    private $jwsLifetime = 3600;
42
43
    /**
44
     * @var integer
45
     */
46
    private $jwsCreatedAt = 0;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param JwtInterface $jwt
52
     */
53
    public function __construct(JwtInterface $jwt)
54
    {
55
        $this->jwt = $jwt;
56
    }
57
58
    /**
59
     * Get jws lifetime
60
     *
61
     * @return integer
62
    */
63
    public function getJwsLifetime() : int
64
    {
65
        return $this->jwsLifetime;
66
    }
67
68
    /**
69
     * Set jws lifetime
70
     *
71
     * @param integer $jwsLifetime
72
     */
73
    public function setJwsLifetime(int  $jwsLifetime)
74
    {
75
        $this->jwsLifetime = abs($jwsLifetime);//ignore sign
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($jwsLifetime) can also be of type double. However, the property $jwsLifetime is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function authenticate(Request $request): Request
82
    {
83
        if (empty($this->jws) || $this->jwsCreatedAt < time() - $this->jwsLifetime) {
84
            $this->jws = $this->createJwsContent();
85
            $this->jwsCreatedAt = time();
86
        }
87
88
        $request = $request->withHeader('authorization', sprintf('bearer %s', $this->jws));
89
90
        return $request;
91
    }
92
93
    /**
94
     * Create the content of JWS by Json Web Token
95
     *
96
     * @return string
97
     */
98
    private function createJwsContent(): string
99
    {
100
        $jwk = JWKFactory::createFromKeyFile($this->jwt->getPath(), '', [
101
            'kid' => $this->jwt->getKey(),
102
            'alg' => self::ALGORITHM,
103
            'use' => 'sig',
104
        ]);
105
106
        $payload = [
107
            'iss' => $this->jwt->getTeamId(),
108
            'iat' => time(),
109
        ];
110
111
        $header = [
112
            'alg' => self::ALGORITHM,
113
            'kid' => $jwk->get('kid'),
114
        ];
115
116
        return JWSFactory::createJWSToCompactJSON(
117
            $payload,
118
            $jwk,
119
            $header
120
        );
121
    }
122
}
123