Dir::getCEK()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
15
16
use Base64Url\Base64Url;
17
use InvalidArgumentException;
18
use Jose\Component\Core\JWK;
19
20
final class Dir implements DirectEncryption
21
{
22
    /**
23
     * @throws InvalidArgumentException if the key is invalid
24
     */
25
    public function getCEK(JWK $key): string
26
    {
27
        if (!\in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
28
            throw new InvalidArgumentException('Wrong key type.');
29
        }
30
        if (!$key->has('k')) {
31
            throw new InvalidArgumentException('The key parameter "k" is missing.');
32
        }
33
        $k = $key->get('k');
34
        if (!\is_string($k)) {
35
            throw new InvalidArgumentException('The key parameter "k" is invalid.');
36
        }
37
38
        return Base64Url::decode($k);
39
    }
40
41
    public function name(): string
42
    {
43
        return 'dir';
44
    }
45
46
    public function allowedKeyTypes(): array
47
    {
48
        return ['oct'];
49
    }
50
51
    public function getKeyManagementMode(): string
52
    {
53
        return self::MODE_DIRECT;
54
    }
55
}
56