Completed
Push — master ( d8c018...19ac78 )
by Florent
06:38
created

JWEFactory::createJWE()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 4
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Factory;
13
14
use Assert\Assertion;
15
use Jose\Encrypter;
16
use Jose\Object\JWE;
17
use Jose\Object\JWKInterface;
18
use Psr\Log\LoggerInterface;
19
20
final class JWEFactory
21
{
22
    /**
23
     * @param mixed       $payload
24
     * @param array       $shared_protected_headers
25
     * @param array       $shared_headers
26
     * @param null|string $aad
27
     *
28
     * @return \Jose\Object\JWEInterface
29
     */
30
    public static function createJWE($payload, array $shared_protected_headers = [], array $shared_headers = [], $aad = null)
31
    {
32
        $jwe = new JWE();
33
        $jwe = $jwe->withSharedProtectedHeaders($shared_protected_headers);
34
        $jwe = $jwe->withSharedHeaders($shared_headers);
35
        $jwe = $jwe->withPayload($payload);
36
37
        if (null !== $aad) {
38
            $jwe = $jwe->withAAD($aad);
39
        }
40
41
        return $jwe;
42
    }
43
44
    /**
45
     * @param mixed                         $payload
46
     * @param \Jose\Object\JWKInterface     $recipient_key
47
     * @param array                         $shared_protected_headers
48
     * @param \Psr\Log\LoggerInterface|null $logger
49
     *
50
     * @return string
51
     */
52
    public static function createJWEToCompactJSON($payload, JWKInterface $recipient_key, array $shared_protected_headers, LoggerInterface $logger = null)
53
    {
54
        $jwe = self::createJWEAndEncrypt($payload, $recipient_key, $shared_protected_headers, [], [], null, $logger);
55
56
        return $jwe->toCompactJSON(0);
57
    }
58
59
    /**
60
     * @param mixed                         $payload
61
     * @param \Jose\Object\JWKInterface     $recipient_key
62
     * @param array                         $shared_protected_headers
63
     * @param array                         $shared_headers
64
     * @param array                         $recipient_headers
65
     * @param string|null                   $aad
66
     * @param \Psr\Log\LoggerInterface|null $logger
67
     *
68
     * @return \Jose\Object\JWSInterface
69
     */
70
    public static function createJWEToFlattenedJSON($payload, JWKInterface $recipient_key, array $shared_protected_headers = [], $shared_headers = [], $recipient_headers = [], $aad = null, LoggerInterface $logger = null)
71
    {
72
        $jwe = self::createJWEAndEncrypt($payload, $recipient_key, $shared_protected_headers, $shared_headers, $recipient_headers, $aad, $logger);
73
74
        return $jwe->toFlattenedJSON(0);
75
    }
76
77
    /**
78
     * @param mixed                         $payload
79
     * @param \Jose\Object\JWKInterface     $recipient_key
80
     * @param array                         $shared_protected_headers
81
     * @param array                         $shared_headers
82
     * @param array                         $recipient_headers
83
     * @param string|null                   $aad
84
     * @param \Psr\Log\LoggerInterface|null $logger
85
     *
86
     * @return \Jose\Object\JWSInterface
87
     */
88
    private static function createJWEAndEncrypt($payload, JWKInterface $recipient_key, array $shared_protected_headers = [], $shared_headers = [], $recipient_headers = [], $aad = null, LoggerInterface $logger = null)
89
    {
90
        $complete_headers = array_merge($shared_protected_headers, $shared_headers, $recipient_headers);
91
        Assertion::keyExists($complete_headers, 'alg', 'No "alg" parameter set in the header');
92
        Assertion::keyExists($complete_headers, 'enc', 'No "enc" parameter set in the header');
93
        $encrypter = Encrypter::createEncrypter([$complete_headers['alg']], [$complete_headers['enc']], ['DEF', 'ZLIB', 'GZ'], $logger);
94
95
        $jwe = self::createJWE($payload, $shared_protected_headers, $shared_headers, $aad);
96
97
        $jwe = $jwe->addRecipientInformation($recipient_key, $recipient_headers);
98
99
        $encrypter->encrypt($jwe);
100
101
        return $jwe;
102
    }
103
}
104