Completed
Push — master ( 57aa9a...9d09aa )
by Jacob
02:21
created

Generator::__invoke()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 24
ccs 21
cts 21
cp 1
rs 8.5125
cc 5
eloc 18
nc 7
nop 1
crap 5
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Canis.io
4
 * @license   MIT
5
 */
6
namespace Canis\Lumen\Jwt\Adapters\Lcobucci;
7
8
use Lcobucci\JWT\ValidationData;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Canis\Lumen\Jwt\Adapters\Lcobucci\ValidationData.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Lcobucci\JWT\Builder;
10
use Lcobucci\JWT\Signer\Hmac\Sha256;
11
use Lcobucci\JWT\Parser;
12
use Canis\Lumen\Jwt\Token;
13
use Canis\Lumen\Jwt\Contracts\Generator as GeneratorContract;
14
15
class Generator
16
    extends HelperBase
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "HelperBase" and comma; 1 found
Loading history...
17
    implements GeneratorContract
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
18
{
19
    /**
20
     * Generates the token
21
     * @param  array $claims
22
     * @return string
23
     */
24 27
    final public function __invoke(array $claims)
25
    {
26 27
        $signer = new Sha256();
27 27
        $builder = new Builder();
28 27
        $claims = array_merge($this->getDefaultClaims(), $claims, $this->getForcedClaims());
29 27
        if (!$this->checkRequiredClaims(array_keys($claims))) {
30 2
            return false;
31 13
        };
32 25
        foreach ($claims as $claim => $value) {
33 25
            if ($this->isBadClaim($claim)) {
34 1
                continue;
35
            }
36 25
            $builder->set($claim, $value);
37 25
        }
38 25
        $jti = substr(hash('sha256', serialize($claims) . openssl_random_pseudo_bytes(20)), 0, 16);
39 25
        $builder->setId($jti, $this->config['jtiInHeader']);
40 25
        $builder->sign($signer, $this->config['secret']);
41 25
        $token = $builder->getToken();
42 25
        $generatedClaims = $token->getClaims();
43 25
        foreach ($generatedClaims as $key => $value) {
44 25
            $generatedClaims[$key] = $value->getValue();
45 25
        }
46 25
        return new Token((string) $token, $generatedClaims);
47
    }
48
49
    /**
50
     * Default claims (can be overriden)
51
     *
52
     * @return array
53
     */
54 27
    protected function getDefaultClaims()
55
    {
56 27
        $default = [];
57 27
        $default['nbf'] = time() + $this->config['nbfOffset'];
58 27
        $default['exp'] = time() + $this->config['expOffset'];
59 27
        if (!empty($this->config['issuer'])) {
60 27
            $default['iss'] = $this->config['issuer'];
61 27
        }
62 27
        if (!empty($this->config['audience'])) {
63 2
            $default['aud'] = $this->config['audience'];
64 2
        }
65 27
        return $default;
66
    }
67
68
    /**
69
     * Forced claims
70
     *
71
     * @return array
72
     */
73 27
    private function getForcedClaims()
74
    {
75
        return [
76 27
            'iat' => time()
77 27
        ];
78
    }
79
80
    /**
81
     * Checks if claim is bad
82
     *
83
     * @param  string  $claim
84
     * @return boolean
85
     */
86 25
    private function isBadClaim($claim)
87
    {
88 25
        return in_array($claim, ['jti']);
89
    }
90
}
91