Completed
Push — master ( 8e9c64...20353b )
by Florent
04:07
created

JWAFactory::getSupportedAlgorithms()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
rs 8.8571
cc 1
eloc 38
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 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\Factory;
13
14
15
use Jose\Algorithm\JWAManager;
16
17
final class JWAFactory
18
{
19
    /**
20
     * @param array $algorithms
21
     *
22
     * @return \Jose\Algorithm\JWAManager
23
     */
24
    public function createAlgorithmManager(array $algorithms)
25
    {
26
        $jwa_manager = new JWAManager();
27
28
        foreach($algorithms as $algorithm) {
29
            $class = $this->getAlgorithmClass($algorithm);
30
            $jwa_manager->addAlgorithm(new $class());
31
        }
32
33
        return $jwa_manager;
34
    }
35
36
    /**
37
     * @param string $algorithm
38
     *
39
     * @return bool
40
     */
41
    private function isAlgorithmSupported($algorithm)
42
    {
43
        return array_key_exists($algorithm, $this->getSupportedAlgorithms());
44
    }
45
46
    /**
47
     * @param string $algorithm
48
     *
49
     * @throws \InvalidArgumentException
50
     *
51
     * @return string
52
     */
53
    private function getAlgorithmClass($algorithm)
54
    {
55
        if ($this->isAlgorithmSupported($algorithm)) {
56
            return $this->getSupportedAlgorithms()[$algorithm];
57
        }
58
        throw new \InvalidArgumentException(sprintf('Algorithm "%s" is not supported.', $algorithm));
59
    }
60
61
    private function getSupportedAlgorithms()
62
    {
63
        return [
64
            'HS256'              => '\Jose\Algorithm\Signature\HS256',
65
            'HS384'              => '\Jose\Algorithm\Signature\HS384',
66
            'HS512'              => '\Jose\Algorithm\Signature\HS512',
67
            'ES256'              => '\Jose\Algorithm\Signature\ES256',
68
            'ES384'              => '\Jose\Algorithm\Signature\ES384',
69
            'ES512'              => '\Jose\Algorithm\Signature\ES512',
70
            'none'               => '\Jose\Algorithm\Signature\None',
71
            'RS256'              => '\Jose\Algorithm\Signature\RS256',
72
            'RS384'              => '\Jose\Algorithm\Signature\RS384',
73
            'RS512'              => '\Jose\Algorithm\Signature\RS512',
74
            'PS256'              => '\Jose\Algorithm\Signature\PS256',
75
            'PS384'              => '\Jose\Algorithm\Signature\PS384',
76
            'PS512'              => '\Jose\Algorithm\Signature\PS512',
77
            'A128GCM'            => '\Jose\Algorithm\ContentEncryption\A128GCM',
78
            'A192GCM'            => '\Jose\Algorithm\ContentEncryption\A192GCM',
79
            'A256GCM'            => '\Jose\Algorithm\ContentEncryption\A256GCM',
80
            'A128CBC-HS256'      => '\Jose\Algorithm\ContentEncryption\A128CBCHS256',
81
            'A192CBC-HS384'      => '\Jose\Algorithm\ContentEncryption\A192CBCHS384',
82
            'A256CBC-HS512'      => '\Jose\Algorithm\ContentEncryption\A256CBCHS512',
83
            'A128KW'             => '\Jose\Algorithm\KeyEncryption\A128KW',
84
            'A192KW'             => '\Jose\Algorithm\KeyEncryption\A192KW',
85
            'A256KW'             => '\Jose\Algorithm\KeyEncryption\A256KW',
86
            'A128GCMKW'          => '\Jose\Algorithm\KeyEncryption\A128GCMKW',
87
            'A192GCMKW'          => '\Jose\Algorithm\KeyEncryption\A192GCMKW',
88
            'A256GCMKW'          => '\Jose\Algorithm\KeyEncryption\A256GCMKW',
89
            'dir'                => '\Jose\Algorithm\KeyEncryption\Dir',
90
            'ECDH-ES'            => '\Jose\Algorithm\KeyEncryption\ECDHES',
91
            'ECDH-ES+A128KW'     => '\Jose\Algorithm\KeyEncryption\ECDHESA128KW',
92
            'ECDH-ES+A192KW'     => '\Jose\Algorithm\KeyEncryption\ECDHESA192KW',
93
            'ECDH-ES+A256KW'     => '\Jose\Algorithm\KeyEncryption\ECDHESA256KW',
94
            'PBES2-HS256+A128KW' => '\Jose\Algorithm\KeyEncryption\PBES2HS256A128KW',
95
            'PBES2-HS384+A192KW' => '\Jose\Algorithm\KeyEncryption\PBES2HS384A192KW',
96
            'PBES2-HS512+A256KW' => '\Jose\Algorithm\KeyEncryption\PBES2HS512A256KW',
97
            'RSA1_5'             => '\Jose\Algorithm\KeyEncryption\RSA15',
98
            'RSA-OAEP'           => '\Jose\Algorithm\KeyEncryption\RSAOAEP',
99
            'RSA-OAEP-256'       => '\Jose\Algorithm\KeyEncryption\RSAOAEP256',
100
        ];
101
    }
102
}
103