Passed
Pull Request — develop (#295)
by Peter
04:30
created

SamlEntity::toIdentityProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Entity;
20
21
use Doctrine\ORM\Mapping as ORM;
22
use GuzzleHttp;
23
use Surfnet\SamlBundle\Entity\IdentityProvider;
24
use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException;
25
26
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
 * @ORM\Entity(repositoryClass="Surfnet\StepupGateway\GatewayBundle\Entity\DoctrineSamlEntityRepository")
28
 * @ORM\Table()
29
 *
30
 * @SuppressWarnings(PHPMD.UnusedPrivateField)
31
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
32
class SamlEntity
33
{
34
    /**
35
     * Constants denoting the type of SamlEntity. Also used in the middleware to make that distinction
36
     */
37
    const TYPE_IDP = 'idp';
38
    const TYPE_SP = 'sp';
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @var string
42
     *
43
     * @ORM\Id
44
     * @ORM\Column(length=36)
45
     */
46
    private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
Coding Style introduced by
Private member variable "id" must be prefixed with an underscore
Loading history...
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @ORM\Column
50
     *
51
     * @var string
52
     */
53
    private $entityId;
0 ignored issues
show
Coding Style introduced by
Private member variable "entityId" must be prefixed with an underscore
Loading history...
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @ORM\Column
57
     *
58
     * @var string
59
     */
60
    private $type;
0 ignored issues
show
Coding Style introduced by
Private member variable "type" must be prefixed with an underscore
Loading history...
61
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @ORM\Column(type="text")
64
     *
65
     * @var string the configuration as json string
66
     */
67
    private $configuration;
0 ignored issues
show
Coding Style introduced by
Private member variable "configuration" must be prefixed with an underscore
Loading history...
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @return IdentityProvider
71
     */
72
    public function toIdentityProvider()
73
    {
74
        if (!$this->type === self::TYPE_IDP) {
0 ignored issues
show
introduced by
The condition ! $this->type === self::TYPE_IDP is always false.
Loading history...
75
            throw new RuntimeException(sprintf(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
76
                'Cannot cast a SAMLEntity to an IdentityProvider if it is not of the type "%s", current type: "%s"',
77
                self::TYPE_IDP,
78
                $this->type
79
            ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
80
        }
81
82
        $decodedConfiguration = $this->decodeConfiguration();
83
84
        // index based will be supported later on
85
        $configuration = [];
86
        $configuration['entityId']             = $this->entityId;
87
        $configuration['configuredLoas']       = $decodedConfiguration['loa'];
88
89
        return new IdentityProvider($configuration);
90
    }
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
93
     * @return ServiceProvider
94
     */
95
    public function toServiceProvider()
96
    {
97
        if (!$this->type === self::TYPE_SP) {
0 ignored issues
show
introduced by
The condition ! $this->type === self::TYPE_SP is always false.
Loading history...
98
            throw new RuntimeException(sprintf(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
99
                'Cannot cast a SAMLEntity to a ServiceProvider if it is not of the type "%s", current type: "%s"',
100
                self::TYPE_SP,
101
                $this->type
102
            ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
103
        }
104
105
        $decodedConfiguration = $this->decodeConfiguration();
106
107
        // Note that we don't set 'assertionConsumerUrl',
108
        // getAssertionConsumerUrl() on this service provider entity will
109
        // yield null. The ACS URL in the AuthnRequest is used instead, and
110
        // this URL is validated by matching against the configured 'allowed
111
        // ACS locations'. If it doesn't match, the gateway will fall back to
112
        // the first configured ACS location.
113
        $configuration = [];
114
        $configuration['allowedAcsLocations'] = $decodedConfiguration['acs'];
115
        $configuration['certificateData']     = $decodedConfiguration['public_key'];
116
        $configuration['entityId']            = $this->entityId;
117
        $configuration['configuredLoas']      = $decodedConfiguration['loa'];
118
119
        $configuration['secondFactorOnly'] = false;
120
        // Allow the sp to evaluate the SSO on 2FA cookie if present? (defaults to false)
121
        $configuration['allowSsoOn2fa'] = false;
122
        // Is the SP allowed to set a SSO on 2FA cookie in Gateway? (defautls to false)
123
        $configuration['setSsoCookieOn2fa'] = false;
124
125
        if (isset($decodedConfiguration['second_factor_only'])) {
126
            $configuration['secondFactorOnly'] = $decodedConfiguration['second_factor_only'];
127
        }
128
        $configuration['secondFactorOnlyNameIdPatterns'] = [];
129
        if (isset($decodedConfiguration['second_factor_only_nameid_patterns'])) {
130
            $configuration['secondFactorOnlyNameIdPatterns'] =
0 ignored issues
show
Coding Style introduced by
Multi-line assignments must have the equal sign on the second line
Loading history...
131
                $decodedConfiguration['second_factor_only_nameid_patterns'];
132
        }
133
        if (isset($decodedConfiguration['allow_sso_on_2fa'])) {
134
            $configuration['allowSsoOn2fa'] = $decodedConfiguration['allow_sso_on_2fa'];
135
        }
136
        if (isset($decodedConfiguration['set_sso_cookie_on_2fa'])) {
137
            $configuration['setSsoCookieOn2fa'] = $decodedConfiguration['set_sso_cookie_on_2fa'];
138
        }
139
        return new ServiceProvider($configuration);
140
    }
141
142
    /**
143
     * Returns the decoded configuration
144
     *
145
     * @return array
146
     */
147
    private function decodeConfiguration()
0 ignored issues
show
Coding Style introduced by
Private method name "SamlEntity::decodeConfiguration" must be prefixed with an underscore
Loading history...
148
    {
149
        return GuzzleHttp\json_decode($this->configuration, true);
150
    }
151
}
152