Completed
Push — master ( 727fca...66493a )
by Stig
8s
created

SAMLConfiguration::asArray()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 75
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 75
rs 8.4736
cc 5
eloc 40
nc 16
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
 * Class SAMLConfiguration
4
 *
5
 * This object's job is to convert configuration from SilverStripe config system
6
 * into an array that can be consumed by the Onelogin SAML implementation.
7
 *
8
 * The configuration tells the IdP and SP how to establish the circle of trust - i.e.
9
 * how to exchange certificates and which endpoints to use (e.g. see SAMLConfiguration::metadata).
10
 *
11
 * https://syncplicity.zendesk.com/hc/en-us/articles/202392814-Single-sign-on-with-ADFS
12
 */
13
class SAMLConfiguration extends Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
{
15
    /**
16
     * @var bool
17
     */
18
    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...
19
20
    /**
21
     * @var bool
22
     */
23
    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...
24
25
    /**
26
     * @var array
27
     */
28
    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...
29
30
    /**
31
     * @var array
32
     */
33
    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...
34
35
    /**
36
     * @return array
37
     */
38
    public function asArray()
39
    {
40
        $conf = array();
41
42
        $conf['strict'] = $this->config()->get('strict');
43
        $conf['debug'] = $this->config()->get('debug');
44
45
        // SERVICE PROVIDER SECTION
46
        $sp = $this->config()->get('SP');
47
        $spCertPath = Director::is_absolute($sp['x509cert']) ? $sp['x509cert'] : sprintf('%s/%s', BASE_PATH, $sp['x509cert']);
48
        $spKeyPath = Director::is_absolute($sp['privateKey']) ? $sp['privateKey'] : sprintf('%s/%s', BASE_PATH, $sp['privateKey']);
49
        $conf['sp']['entityId'] = $sp['entityId'];
50
        $conf['sp']['assertionConsumerService'] = array(
51
            'url' => $sp['entityId'] . '/saml/acs',
52
            'binding' => OneLogin_Saml2_Constants::BINDING_HTTP_POST
53
        );
54
        $conf['sp']['NameIDFormat'] = OneLogin_Saml2_Constants::NAMEID_TRANSIENT;
55
        $conf['sp']['x509cert'] = file_get_contents($spCertPath);
56
        $conf['sp']['privateKey'] = file_get_contents($spKeyPath);
57
58
        // IDENTITY PROVIDER SECTION
59
        $idp = $this->config()->get('IdP');
60
        $conf['idp']['entityId'] = $idp['entityId'];
61
        $conf['idp']['singleSignOnService'] = array(
62
            'url' => $idp['singleSignOnService'],
63
            'binding' => OneLogin_Saml2_Constants::BINDING_HTTP_REDIRECT,
64
        );
65
        if (isset($idp['singleLogoutService'])) {
66
            $conf['idp']['singleLogoutService'] = array(
67
                'url' => $idp['singleLogoutService'],
68
                'binding' => OneLogin_Saml2_Constants::BINDING_HTTP_REDIRECT,
69
            );
70
        }
71
72
        $idpCertPath = Director::is_absolute($idp['x509cert']) ? $idp['x509cert'] : sprintf('%s/%s', BASE_PATH, $idp['x509cert']);
73
        $conf['idp']['x509cert'] = file_get_contents($idpCertPath);
74
75
        // SECURITY SECTION
76
        $conf['security'] = array(
77
            /** signatures and encryptions offered */
78
            // Indicates that the nameID of the <samlp:logoutRequest> sent by this SP will be encrypted.
79
            'nameIdEncrypted' => true,
80
            // Indicates whether the <samlp:AuthnRequest> messages sent by this SP will be signed. [Metadata of the SP will offer this info]
81
            'authnRequestsSigned' => true,
82
            // Indicates whether the <samlp:logoutRequest> messages sent by this SP will be signed.
83
            'logoutRequestSigned' => true,
84
            // Indicates whether the <samlp:logoutResponse> messages sent by this SP will be signed.
85
            'logoutResponseSigned' => true,
86
            'signMetadata' => false,
87
            /** signatures and encryptions required **/
88
            // Indicates a requirement for the <samlp:Response>, <samlp:LogoutRequest>
89
            // and <samlp:LogoutResponse> elements received by this SP to be signed.
90
            'wantMessagesSigned' => false,
91
            // Indicates a requirement for the <saml:Assertion> elements received by
92
            // this SP to be signed. [Metadata of the SP will offer this info]
93
            'wantAssertionsSigned' => true,
94
            // Indicates a requirement for the NameID received by
95
            // this SP to be encrypted.
96
            'wantNameIdEncrypted' => false,
97
            // Authentication context.
98
            // Set to false and no AuthContext will be sent in the AuthNRequest,
99
            // Set true or don't present thi parameter and you will get an AuthContext 'exact' 'urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport'
100
            // Set an array with the possible auth context values: array ('urn:oasis:names:tc:SAML:2.0:ac:classes:Password', 'urn:oasis:names:tc:SAML:2.0:ac:classes:X509'),
101
            'requestedAuthnContext' => array(
102
                'urn:federation:authentication:windows',
103
                'urn:oasis:names:tc:SAML:2.0:ac:classes:Password',
104
                'urn:oasis:names:tc:SAML:2.0:ac:classes:X509',
105
            ),
106
            // Indicates if the SP will validate all received xmls.
107
            // (In order to validate the xml, 'strict' and 'wantXMLValidation' must be true).
108
            'wantXMLValidation' => true,
109
        );
110
111
        return $conf;
112
    }
113
}
114