Completed
Push — master ( 507af9...d8c018 )
by Florent
02:29
created

JWTCreator::isEncryptionSupportEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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;
13
14
use Assert\Assertion;
15
use Jose\Factory\JWEFactory;
16
use Jose\Factory\JWSFactory;
17
use Jose\Object\JWKInterface;
18
19
final class JWTCreator
20
{
21
    /**
22
     * @var \Jose\EncrypterInterface|null
23
     */
24
    private $encrypter = null;
25
26
    /**
27
     * @var \Jose\SignerInterface
28
     */
29
    private $signer;
30
31
    /**
32
     * JWTCreator constructor.
33
     *
34
     * @param \Jose\SignerInterface $signer
35
     */
36
    public function __construct(SignerInterface $signer)
37
    {
38
        $this->signer = $signer;
39
    }
40
41
    /**
42
     * @param \Jose\EncrypterInterface $encrypter
43
     */
44
    public function enableEncryptionSupport(EncrypterInterface $encrypter)
45
    {
46
        $this->encrypter = $encrypter;
47
    }
48
49
    /**
50
     * @param mixed                     $payload
51
     * @param array                     $signature_protected_headers
52
     * @param \Jose\Object\JWKInterface $signature_key
53
     *
54
     * @return string
55
     */
56
    public function sign($payload, array $signature_protected_headers, JWKInterface $signature_key)
57
    {
58
        $jws = JWSFactory::createJWS($payload);
59
60
        $jws = $jws->addSignatureInformation($signature_key, $signature_protected_headers);
61
        $this->signer->sign($jws);
62
63
        return $jws->toCompactJSON(0);
64
    }
65
66
    /**
67
     * @param string                    $payload
68
     * @param array                     $encryption_protected_headers
69
     * @param \Jose\Object\JWKInterface $encryption_key
70
     *
71
     * @return string
72
     */
73
    public function encrypt($payload, array $encryption_protected_headers, JWKInterface $encryption_key)
74
    {
75
        Assertion::true($this->isEncryptionSupportEnabled(), 'The encryption support is not enabled');
76
77
        $jwe = JWEFactory::createJWE($payload, $encryption_protected_headers);
78
        $jwe = $jwe->addRecipientInformation($encryption_key);
79
        $this->encrypter->encrypt($jwe);
80
81
        return $jwe->toCompactJSON(0);
82
    }
83
84
    /**
85
     * @param mixed                     $payload
86
     * @param array                     $signature_protected_headers
87
     * @param \Jose\Object\JWKInterface $signature_key
88
     * @param array                     $encryption_protected_headers
89
     * @param \Jose\Object\JWKInterface $encryption_key
90
     *
91
     * @return string
92
     */
93
    public function signAndEncrypt($payload, array $signature_protected_headers, JWKInterface $signature_key, array $encryption_protected_headers, JWKInterface $encryption_key)
94
    {
95
        $jws = $this->sign($payload, $signature_protected_headers, $signature_key);
96
        $jwe = $this->encrypt($jws, $encryption_protected_headers, $encryption_key);
97
98
        return $jwe;
99
    }
100
101
    /**
102
     * @return string[]
103
     */
104
    public function getSupportedSignatureAlgorithms()
105
    {
106
        return $this->signer->getSupportedSignatureAlgorithms();
107
    }
108
109
    /**
110
     * @return string[]
111
     */
112
    public function getSupportedKeyEncryptionAlgorithms()
113
    {
114
        return false === $this->isEncryptionSupportEnabled() ? [] : $this->encrypter->getSupportedKeyEncryptionAlgorithms();
115
    }
116
117
    /**
118
     * @return string[]
119
     */
120
    public function getSupportedContentEncryptionAlgorithms()
121
    {
122
        return false === $this->isEncryptionSupportEnabled() ? [] : $this->encrypter->getSupportedContentEncryptionAlgorithms();
123
    }
124
125
    /**
126
     * @return string[]
127
     */
128
    public function getSupportedCompressionMethods()
129
    {
130
        return false === $this->isEncryptionSupportEnabled() ? [] : $this->encrypter->getSupportedCompressionMethods();
131
    }
132
133
    /**
134
     * @return bool
135
     */
136
    public function isEncryptionSupportEnabled()
137
    {
138
        return null !== $this->encrypter;
139
    }
140
}
141