Completed
Pull Request — master (#24)
by Florent
11:12 queued 08:26
created

ConfigurationHelper::getEncrypterConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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