Completed
Push — dev ( 8f8cd7...5b0430 )
by Tristan
16s
created

JwtValidator::signatureIsValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace App\Services\Auth;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use Lcobucci\JWT\Token;
5
use Lcobucci\JWT\Signer\Rsa\Sha256;
6
use App\Services\Auth\JwtKeysFetcher;
7
8
class JwtValidator {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JwtValidator
Loading history...
Coding Style introduced by
Opening brace of a class must be on the line after the definition
Loading history...
9
    
10
    /**
11
     * Will fetch and cache JWT keys
12
     * @var JwtKeysFetcher 
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
13
     */
14
    protected $keyFetcher;
15
    
16
    /**
17
     * A map of valid Issuer->Audience pairs
18
     * @var array 
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
19
     */
20
    protected $validIssAud;
21
    
22
    public function __construct(JwtKeysFetcher $keyFetcher, array $validIssAud) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
23
        $this->keyFetcher = $keyFetcher;
24
        $this->validIssAud = $validIssAud;
25
    }
26
    
27
    public function signatureIsValid(Token $token) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function signatureIsValid()
Loading history...
28
        $kid = $token->getHeader("kid");
29
        $alg = $token->getHeader("alg");
30
        $publicKey = $this->keyFetcher->getByKID($kid);
31
        
32
        switch ($alg) {
33
            case "RS256":
34
                $signer = new Sha256();
35
                break;
36
            default:
37
                $signer = new Sha256();
38
                break;
39
        }
40
41
        $signatureIsValid = $token->verify($signer, $publicKey);
0 ignored issues
show
Bug introduced by
$publicKey of type Lcobucci\JWT\Signer\Key is incompatible with the type string expected by parameter $key of Lcobucci\JWT\Token::verify(). ( Ignorable by Annotation )

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

41
        $signatureIsValid = $token->verify($signer, /** @scrutinizer ignore-type */ $publicKey);
Loading history...
42
        return $signatureIsValid;
43
    }
44
    
45
    public function isExpired(Token $token) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isExpired()
Loading history...
46
        return $token->isExpired();
47
    }
48
    
49
    public function claimsAreValid(Token $token) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function claimsAreValid()
Loading history...
50
        $iss = $token->getClaim("iss");
51
        $aud = $token->getClaim("aud");
52
        return array_has($this->validIssAud, $iss) && 
53
                $this->validIssAud[$iss] === $aud;
54
    }
55
}
56