Completed
Push — master ( 4b394d...0ca4f0 )
by Damien
09:55
created

EntityDescriptorHelper   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 0
loc 238
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdpDescriptors() 0 4 1
A getSpDescriptors() 0 4 1
A getDescriptors() 0 12 3
A getFirstArtifactResolutionService() 0 4 1
A getFirstSLOService() 0 4 1
A getFirstManageNameIDService() 0 4 1
A getFirstSpAssertionConsumerService() 0 4 1
A getFirstSpAttributeConsumingService() 0 4 1
A getFirstIdpSSOService() 0 4 1
A getFirstIdpAssertionIdRequestService() 0 4 1
A getFirstIdpNameIDMappingService() 0 4 1
A getFirstService() 0 37 4
B getFirstIndexedEndpointType() 0 31 6
1
<?php
2
3
4
namespace flipbox\saml\core\helpers;
5
6
7
use flipbox\saml\core\AbstractPlugin;
8
use SAML2\XML\md\EndpointType;
9
use SAML2\XML\md\EntityDescriptor;
10
use SAML2\XML\md\IDPSSODescriptor;
11
use SAML2\XML\md\IndexedEndpointType;
12
use SAML2\XML\md\SPSSODescriptor;
13
use SAML2\XML\md\SSODescriptorType;
14
15
class EntityDescriptorHelper
16
{
17
    const ENDPOINT_SERVICE_ARTIFACT_RESOLUTION = 'ArtifactResolution';
18
    const ENDPOINT_SERVICE_SINGLE_LOGOUT = 'SingleLogout';
19
    const ENDPOINT_SERVICE_MANAGE_NAME_ID = 'ManageNameID';
20
21
    const ENDPOINT_SERVICE_ASSERTION_CONSUMER = 'AssertionConsumer';
22
    const ENDPOINT_SERVICE_ATTRIBUTE_CONSUMING = 'AttributeConsuming';
23
24
    const ENDPOINT_SERVICE_SINGLE_SIGN_ON = 'SingleSignOn';
25
    const ENDPOINT_SERVICE_ASSERTION_ID_REQUEST = 'AssertionIDRequest';
26
    const ENDPOINT_SERVICE_NAME_ID_MAPPING = 'NameIDMapping';
27
28
    const ENDPOINT_SERVICE_OPTIONS = [
29
        // Common
30
        self::ENDPOINT_SERVICE_ARTIFACT_RESOLUTION,
31
        self::ENDPOINT_SERVICE_SINGLE_LOGOUT,
32
        self::ENDPOINT_SERVICE_MANAGE_NAME_ID,
33
34
        // SP
35
        self::ENDPOINT_SERVICE_ASSERTION_CONSUMER,
36
        self::ENDPOINT_SERVICE_ATTRIBUTE_CONSUMING,
37
38
        // IDP
39
        self::ENDPOINT_SERVICE_SINGLE_SIGN_ON,
40
        self::ENDPOINT_SERVICE_ASSERTION_ID_REQUEST,
41
        self::ENDPOINT_SERVICE_NAME_ID_MAPPING,
42
    ];
43
44
    /**
45
     * @param EntityDescriptor $entityDescriptor
46
     * @return IDPSSODescriptor[]
47
     */
48
    public static function getIdpDescriptors(EntityDescriptor $entityDescriptor)
49
    {
50
        return static::getDescriptors($entityDescriptor, IDPSSODescriptor::class);
51
    }
52
53
    /**
54
     * @param EntityDescriptor $entityDescriptor
55
     * @return SPSSODescriptor[]
56
     */
57
    public static function getSpDescriptors(EntityDescriptor $entityDescriptor)
58
    {
59
        return static::getDescriptors($entityDescriptor, SPSSODescriptor::class);
60
    }
61
62
    /**
63
     * @param EntityDescriptor $entityDescriptor
64
     * @param string $type
65
     * @return SSODescriptorType[]
66
     */
67
    protected static function getDescriptors(EntityDescriptor $entityDescriptor, string $type)
68
    {
69
        $descriptors = [];
70
        foreach ($entityDescriptor->getRoleDescriptor() as $roleDescriptor) {
71
            if ($roleDescriptor instanceof $type) {
72
                $descriptors[] = $roleDescriptor;
73
            }
74
        }
75
76
        return $descriptors;
77
78
    }
79
80
    /**
81
     * Common
82
     */
83
84
    /**
85
     * @param SSODescriptorType[] $roleDescriptors
86
     * @param string $binding
87
     * @return EndpointType|null
88
     */
89
    public static function getFirstArtifactResolutionService(array $roleDescriptors, string $binding = null)
90
    {
91
        return static::getFirstService(self::ENDPOINT_SERVICE_ARTIFACT_RESOLUTION, $roleDescriptors, $binding);
92
    }
93
94
    /**
95
     * @param SSODescriptorType[] $roleDescriptors
96
     * @param string $binding
97
     * @return EndpointType|null
98
     */
99
    public static function getFirstSLOService(array $roleDescriptors, string $binding = null)
100
    {
101
        return static::getFirstService(self::ENDPOINT_SERVICE_SINGLE_LOGOUT, $roleDescriptors, $binding);
102
    }
103
104
    /**
105
     * @param SSODescriptorType[] $roleDescriptors
106
     * @param string $binding
107
     * @return EndpointType|null
108
     */
109
    public static function getFirstManageNameIDService(array $roleDescriptors, string $binding = null)
110
    {
111
        return static::getFirstService(self::ENDPOINT_SERVICE_MANAGE_NAME_ID, $roleDescriptors, $binding);
112
    }
113
114
    /**
115
     * Get First SP Services
116
     */
117
118
    /**
119
     * @param SPSSODescriptor[] $roleDescriptors
120
     * @param string $binding
121
     * @return EndpointType|null
122
     */
123
    public static function getFirstSpAssertionConsumerService(array $roleDescriptors, string $binding = null)
124
    {
125
        return static::getFirstService(self::ENDPOINT_SERVICE_ASSERTION_CONSUMER, $roleDescriptors, $binding);
126
    }
127
128
    /**
129
     * @param SPSSODescriptor[] $roleDescriptors
130
     * @param string $binding
131
     * @return EndpointType|null
132
     */
133
    public static function getFirstSpAttributeConsumingService(array $roleDescriptors, string $binding = null)
134
    {
135
        return static::getFirstService(self::ENDPOINT_SERVICE_ATTRIBUTE_CONSUMING, $roleDescriptors, $binding);
136
    }
137
138
    /**
139
     * Get First IDP Services
140
     */
141
142
    /**
143
     * @param IDPSSODescriptor[] $roleDescriptors
144
     * @param string $binding
145
     * @return EndpointType|null
146
     */
147
    public static function getFirstIdpSSOService(array $roleDescriptors, string $binding = null)
148
    {
149
        return static::getFirstService(self::ENDPOINT_SERVICE_SINGLE_SIGN_ON, $roleDescriptors, $binding);
150
    }
151
152
    /**
153
     * @param IDPSSODescriptor[] $roleDescriptors
154
     * @param string $binding
155
     * @return EndpointType|null
156
     */
157
    public static function getFirstIdpAssertionIdRequestService(array $roleDescriptors, string $binding = null)
158
    {
159
        return static::getFirstService(self::ENDPOINT_SERVICE_ASSERTION_ID_REQUEST, $roleDescriptors, $binding);
160
    }
161
162
    /**
163
     * @param IDPSSODescriptor[] $roleDescriptors
164
     * @param string $binding
165
     * @return EndpointType|null
166
     */
167
    public static function getFirstIdpNameIDMappingService(array $roleDescriptors, string $binding = null)
168
    {
169
        return static::getFirstService(self::ENDPOINT_SERVICE_NAME_ID_MAPPING, $roleDescriptors, $binding);
170
    }
171
172
    /**
173
     * @param SSODescriptorType[] $roleDescriptors
174
     * @param string $binding
175
     * @param string $service
176
     * @return EndpointType|null
177
     */
178
    protected static function getFirstService(string $service, array $roleDescriptors, string $binding = null)
179
    {
180
181
        if (! in_array($service, static::ENDPOINT_SERVICE_OPTIONS)) {
182
            throw new \InvalidArgumentException('Unknown service passed: ' . $service);
183
        }
184
185
        \Craft::info(
186
            sprintf(
187
                'Looping thru %s role descriptors', count($roleDescriptors)
188
            ),
189
            'saml-core'
190
        );
191
192
        $serviceMethod = 'get' . $service . 'Service';
193
194
        \Craft::info(
195
            'Using Service method: ' . $serviceMethod,
196
            'saml-core'
197
        );
198
199
        $return = null;
200
        foreach ($roleDescriptors as $descriptor) {
201
202
            if (
203
            $return = static::getFirstIndexedEndpointType(
204
                call_user_func([$descriptor, $serviceMethod]),
205
                $binding
206
            )
207
            ) {
208
                break;
209
            }
210
211
        }
212
213
        return $return;
214
    }
215
216
    /**
217
     * @param IndexedEndpointType[] $endpointTypes
218
     * @param $binding
219
     * @return EndpointType|null
220
     */
221
    protected static function getFirstIndexedEndpointType(array $endpointTypes, string $binding = null)
222
    {
223
224
        // Is there one?
225
        if (!isset($endpointTypes[0])) {
226
            return null;
227
        }
228
229
        // Default to the first one
230
        $return = $endpointTypes[0];
231
        if (is_null($binding)) {
232
            return $return;
233
        }
234
235
        // Reset the return
236
        $return = null;
237
238
        /** @var EndpointType $endpointType */
239
        foreach ($endpointTypes as $endpointType) {
240
            if (! $endpointType instanceof EndpointType) {
241
                throw new \InvalidArgumentException();
242
            }
243
244
            if ($endpointType->getBinding() === $binding) {
245
                $return = $endpointType;
246
                break;
247
            }
248
        }
249
250
        return $return;
251
    }
252
}