Completed
Push — master ( de1564...663938 )
by Florent
10:19
created

ConfigurationHelper::getEncrypterConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 16
nc 1
nop 5
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 SpomkyLabs\JoseBundle\Helper;
13
use Assert\Assertion;
14
15
/**
16
 * This helper will help you to create services configuration
17
 */
18
final class ConfigurationHelper
19
{
20
    /**
21
     * @param string   $name
22
     * @param string[] $header_checkers
23
     * @param string[] $claim_checkers
24
     *
25
     * @return array
26
     */
27
    public static function getCheckerConfiguration($name, array $header_checkers, array $claim_checkers)
28
    {
29
        Assertion::string($name);
30
        Assertion::notEmpty($name);
31
        Assertion::allString($header_checkers);
32
        Assertion::allString($claim_checkers);
33
        return [
34
            'jose' => [
35
                'checkers' => [
36
                    $name => [
37
                        'claims' => $claim_checkers,
38
                        'headers' => $header_checkers,
39
                    ]
40
                ]
41
            ]
42
        ];
43
    }
44
45
    /**
46
     * @param string      $name
47
     * @param string[]    $signature_algorithms
48
     *
49
     * @param bool        $create_verifier
50
     *
51
     * @return array
52
     */
53
    public static function getSignerConfiguration($name, array $signature_algorithms, $create_verifier = false)
54
    {
55
        Assertion::string($name);
56
        Assertion::notEmpty($name);
57
        Assertion::allString($signature_algorithms);
58
        Assertion::notEmpty($signature_algorithms);
59
        Assertion::boolean($create_verifier);
60
        return [
61
            'jose' => [
62
                'signers' => [
63
                    $name => [
64
                        'algorithms' => $signature_algorithms,
65
                        'create_verifier' => $create_verifier,
66
                    ]
67
                ]
68
            ]
69
        ];
70
    }
71
72
    /**
73
     * @param string      $name
74
     * @param string[]    $signature_algorithms
75
     *
76
     *
77
     * @return array
78
     */
79
    public static function getVerifierConfiguration($name, array $signature_algorithms)
80
    {
81
        Assertion::string($name);
82
        Assertion::notEmpty($name);
83
        Assertion::allString($signature_algorithms);
84
        Assertion::notEmpty($signature_algorithms);
85
        return [
86
            'jose' => [
87
                'signers' => [
88
                    $name => [
89
                        'algorithms' => $signature_algorithms,
90
                    ]
91
                ]
92
            ]
93
        ];
94
    }
95
96
    /**
97
     * @param string      $name
98
     * @param string[]    $key_encryption_algorithms
99
     * @param string[]    $content_encryption_algorithms
100
     * @param string[]    $compression_methods
101
     *
102
     * @param bool        $create_decrypter
103
     *
104
     * @return array
105
     */
106
    public static function getEncrypterConfiguration($name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'], $create_decrypter = false)
107
    {
108
        Assertion::string($name);
109
        Assertion::notEmpty($name);
110
        Assertion::allString($key_encryption_algorithms);
111
        Assertion::notEmpty($key_encryption_algorithms);
112
        Assertion::allString($content_encryption_algorithms);
113
        Assertion::notEmpty($content_encryption_algorithms);
114
        Assertion::boolean($create_decrypter);
115
        return [
116
            'jose' => [
117
                'encrypters' => [
118
                    $name => [
119
                        'key_encryption_algorithms' => $key_encryption_algorithms,
120
                        'content_encryption_algorithms' => $content_encryption_algorithms,
121
                        'compression_methods' => $compression_methods,
122
                        'create_decrypter' => $create_decrypter,
123
                    ]
124
                ]
125
            ]
126
        ];
127
    }
128
129
    /**
130
     * @param string      $name
131
     * @param string[]    $key_encryption_algorithms
132
     * @param string[]    $content_encryption_algorithms
133
     * @param string[]    $compression_methods
134
     *
135
     *
136
     * @return array
137
     */
138
    public static function getDecrypterConfiguration($name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'])
139
    {
140
        Assertion::string($name);
141
        Assertion::notEmpty($name);
142
        Assertion::allString($key_encryption_algorithms);
143
        Assertion::notEmpty($key_encryption_algorithms);
144
        Assertion::allString($content_encryption_algorithms);
145
        Assertion::notEmpty($content_encryption_algorithms);
146
        return [
147
            'jose' => [
148
                'decrypters' => [
149
                    $name => [
150
                        'key_encryption_algorithms' => $key_encryption_algorithms,
151
                        'content_encryption_algorithms' => $content_encryption_algorithms,
152
                        'compression_methods' => $compression_methods,
153
                    ]
154
                ]
155
            ]
156
        ];
157
    }
158
159
    /**
160
     * @param string      $name
161
     * @param string      $signer
162
     * @param string|null $encrypter
163
     *
164
     * @return array
165
     */
166
    public static function getJWTCreatorConfiguration($name, $signer, $encrypter = null)
167
    {
168
        Assertion::string($name);
169
        Assertion::notEmpty($name);
170
        Assertion::string($signer);
171
        Assertion::notEmpty($signer);
172
        Assertion::nullOrString($encrypter);
173
        return [
174
            'jose' => [
175
                'jwt_creators' => [
176
                    $name => [
177
                        'signer' => $signer,
178
                        'encrypter' => $encrypter,
179
                    ]
180
                ]
181
            ]
182
        ];
183
    }
184
185
    /**
186
     * @param string      $name
187
     * @param string      $verifier
188
     * @param string      $checker
189
     * @param string|null $decrypter
190
     *
191
     *
192
     * @return array
193
     */
194
    public static function getJWTLoaderConfiguration($name, $verifier, $checker, $decrypter = null)
195
    {
196
        Assertion::string($name);
197
        Assertion::notEmpty($name);
198
        Assertion::string($verifier);
199
        Assertion::notEmpty($verifier);
200
        Assertion::string($checker);
201
        Assertion::notEmpty($checker);
202
        Assertion::nullOrString($decrypter);
203
        return [
204
            'jose' => [
205
                'jwt_loaders' => [
206
                    $name => [
207
                        'verifier' => $verifier,
208
                        'checker' => $checker,
209
                        'decrypter' => $decrypter,
210
                    ]
211
                ]
212
            ]
213
        ];
214
    }
215
}
216