Completed
Push — develop ( db3d66...686594 )
by Florent
02:42
created

JWEFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 5
c 7
b 1
f 2
lcom 1
cbo 5
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createJWEToCompactJSON() 0 6 1
A createJWEToFlattenedJSON() 0 6 1
A createJWE() 0 13 2
A createJWEAndEncrypt() 0 15 1
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\Object\JWE;
16
use Jose\Object\JWKInterface;
17
use Psr\Log\LoggerInterface;
18
19
final class JWEFactory
20
{
21
    /**
22
     * @param mixed       $payload
23
     * @param array       $shared_protected_headers
24
     * @param array       $shared_headers
25
     * @param null|string $aad
26
     *
27
     * @return \Jose\Object\JWEInterface
28
     */
29
    public static function createJWE($payload, array $shared_protected_headers = [], array $shared_headers = [], $aad = null)
30
    {
31
        $jwe = new JWE();
32
        $jwe = $jwe->withSharedProtectedHeaders($shared_protected_headers);
33
        $jwe = $jwe->withSharedHeaders($shared_headers);
34
        $jwe = $jwe->withPayload($payload);
35
36
        if (null !== $aad) {
37
            $jwe = $jwe->withAAD($aad);
38
        }
39
40
        return $jwe;
41
    }
42
43
    /**
44
     * @param mixed                         $payload
45
     * @param \Jose\Object\JWKInterface     $recipient_key
46
     * @param array                         $shared_protected_headers
47
     * @param \Psr\Log\LoggerInterface|null $logger
48
     *
49
     * @return string
50
     */
51
    public static function createJWEToCompactJSON($payload, JWKInterface $recipient_key, array $shared_protected_headers, LoggerInterface $logger = null)
52
    {
53
        $jwe = self::createJWEAndEncrypt($payload, $recipient_key, $shared_protected_headers, [], [], null, $logger);
54
55
        return $jwe->toCompactJSON(0);
56
    }
57
58
    /**
59
     * @param mixed                         $payload
60
     * @param \Jose\Object\JWKInterface     $recipient_key
61
     * @param array                         $shared_protected_headers
62
     * @param array                         $shared_headers
63
     * @param array                         $recipient_headers
64
     * @param string|null                   $aad
65
     * @param \Psr\Log\LoggerInterface|null $logger
66
     *
67
     * @return \Jose\Object\JWSInterface
68
     */
69
    public static function createJWEToFlattenedJSON($payload, JWKInterface $recipient_key, array $shared_protected_headers = [], $shared_headers = [], $recipient_headers = [], $aad = null, LoggerInterface $logger = null)
70
    {
71
        $jwe = self::createJWEAndEncrypt($payload, $recipient_key, $shared_protected_headers, $shared_headers, $recipient_headers, $aad, $logger);
72
73
        return $jwe->toFlattenedJSON(0);
74
    }
75
76
    /**
77
     * @param mixed                         $payload
78
     * @param \Jose\Object\JWKInterface     $recipient_key
79
     * @param array                         $shared_protected_headers
80
     * @param array                         $shared_headers
81
     * @param array                         $recipient_headers
82
     * @param string|null                   $aad
83
     * @param \Psr\Log\LoggerInterface|null $logger
84
     *
85
     * @return \Jose\Object\JWSInterface
86
     */
87
    private static function createJWEAndEncrypt($payload, JWKInterface $recipient_key, array $shared_protected_headers = [], $shared_headers = [], $recipient_headers = [], $aad = null, LoggerInterface $logger = null)
88
    {
89
        $complete_headers = array_merge($shared_protected_headers, $shared_headers, $recipient_headers);
90
        Assertion::keyExists($complete_headers, 'alg', 'No "alg" parameter set in the header');
91
        Assertion::keyExists($complete_headers, 'enc', 'No "enc" parameter set in the header');
92
        $encrypter = EncrypterFactory::createEncrypter([$complete_headers['alg'], $complete_headers['enc']], ['DEF', 'ZLIB', 'GZ'], $logger);
93
94
        $jwe = self::createJWE($payload, $shared_protected_headers, $shared_headers, $aad);
95
96
        $jwe = $jwe->addRecipient($recipient_key, $recipient_headers);
97
98
        $encrypter->encrypt($jwe);
99
100
        return $jwe;
101
    }
102
}
103