Completed
Push — master ( 022625...2b82d4 )
by Florent
05:58
created

AlgorithmManagerFactory::isAlgorithmSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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