Completed
Push — feature/upgrade-remote-vetting ( 883904 )
by
unknown
65:35
created

IdentityProviderFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 1
dl 0
loc 74
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 29 3
A create() 0 8 2
A getAttributeMapping() 0 10 2
1
<?php
2
/**
3
 * Copyright 2010 SURFnet B.V.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting;
19
20
use SAML2\Configuration\PrivateKey;
21
use Surfnet\SamlBundle\Entity\IdentityProvider;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Assert;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\InvalidRemoteVettingIdentityProviderException;
24
25
class IdentityProviderFactory
26
{
27
    /**
28
     * @var IdentityProvider[]
29
     */
30
    private $identityProviders = [];
31
32
    /**
33
     * @var array
34
     */
35
    private $attributeMapping = [];
36
37
    /**
38
     * @param array $configuration
39
     */
40
    public function __construct(array $configuration)
41
    {
42
        foreach ($configuration as $idpConfiguration) {
43
            Assert::keyExists($idpConfiguration, 'name', 'name should be set');
44
            Assert::keyExists($idpConfiguration, 'entityId', 'entityId should be set');
45
            Assert::keyExists($idpConfiguration, 'ssoUrl', 'ssoUrl should be set');
46
            Assert::keyExists($idpConfiguration, 'privateKey', 'privateKey should be set');
47
            Assert::keyExists($idpConfiguration, 'certificateFile', 'certificateFile should be set');
48
            Assert::isArray($idpConfiguration, 'attributeMapping should be an array');
49
            Assert::allString($idpConfiguration['attributeMapping'], 'attributeMapping should consist of strings');
50
51
            Assert::string($idpConfiguration['name'], 'name should be a string');
52
            Assert::url($idpConfiguration['entityId'], 'entityId should be an url');
53
            Assert::url($idpConfiguration['ssoUrl'], 'ssoUrl should be an url');
54
            Assert::file($idpConfiguration['privateKey'], 'privateKey should be a file');
55
            Assert::file($idpConfiguration['certificateFile'], 'certificateFile should be a file');
56
57
            $idpConfiguration['privateKeys'] = [new PrivateKey($idpConfiguration['privateKey'], PrivateKey::NAME_DEFAULT)];
58
            unset($idpConfiguration['privateKey']);
59
60
            // set idp
61
            $this->identityProviders[$idpConfiguration['slug']] = new IdentityProvider($idpConfiguration);
62
63
            // set mapping
64
            foreach ($idpConfiguration['attributeMapping'] as $key => $value) {
65
                $this->attributeMapping[$idpConfiguration['slug']][$key] = $value;
66
            }
67
        }
68
    }
69
70
    /**
71
     * @param string $name
72
     * @return IdentityProvider
73
     */
74
    public function create($name)
75
    {
76
        if (array_key_exists($name, $this->identityProviders)) {
77
            return $this->identityProviders[$name];
78
        }
79
80
        throw new InvalidRemoteVettingIdentityProviderException(sprintf("Invalid IdP requested '%s'", $name));
81
    }
82
83
84
    /**
85
     * @param string $name
86
     * @return array
87
     */
88
    public function getAttributeMapping($name)
89
    {
90
        if (array_key_exists($name, $this->attributeMapping)) {
91
            return $this->attributeMapping[$name];
92
        }
93
94
        throw new InvalidRemoteVettingIdentityProviderException(
95
            sprintf("Unable to find the attribute mapping for an unknown IdP identified by '%s'", $name)
96
        );
97
    }
98
}
99