Passed
Push — master ( 879ca4...d0a2dc )
by Robbie
03:13
created

SAMLConfiguration::asArray()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 100
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 8.1463
c 0
b 0
f 0
cc 6
eloc 50
nc 32
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\SAML\Services;
4
5
use OneLogin_Saml2_Constants;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\Control\Director;
8
9
/**
10
 * Class SAMLConfiguration
11
 *
12
 * This object's job is to convert configuration from SilverStripe config system
13
 * into an array that can be consumed by the Onelogin SAML implementation.
14
 *
15
 * The configuration tells the IdP and SP how to establish the circle of trust - i.e.
16
 * how to exchange certificates and which endpoints to use (e.g. see SAMLConfiguration::metadata).
17
 *
18
 * https://syncplicity.zendesk.com/hc/en-us/articles/202392814-Single-sign-on-with-ADFS
19
 *
20
 * @package activedirectory
21
 */
22
class SAMLConfiguration
23
{
24
    use Injectable;
25
26
    /**
27
     * @var bool
28
     */
29
    private static $strict;
0 ignored issues
show
Unused Code introduced by
The property $strict is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    /**
32
     * @var bool
33
     */
34
    private static $debug;
0 ignored issues
show
Unused Code introduced by
The property $debug is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * @var array
38
     */
39
    private static $SP;
0 ignored issues
show
Unused Code introduced by
The property $SP is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
41
    /**
42
     * @var array
43
     */
44
    private static $IdP;
0 ignored issues
show
Unused Code introduced by
The property $IdP is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
45
46
    /**
47
     * @return array
48
     */
49
    public function asArray()
50
    {
51
        $conf = [];
52
53
        $conf['strict'] = $this->config()->get('strict');
0 ignored issues
show
Bug introduced by
The method config() does not exist on SilverStripe\SAML\Services\SAMLConfiguration. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        $conf['strict'] = $this->/** @scrutinizer ignore-call */ config()->get('strict');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        $conf['debug'] = $this->config()->get('debug');
55
56
        // SERVICE PROVIDER SECTION
57
        $sp = $this->config()->get('SP');
58
        $spCertPath = Director::is_absolute($sp['x509cert'])
59
            ? $sp['x509cert']
60
            : sprintf('%s/%s', BASE_PATH, $sp['x509cert']);
61
        $spKeyPath = Director::is_absolute($sp['privateKey'])
62
            ? $sp['privateKey']
63
            : sprintf('%s/%s', BASE_PATH, $sp['privateKey']);
64
65
        // set baseurl for SAML messages coming back to the SP
66
        $conf['baseurl'] = $sp['entityId'];
67
68
        $conf['sp']['entityId'] = $sp['entityId'];
69
        $conf['sp']['assertionConsumerService'] = [
70
            'url' => $sp['entityId'] . '/saml/acs',
71
            'binding' => OneLogin_Saml2_Constants::BINDING_HTTP_POST
72
        ];
73
        $conf['sp']['NameIDFormat'] = isset($sp['nameIdFormat']) ?
74
            $sp['nameIdFormat'] : OneLogin_Saml2_Constants::NAMEID_TRANSIENT;
75
        $conf['sp']['x509cert'] = file_get_contents($spCertPath);
76
        $conf['sp']['privateKey'] = file_get_contents($spKeyPath);
77
78
        // IDENTITY PROVIDER SECTION
79
        $idp = $this->config()->get('IdP');
80
        $conf['idp']['entityId'] = $idp['entityId'];
81
        $conf['idp']['singleSignOnService'] = [
82
            'url' => $idp['singleSignOnService'],
83
            'binding' => OneLogin_Saml2_Constants::BINDING_HTTP_REDIRECT,
84
        ];
85
        if (isset($idp['singleLogoutService'])) {
86
            $conf['idp']['singleLogoutService'] = [
87
                'url' => $idp['singleLogoutService'],
88
                'binding' => OneLogin_Saml2_Constants::BINDING_HTTP_REDIRECT,
89
            ];
90
        }
91
92
        $idpCertPath = Director::is_absolute($idp['x509cert'])
93
            ? $idp['x509cert']
94
            : sprintf('%s/%s', BASE_PATH, $idp['x509cert']);
95
        $conf['idp']['x509cert'] = file_get_contents($idpCertPath);
96
97
        // SECURITY SECTION
98
        $security = $this->config()->get('Security');
99
        $signatureAlgorithm = $security['signatureAlgorithm'];
100
101
        $conf['security'] = [
102
            /** signatures and encryptions offered */
103
            // Indicates that the nameID of the <samlp:logoutRequest> sent by this SP will be encrypted.
104
            'nameIdEncrypted' => true,
105
            // Indicates whether the <samlp:AuthnRequest> messages sent by this SP will be signed. [Metadata of the
106
            // SP will offer this info]
107
            'authnRequestsSigned' => true,
108
            // Indicates whether the <samlp:logoutRequest> messages sent by this SP will be signed.
109
            'logoutRequestSigned' => true,
110
            // Indicates whether the <samlp:logoutResponse> messages sent by this SP will be signed.
111
            'logoutResponseSigned' => true,
112
            'signMetadata' => false,
113
            /** signatures and encryptions required **/
114
            // Indicates a requirement for the <samlp:Response>, <samlp:LogoutRequest>
115
            // and <samlp:LogoutResponse> elements received by this SP to be signed.
116
            'wantMessagesSigned' => false,
117
            // Indicates a requirement for the <saml:Assertion> elements received by
118
            // this SP to be signed. [Metadata of the SP will offer this info]
119
            'wantAssertionsSigned' => true,
120
            // Indicates a requirement for the NameID received by
121
            // this SP to be encrypted.
122
            'wantNameIdEncrypted' => false,
123
124
            // Algorithm that the toolkit will use on signing process. Options:
125
            //  - 'http://www.w3.org/2000/09/xmldsig#rsa-sha1'
126
            //  - 'http://www.w3.org/2000/09/xmldsig#dsa-sha1'
127
            //  - 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'
128
            //  - 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384'
129
            //  - 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512'
130
            'signatureAlgorithm' => $signatureAlgorithm,
131
132
            // Authentication context.
133
            // Set to false and no AuthContext will be sent in the AuthNRequest,
134
            // Set true or don't present thi parameter and you will get an AuthContext
135
            // 'exact' 'urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport'
136
            // Set an array with the possible auth context values:
137
            // array ('urn:oasis:names:tc:SAML:2.0:ac:classes:Password', 'urn:oasis:names:tc:SAML:2.0:ac:classes:X509'),
138
            'requestedAuthnContext' => [
139
                'urn:federation:authentication:windows',
140
                'urn:oasis:names:tc:SAML:2.0:ac:classes:Password',
141
                'urn:oasis:names:tc:SAML:2.0:ac:classes:X509',
142
            ],
143
            // Indicates if the SP will validate all received xmls.
144
            // (In order to validate the xml, 'strict' and 'wantXMLValidation' must be true).
145
            'wantXMLValidation' => true,
146
        ];
147
148
        return $conf;
149
    }
150
}
151