Test Failed
Pull Request — master (#88)
by Artem
04:05
created

ConfigResolver::resolveNameIdFormatPrefix()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
nc 5
nop 1
dl 0
loc 10
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace Slides\Saml2\Resolvers;
4
5
use Slides\Saml2\Contracts\IdentityProvidable;
6
use Slides\Saml2\Contracts\ResolvesIdpConfig;
7
use Slides\Saml2\Exceptions\ConfigurationException;
8
9
class ConfigResolver implements ResolvesIdpConfig
10
{
11
    /**
12
     * Adjust SAML configuration for the given identity provider.
13
     *
14
     * @param IdentityProvidable $idp
15
     * @param array $config
16
     *
17
     * @return array
18
     */
19
    public function resolve(IdentityProvidable $idp, array $config): array
20
    {
21
        if ($idp->idpX509cert() === null) {
22
            throw new ConfigurationException('Identity Provider certificate is missing');
23
        }
24
25
        $config['idp'] = [
26
            'entityId' => $idp->idpEntityId(),
27
            'singleSignOnService' => ['url' => $idp->idpLoginUrl()],
28
            'singleLogoutService' => ['url' => $idp->idpLogoutUrl()],
29
            'x509cert' => $idp->idpX509cert()
30
        ];
31
32
        $config['sp']['NameIDFormat'] = $this->resolveNameIdFormatPrefix($idp->idpNameIdFormat());
33
34
        return $config;
35
    }
36
37
    /**
38
     * Resolve the Name ID Format prefix.
39
     *
40
     * @param string $format
41
     *
42
     * @return string
43
     */
44
    protected function resolveNameIdFormatPrefix(string $format): string
45
    {
46
        switch ($format) {
47
            case 'emailAddress':
48
            case 'X509SubjectName':
49
            case 'WindowsDomainQualifiedName':
50
            case 'unspecified':
51
                return 'urn:oasis:names:tc:SAML:1.1:nameid-format:' . $format;
52
            default:
53
                return 'urn:oasis:names:tc:SAML:2.0:nameid-format:'. $format;
54
        }
55
    }
56
}
57