Failed Conditions
Push — master ( 71e756...c4ef0e )
by Florent
08:21 queued 10s
created

JWEBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Easy;
15
16
use InvalidArgumentException;
17
use Jose\Component\Core\Algorithm;
18
use Jose\Component\Core\AlgorithmManager;
19
use Jose\Component\Core\JWK;
20
use Jose\Component\Core\Util\JsonConverter;
21
use Jose\Component\Encryption\Algorithm\ContentEncryption;
22
use Jose\Component\Encryption\Algorithm\KeyEncryption;
23
use Jose\Component\Encryption\Compression\CompressionMethod;
24
use Jose\Component\Encryption\Compression\CompressionMethodManager;
25
use Jose\Component\Encryption\Compression\Deflate;
26
use Jose\Component\Encryption\JWEBuilder as JoseBuilder;
27
use Jose\Component\Encryption\Serializer\CompactSerializer;
28
29
class JWEBuilder extends AbstractBuilder
30
{
31
    /**
32
     * @var CompressionMethod[]
33
     */
34
    private $compressionMethods;
35
36
    public function __construct()
37
    {
38
        parent::__construct();
39
        $this->compressionMethods = [
40
            new Deflate(),
41
        ];
42
    }
43
44
    /**
45
     * @param Algorithm|string $enc
46
     *
47
     * @throws InvalidArgumentException if the header parameter "enc" is invalid
48
     */
49
    public function enc($enc): self
50
    {
51
        $clone = clone $this;
52
        switch (true) {
53
            case $enc instanceof Algorithm:
54
                $clone->algorithms[] = $enc;
55
                $clone->jwt->header->set('enc', $enc->name());
56
57
                break;
58
            case \is_string($enc):
59
                $clone->jwt->header->set('enc', $enc);
60
61
                break;
62
            default:
63
                throw new InvalidArgumentException('Invalid algorithm');
64
        }
65
66
        return $clone;
67
    }
68
69
    /**
70
     * @param CompressionMethod|string $zip
71
     *
72
     * @throws InvalidArgumentException if the header parameter "zip" is invalid
73
     */
74
    public function zip($zip): self
75
    {
76
        $clone = clone $this;
77
        switch (true) {
78
            case $zip instanceof CompressionMethod:
79
                $clone->compressionMethods[] = $zip;
80
                $clone->jwt->header->set('zip', $zip->name());
81
82
                break;
83
            case \is_string($zip):
84
                $clone->jwt->header->set('zip', $zip);
85
86
                break;
87
            default:
88
                throw new InvalidArgumentException('Invalid compression method');
89
        }
90
91
        return $clone;
92
    }
93
94
    public function encrypt(JWK $jwk): string
95
    {
96
        $builder = new JoseBuilder(
97
            new AlgorithmManager($this->algorithms),
98
            new AlgorithmManager($this->algorithms),
99
            new CompressionMethodManager($this->compressionMethods)
100
        );
101
        $jwe = $builder
102
            ->create()
103
            ->withPayload(JsonConverter::encode($this->jwt->claims->all()))
104
            ->withSharedProtectedHeader($this->jwt->header->all())
105
            ->addRecipient($jwk)
106
            ->build()
107
        ;
108
109
        return (new CompactSerializer())->serialize($jwe);
110
    }
111
112
    protected function getAlgorithmMap(): array
113
    {
114
        return [
115
            KeyEncryption\A128GCMKW::class,
116
            KeyEncryption\A192GCMKW::class,
117
            KeyEncryption\A256GCMKW::class,
118
            KeyEncryption\A128KW::class,
119
            KeyEncryption\A192KW::class,
120
            KeyEncryption\A256KW::class,
121
            KeyEncryption\Dir::class,
122
            KeyEncryption\ECDHES::class,
123
            KeyEncryption\ECDHESA128KW::class,
124
            KeyEncryption\ECDHESA192KW::class,
125
            KeyEncryption\ECDHESA256KW::class,
126
            KeyEncryption\PBES2HS256A128KW::class,
127
            KeyEncryption\PBES2HS384A192KW::class,
128
            KeyEncryption\PBES2HS512A256KW::class,
129
            KeyEncryption\RSA15::class,
130
            KeyEncryption\RSAOAEP::class,
131
            KeyEncryption\RSAOAEP256::class,
132
            ContentEncryption\A128GCM::class,
133
            ContentEncryption\A192GCM::class,
134
            ContentEncryption\A256GCM::class,
135
            ContentEncryption\A128CBCHS256::class,
136
            ContentEncryption\A192CBCHS384::class,
137
            ContentEncryption\A256CBCHS512::class,
138
        ];
139
    }
140
}
141