TokenSignatory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A __invoke() 0 4 1
1
<?php
2
3
namespace Schnittstabil\Csrf\TokenService;
4
5
use Base64Url\Base64Url;
6
7
/**
8
 * A TokenSignatory.
9
 */
10
class TokenSignatory
11
{
12
    protected $key;
13
    protected $algo;
14
    protected $base64url;
15
16
    /**
17
     * Create a new TokenSignatory.
18
     *
19
     * @param string $key  Shared secret key used for generating token signatures
20
     * @param string $algo Name of hashing algorithm. See hash_algos() for a list of supported algorithms
21
     */
22
    public function __construct($key, $algo = 'SHA512')
23
    {
24
        if (empty($key)) {
25
            throw new \InvalidArgumentException('Key may not be empty');
26
        }
27
28
        $this->key = $key;
29
        $this->algo = $algo;
30
        $this->base64url = new Base64Url();
31
    }
32
33
    /**
34
     * Sign a Base64Url encoded token payload.
35
     *
36
     * @param string $tokenPayload The Base64Url encoded token payload
37
     *
38
     * @return string Base64Url encoded signature
39
     */
40
    public function __invoke($tokenPayload)
41
    {
42
        return $this->base64url->encode(hash_hmac($this->algo, $tokenPayload, $this->key, true));
43
    }
44
}
45