Failed Conditions
Push — issue#763 ( 4f5bf6 )
by Guilherme
11:38
created

ClientMetadata::canonicalizeUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\Entity;
12
13
use Doctrine\Common\Collections\ArrayCollection;
14
use LoginCidadao\CoreBundle\Model\PersonInterface;
15
use LoginCidadao\OAuthBundle\Model\OrganizationInterface;
16
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
17
use LoginCidadao\OpenIDBundle\Validator\Constraints\SectorIdentifierUri;
18
use Symfony\Component\Validator\Constraints as Assert;
19
use LoginCidadao\OAuthBundle\Entity\Client;
20
use JMS\Serializer\Annotation as JMS;
21
use Doctrine\ORM\Mapping as ORM;
22
23
/**
24
 * @ORM\Entity(repositoryClass="LoginCidadao\OpenIDBundle\Entity\ClientMetadataRepository")
25
 * @UniqueEntity("client")
26
 * @UniqueEntity("client_name")
27
 * @ORM\HasLifecycleCallbacks
28
 * @ORM\Table(name="client_metadata")
29
 * @JMS\ExclusionPolicy("all")
30
 * @SectorIdentifierUri
31
 */
32
class ClientMetadata
33
{
34
    /**
35
     * @ORM\Id
36
     * @ORM\Column(type="integer")
37
     * @ORM\GeneratedValue(strategy="AUTO")
38
     */
39
    protected $id;
40
    protected $client_id;
41
    protected $client_secret;
42
43
    /**
44
     * @var Client
45
     * @ORM\OneToOne(targetEntity="LoginCidadao\OAuthBundle\Entity\Client", inversedBy="metadata", cascade={"persist"})
46
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
47
     */
48
    protected $client;
49
50
    /**
51
     * @JMS\Expose
52
     * @JMS\Groups({"client_metadata"})
53
     * @Assert\All({
54
     *      @Assert\Type(type="string"),
55
     *      @Assert\NotBlank,
56
     *      @Assert\Url(checkDNS = false)
57
     * })
58
     * @ORM\Column(name="redirect_uris", type="json_array", nullable=false)
59
     */
60
    protected $redirect_uris;
61
62
    /**
63
     * @JMS\Expose
64
     * @JMS\Groups({"client_metadata"})
65
     * @Assert\All({
66
     *      @Assert\Type("string")
67
     * })
68
     * @ORM\Column(name="response_types", type="simple_array", nullable=false)
69
     */
70
    protected $response_types = array('code');
71
72
    /**
73
     * @JMS\Expose
74
     * @JMS\Groups({"client_metadata"})
75
     * @Assert\All({
76
     *      @Assert\Type("string")
77
     * })
78
     * @ORM\Column(type="simple_array", nullable=false)
79
     */
80
    protected $grant_types = array('authorization_code');
81
82
    /**
83
     * @JMS\Expose
84
     * @JMS\Groups({"client_metadata"})
85
     * @Assert\Type(type="string")
86
     * @ORM\Column(name="application_type", type="string", length=100, nullable=false)
87
     */
88
    protected $application_type = 'web';
89
90
    /**
91
     * @JMS\Expose
92
     * @JMS\Groups({"client_metadata"})
93
     * @Assert\All({
94
     *      @Assert\Type("string")
95
     * })
96
     * @ORM\Column(type="simple_array", nullable=true)
97
     */
98
    protected $contacts;
99
100
    /**
101
     * @JMS\Expose
102
     * @JMS\Groups({"client_metadata"})
103
     * @Assert\Type(type="string")
104
     * @ORM\Column(type="string", nullable=true)
105
     */
106
    protected $client_name;
107
108
    /**
109
     * @JMS\Expose
110
     * @JMS\Groups({"client_metadata"})
111
     * @Assert\Type(type="string")
112
     * @Assert\Url(checkDNS = false)
113
     * @ORM\Column(type="string", length=2000, nullable=true)
114
     */
115
    protected $logo_uri;
116
117
    /**
118
     * @JMS\Expose
119
     * @JMS\Groups({"client_metadata"})
120
     * @Assert\Type(type="string")
121
     * @Assert\Url(checkDNS = false)
122
     * @ORM\Column(type="string", length=2000, nullable=true)
123
     */
124
    protected $client_uri;
125
126
    /**
127
     * @JMS\Expose
128
     * @JMS\Groups({"client_metadata"})
129
     * @Assert\Type(type="string")
130
     * @Assert\Url(checkDNS = false)
131
     * @ORM\Column(type="string", length=2000, nullable=true)
132
     */
133
    protected $policy_uri;
134
135
    /**
136
     * @JMS\Expose
137
     * @JMS\Groups({"client_metadata"})
138
     * @Assert\Url(checkDNS = false)
139
     * @Assert\Type(type="string")
140
     * @ORM\Column(type="string", length=2000, nullable=true)
141
     */
142
    protected $tos_uri;
143
144
    /**
145
     * @JMS\Expose
146
     * @JMS\Groups({"client_metadata"})
147
     * @Assert\Url(checkDNS = false)
148
     * @Assert\Type(type="string")
149
     * @ORM\Column(type="string", length=2000, nullable=true)
150
     */
151
    protected $jwks_uri;
152
153
    /**
154
     * @JMS\Expose
155
     * @JMS\Groups({"client_metadata"})
156
     * @Assert\Type(type="string")
157
     * @ORM\Column(type="text", nullable=true)
158
     */
159
    protected $jwks;
160
161
    /**
162
     * @JMS\Expose
163
     * @JMS\Groups({"client_metadata"})
164
     * @Assert\Url(checkDNS = false, protocols = {"http", "https"})
165
     * @Assert\Type(type="string")
166
     * @ORM\Column(type="string", length=2000, nullable=true)
167
     */
168
    protected $sector_identifier_uri;
169
170
    /**
171
     * @JMS\Expose
172
     * @JMS\Groups({"client_metadata"})
173
     * @Assert\Type(type="string")
174
     * @ORM\Column(type="string", length=20, nullable=true)
175
     */
176
    protected $subject_type;
177
178
    /**
179
     * @JMS\Expose
180
     * @JMS\Groups({"client_metadata"})
181
     * @Assert\Type(type="string")
182
     * @ORM\Column(type="string", length=50, nullable=true)
183
     */
184
    protected $id_token_signed_response_alg;
185
186
    /**
187
     * @JMS\Expose
188
     * @JMS\Groups({"client_metadata"})
189
     * @Assert\Type(type="string")
190
     * @ORM\Column(type="string", length=50, nullable=true)
191
     */
192
    protected $id_token_encrypted_response_alg;
193
194
    /**
195
     * @JMS\Expose
196
     * @JMS\Groups({"client_metadata"})
197
     * @Assert\Type(type="string")
198
     * @ORM\Column(type="string", length=50, nullable=true)
199
     */
200
    protected $id_token_encrypted_response_enc;
201
202
    /**
203
     * @JMS\Expose
204
     * @JMS\Groups({"client_metadata"})
205
     * @Assert\Type(type="string")
206
     * @ORM\Column(type="string", length=50, nullable=true)
207
     */
208
    protected $userinfo_signed_response_alg;
209
210
    /**
211
     * @JMS\Expose
212
     * @JMS\Groups({"client_metadata"})
213
     * @Assert\Type(type="string")
214
     * @ORM\Column(type="string", length=50, nullable=true)
215
     */
216
    protected $userinfo_encrypted_response_alg;
217
218
    /**
219
     * @JMS\Expose
220
     * @JMS\Groups({"client_metadata"})
221
     * @Assert\Type(type="string")
222
     * @ORM\Column(type="string", length=50, nullable=true)
223
     */
224
    protected $userinfo_encrypted_response_enc;
225
226
    /**
227
     * @JMS\Expose
228
     * @JMS\Groups({"client_metadata"})
229
     * @Assert\Type(type="string")
230
     * @ORM\Column(type="string", length=50, nullable=true)
231
     */
232
    protected $request_object_signing_alg;
233
234
    /**
235
     * @JMS\Expose
236
     * @JMS\Groups({"client_metadata"})
237
     * @Assert\Type(type="string")
238
     * @ORM\Column(type="string", length=50, nullable=true)
239
     */
240
    protected $request_object_encryption_alg;
241
242
    /**
243
     * @JMS\Expose
244
     * @JMS\Groups({"client_metadata"})
245
     * @Assert\Type(type="string")
246
     * @ORM\Column(type="string", length=50, nullable=true)
247
     */
248
    protected $request_object_encryption_enc;
249
250
    /**
251
     * @JMS\Expose
252
     * @JMS\Groups({"client_metadata"})
253
     * @Assert\Type(type="string")
254
     * @ORM\Column(type="string", length=50, nullable=true)
255
     */
256
    protected $token_endpoint_auth_method;
257
258
    /**
259
     * @JMS\Expose
260
     * @JMS\Groups({"client_metadata"})
261
     * @Assert\Type(type="string")
262
     * @ORM\Column(type="string", length=50, nullable=true)
263
     */
264
    protected $token_endpoint_auth_signing_alg;
265
266
    /**
267
     * @JMS\Expose
268
     * @JMS\Groups({"client_metadata"})
269
     * @Assert\Type(type="integer")
270
     * @ORM\Column(type="integer", nullable=true)
271
     */
272
    protected $default_max_age;
273
274
    /**
275
     * @JMS\Expose
276
     * @JMS\Groups({"client_metadata"})
277
     * @Assert\Type(type="boolean")
278
     */
279
    protected $require_auth_time = false;
280
281
    /**
282
     * @JMS\Expose
283
     * @JMS\Groups({"client_metadata"})
284
     * @Assert\Type(type="array")
285
     * @ORM\Column(type="simple_array", nullable=true)
286
     */
287
    protected $default_acr_values;
288
289
    /**
290
     * @JMS\Expose
291
     * @JMS\Groups({"client_metadata"})
292
     * @Assert\Url(checkDNS = false)
293
     * @Assert\Type(type="string")
294
     * @ORM\Column(type="string", length=2000, nullable=true)
295
     */
296
    protected $initiate_login_uri;
297
298
    /**
299
     * @JMS\Expose
300
     * @JMS\Groups({"client_metadata"})
301
     * @Assert\All({
302
     *      @Assert\Type("string"),
303
     *      @Assert\Url(checkDNS = false)
304
     * })
305
     * @ORM\Column(type="simple_array", nullable=true)
306
     */
307
    protected $request_uris;
308
309
    /**
310
     * @JMS\Expose
311
     * @JMS\Groups({"client_metadata"})
312
     * @Assert\Type(type="string")
313
     * @ORM\Column(type="string", nullable=true)
314
     */
315
    protected $registration_access_token;
316
317
    /**
318
     * @var OrganizationInterface
319
     * @ORM\ManyToOne(targetEntity="LoginCidadao\OAuthBundle\Model\OrganizationInterface", inversedBy="clients")
320
     * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL")
321
     */
322
    protected $organization;
323
324
    /**
325
     * @JMS\Expose
326
     * @JMS\Groups({"client_metadata"})
327
     * @Assert\All({
328
     *      @Assert\Type("string"),
329
     *      @Assert\Url(checkDNS = false)
330
     * })
331
     * @ORM\Column(type="simple_array", nullable=true)
332
     */
333
    protected $post_logout_redirect_uris;
334
335 8
    public function __construct()
336
    {
337 8
        $this->response_types = array('code');
0 ignored issues
show
Documentation Bug introduced by
It seems like array('code') of type array<integer,string> is incompatible with the declared type string of property $response_types.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
338 8
        $this->grant_types = array('authorization_code');
0 ignored issues
show
Documentation Bug introduced by
It seems like array('authorization_code') of type array<integer,string> is incompatible with the declared type string of property $grant_types.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
339 8
        $this->application_type = 'web';
340 8
        $this->require_auth_time = false;
341 8
        $this->subject_type = 'pairwise';
342 8
    }
343
344 3
    public function getRedirectUris()
345
    {
346 3
        return $this->redirect_uris;
347
    }
348
349 3
    public function setRedirectUris($redirect_uris)
350
    {
351 3
        $this->redirect_uris = $redirect_uris;
352
353 3
        return $this;
354
    }
355
356 3
    public function getResponseTypes()
357
    {
358 3
        return $this->response_types;
359
    }
360
361 3
    public function setResponseTypes($response_types)
362
    {
363 3
        $this->response_types = $response_types;
364
365 3
        return $this;
366
    }
367
368 3
    public function getGrantTypes()
369
    {
370 3
        return $this->grant_types;
371
    }
372
373 3
    public function setGrantTypes($grant_types)
374
    {
375 3
        $this->grant_types = $grant_types;
376
377 3
        return $this;
378
    }
379
380 3
    public function getApplicationType()
381
    {
382 3
        return $this->application_type;
383
    }
384
385 3
    public function setApplicationType($application_type)
386
    {
387 3
        $this->application_type = $application_type;
388
389 3
        return $this;
390
    }
391
392 3
    public function getContacts()
393
    {
394 3
        $owners = [];
395 3
        if ($this->getClient()) {
396
            $owners = array_map(
397
                function (PersonInterface $owner) {
398
                    return $owner->getEmail();
399
                },
400
                $this->getClient()->getOwners()->toArray()
401
            );
402
        }
403 3
        $contacts = is_array($this->contacts) ? $this->contacts : [];
0 ignored issues
show
introduced by
The condition is_array($this->contacts) is always false.
Loading history...
404
405 3
        return array_unique(array_merge($contacts, $owners));
406
    }
407
408 3
    public function setContacts($contacts)
409
    {
410 3
        $this->contacts = $contacts;
411
412 3
        return $this;
413
    }
414
415 3
    public function getClientName()
416
    {
417 3
        return $this->client_name;
418
    }
419
420 1
    public function setClientName($client_name)
421
    {
422 1
        $this->client_name = $client_name;
423
424 1
        return $this;
425
    }
426
427 3
    public function getLogoUri()
428
    {
429 3
        return $this->logo_uri;
430
    }
431
432 1
    public function setLogoUri($logo_uri)
433
    {
434 1
        $this->logo_uri = $logo_uri;
435
436 1
        return $this;
437
    }
438
439 3
    public function getClientUri()
440
    {
441 3
        return $this->client_uri;
442
    }
443
444 1
    public function setClientUri($client_uri)
445
    {
446 1
        $this->client_uri = $client_uri;
447
448 1
        return $this;
449
    }
450
451 3
    public function getPolicyUri()
452
    {
453 3
        return $this->policy_uri;
454
    }
455
456
    public function setPolicyUri($policy_uri)
457
    {
458
        $this->policy_uri = $policy_uri;
459
460
        return $this;
461
    }
462
463 3
    public function getTosUri()
464
    {
465 3
        return $this->tos_uri;
466
    }
467
468 1
    public function setTosUri($tos_uri)
469
    {
470 1
        $this->tos_uri = $tos_uri;
471
472 1
        return $this;
473
    }
474
475 3
    public function getJwksUri()
476
    {
477 3
        return $this->jwks_uri;
478
    }
479
480
    public function setJwksUri($jwks_uri)
481
    {
482
        $this->jwks_uri = $jwks_uri;
483
484
        return $this;
485
    }
486
487 3
    public function getJwks()
488
    {
489 3
        return $this->jwks;
490
    }
491
492
    public function setJwks($jwks)
493
    {
494
        $this->jwks = $jwks;
495
496
        return $this;
497
    }
498
499 3
    public function getSectorIdentifierUri()
500
    {
501 3
        return $this->sector_identifier_uri;
502
    }
503
504
    public function setSectorIdentifierUri($sector_identifier_uri)
505
    {
506
        $this->sector_identifier_uri = $sector_identifier_uri;
507
508
        return $this;
509
    }
510
511 3
    public function getSubjectType()
512
    {
513 3
        return $this->subject_type;
514
    }
515
516 3
    public function setSubjectType($subject_type)
517
    {
518 3
        $this->subject_type = $subject_type;
519
520 3
        return $this;
521
    }
522
523 3
    public function getIdTokenSignedResponseAlg()
524
    {
525 3
        return $this->id_token_signed_response_alg;
526
    }
527
528 1
    public function setIdTokenSignedResponseAlg($id_token_signed_response_alg)
529
    {
530 1
        $this->id_token_signed_response_alg = $id_token_signed_response_alg;
531
532 1
        return $this;
533
    }
534
535 3
    public function getIdTokenEncryptedResponseAlg()
536
    {
537 3
        return $this->id_token_encrypted_response_alg;
538
    }
539
540
    public function setIdTokenEncryptedResponseAlg($id_token_encrypted_response_alg)
541
    {
542
        $this->id_token_encrypted_response_alg = $id_token_encrypted_response_alg;
543
544
        return $this;
545
    }
546
547 3
    public function getIdTokenEncryptedResponseEnc()
548
    {
549 3
        return $this->id_token_encrypted_response_enc;
550
    }
551
552
    public function setIdTokenEncryptedResponseEnc($id_token_encrypted_response_enc)
553
    {
554
        $this->id_token_encrypted_response_enc = $id_token_encrypted_response_enc;
555
556
        return $this;
557
    }
558
559 3
    public function getUserinfoSignedResponseAlg()
560
    {
561 3
        return $this->userinfo_signed_response_alg;
562
    }
563
564
    public function setUserinfoSignedResponseAlg($userinfo_signed_response_alg)
565
    {
566
        $this->userinfo_signed_response_alg = $userinfo_signed_response_alg;
567
568
        return $this;
569
    }
570
571 3
    public function getUserinfoEncryptedResponseAlg()
572
    {
573 3
        return $this->userinfo_encrypted_response_alg;
574
    }
575
576
    public function setUserinfoEncryptedResponseAlg($userinfo_encrypted_response_alg)
577
    {
578
        $this->userinfo_encrypted_response_alg = $userinfo_encrypted_response_alg;
579
580
        return $this;
581
    }
582
583 3
    public function getUserinfoEncryptedResponseEnc()
584
    {
585 3
        return $this->userinfo_encrypted_response_enc;
586
    }
587
588
    public function setUserinfoEncryptedResponseEnc($userinfo_encrypted_response_enc)
589
    {
590
        $this->userinfo_encrypted_response_enc = $userinfo_encrypted_response_enc;
591
592
        return $this;
593
    }
594
595 3
    public function getRequestObjectSigningAlg()
596
    {
597 3
        return $this->request_object_signing_alg;
598
    }
599
600
    public function setRequestObjectSigningAlg($request_object_signing_alg)
601
    {
602
        $this->request_object_signing_alg = $request_object_signing_alg;
603
604
        return $this;
605
    }
606
607 3
    public function getRequestObjectEncryptionAlg()
608
    {
609 3
        return $this->request_object_encryption_alg;
610
    }
611
612
    public function setRequestObjectEncryptionAlg($request_object_encryption_alg)
613
    {
614
        $this->request_object_encryption_alg = $request_object_encryption_alg;
615
616
        return $this;
617
    }
618
619 3
    public function getRequestObjectEncryptionEnc()
620
    {
621 3
        return $this->request_object_encryption_enc;
622
    }
623
624
    public function setRequestObjectEncryptionEnc($request_object_encryption_enc)
625
    {
626
        $this->request_object_encryption_enc = $request_object_encryption_enc;
627
628
        return $this;
629
    }
630
631 3
    public function getTokenEndpointAuthMethod()
632
    {
633 3
        return $this->token_endpoint_auth_method;
634
    }
635
636 1
    public function setTokenEndpointAuthMethod($token_endpoint_auth_method)
637
    {
638 1
        $this->token_endpoint_auth_method = $token_endpoint_auth_method;
639
640 1
        return $this;
641
    }
642
643 3
    public function getTokenEndpointAuthSigningAlg()
644
    {
645 3
        return $this->token_endpoint_auth_signing_alg;
646
    }
647
648
    public function setTokenEndpointAuthSigningAlg($token_endpoint_auth_signing_alg)
649
    {
650
        $this->token_endpoint_auth_signing_alg = $token_endpoint_auth_signing_alg;
651
652
        return $this;
653
    }
654
655 3
    public function getDefaultMaxAge()
656
    {
657 3
        return $this->default_max_age;
658
    }
659
660
    public function setDefaultMaxAge($default_max_age)
661
    {
662
        $this->default_max_age = $default_max_age;
663
664
        return $this;
665
    }
666
667 3
    public function getRequireAuthTime()
668
    {
669 3
        return $this->require_auth_time;
670
    }
671
672 1
    public function setRequireAuthTime($require_auth_time)
673
    {
674 1
        $this->require_auth_time = $require_auth_time;
675
676 1
        return $this;
677
    }
678
679 3
    public function getDefaultAcrValues()
680
    {
681 3
        return $this->default_acr_values;
682
    }
683
684 3
    public function setDefaultAcrValues($default_acr_values)
685
    {
686 3
        $this->default_acr_values = $default_acr_values;
687
688 3
        return $this;
689
    }
690
691 3
    public function getInitiateLoginUri()
692
    {
693 3
        return $this->initiate_login_uri;
694
    }
695
696
    public function setInitiateLoginUri($initiate_login_uri)
697
    {
698
        $this->initiate_login_uri = $initiate_login_uri;
699
700
        return $this;
701
    }
702
703 3
    public function getRequestUris()
704
    {
705 3
        return $this->request_uris;
706
    }
707
708
    public function setRequestUris($request_uris)
709
    {
710
        $this->request_uris = $request_uris;
711
712
        return $this;
713
    }
714
715
    /**
716
     * @JMS\Groups({"client_metadata"})
717
     * @JMS\VirtualProperty
718
     * @JMS\SerializedName("client_id")
719
     */
720 3
    public function getClientId()
721
    {
722 3
        if ($this->client_id === null && $this->client) {
723
            return $this->client->getClientId();
724
        }
725
726 3
        return $this->client_id;
727
    }
728
729 1
    public function setClientId($client_id)
730
    {
731 1
        $this->client_id = $client_id;
732
733 1
        return $this;
734
    }
735
736
    /**
737
     * @JMS\Groups({"client_metadata"})
738
     * @JMS\VirtualProperty
739
     * @JMS\SerializedName("client_secret")
740
     */
741 3
    public function getClientSecret()
742
    {
743 3
        if ($this->client_id === null && $this->client) {
744
            return $this->client->getClientSecret();
745
        }
746
747 3
        return $this->client_secret;
748
    }
749
750 1
    public function setClientSecret($client_secret)
751
    {
752 1
        $this->client_secret = $client_secret;
753
754 1
        return $this;
755
    }
756
757
    /**
758
     * @param Client $client
759
     * @return ClientMetadata
760
     */
761 1
    public function fromClient(Client $client)
762
    {
763 1
        $this->setGrantTypes($client->getAllowedGrantTypes())
764 1
            ->setClientUri($client->getSiteUrl())
765 1
            ->setTosUri($client->getTermsOfUseUrl())
766 1
            ->setClientName($client->getName())
767 1
            ->setRedirectUris($client->getRedirectUris());
768
769 1
        $this->setClientId($client->getPublicId())
770 1
            ->setClientSecret($client->getSecret());
771
772 1
        return $this;
773
    }
774
775
    /**
776
     * @return Client
777
     */
778 1
    public function toClient()
779
    {
780 1
        $name = $this->getClientName();
781 1
        $hasName = $name !== null && strlen($name) > 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $hasName is dead and can be removed.
Loading history...
782
783 1
        $grantTypes = $this->getGrantTypes();
784 1
        $clientUri = $this->getClientUri();
785 1
        $tosUri = $this->getTosUri();
786 1
        $clientName = $this->getClientName();
787 1
        $redirectUris = $this->getRedirectUris();
788
789 1
        $client = new Client();
790
791 1
        if ($grantTypes) {
792
            $client->setAllowedGrantTypes($grantTypes);
0 ignored issues
show
Bug introduced by
$grantTypes of type string is incompatible with the type array expected by parameter $grantTypes of FOS\OAuthServerBundle\Mo...:setAllowedGrantTypes(). ( Ignorable by Annotation )

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

792
            $client->setAllowedGrantTypes(/** @scrutinizer ignore-type */ $grantTypes);
Loading history...
793
        }
794
795 1
        if ($clientUri) {
796
            $client->setLandingPageUrl($clientUri)
797
                ->setSiteUrl($clientUri);
798
        }
799
800 1
        if ($tosUri) {
801
            $client->setTermsOfUseUrl($tosUri);
802
        }
803
804 1
        if ($clientName) {
805
            $client->setName($clientName);
806
        }
807
808 1
        if ($redirectUris) {
809 1
            $client->setRedirectUris($redirectUris);
0 ignored issues
show
Bug introduced by
$redirectUris of type string is incompatible with the type array expected by parameter $redirectUris of LoginCidadao\OAuthBundle...ient::setRedirectUris(). ( Ignorable by Annotation )

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

809
            $client->setRedirectUris(/** @scrutinizer ignore-type */ $redirectUris);
Loading history...
810
        }
811
812 1
        $client->setVisible(false)
813 1
            ->setPublished(false);
814
815 1
        return $client;
816
    }
817
818 3
    public function getClient()
819
    {
820 3
        return $this->client;
821
    }
822
823 3
    public function setClient(Client $client)
824
    {
825 3
        $this->client = $client;
826
827 3
        return $this;
828
    }
829
830
    /**
831
     * @ORM\PrePersist()
832
     */
833 1
    public function checkDefaults()
834
    {
835 1
        if (!$this->getGrantTypes()) {
836 1
            $this->setGrantTypes(array('authorization_code'));
837
        }
838
839 1
        if (!$this->getResponseTypes()) {
840 1
            $this->setResponseTypes(array('code'));
841
        }
842
843 1
        if (!$this->getApplicationType()) {
844 1
            $this->setApplicationType('web');
845
        }
846
847 1
        if (!$this->getRequireAuthTime()) {
848 1
            $this->setRequireAuthTime(false);
849
        }
850
851 1
        if (!$this->getIdTokenSignedResponseAlg()) {
852 1
            $this->setIdTokenSignedResponseAlg('RS256');
853
        }
854
855 1
        if (!$this->getTokenEndpointAuthMethod()) {
856 1
            $this->setTokenEndpointAuthMethod('client_secret_basic');
857
        }
858 1
    }
859
860
    public function getSectorIdentifier()
861
    {
862
        $siUri = $this->getSectorIdentifierUri();
863
        if ($siUri) {
864
            $uri = $siUri;
865
        } else {
866
            $uris = $this->getRedirectUris();
867
            $uri = reset($uris);
0 ignored issues
show
Bug introduced by
$uris of type string is incompatible with the type array expected by parameter $array of reset(). ( Ignorable by Annotation )

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

867
            $uri = reset(/** @scrutinizer ignore-type */ $uris);
Loading history...
868
        }
869
870
        return parse_url($uri, PHP_URL_HOST);
871
    }
872
873
    public function getRegistrationAccessToken()
874
    {
875
        return $this->registration_access_token;
876
    }
877
878
    /**
879
     * @return OrganizationInterface
880
     */
881
    public function getOrganization()
882
    {
883
        return $this->organization;
884
    }
885
886
    /**
887
     * @param OrganizationInterface $organization
888
     */
889
    public function setOrganization($organization = null)
890
    {
891
        $this->organization = $organization;
892
    }
893
894
    /**
895
     * @return array
896
     */
897 3
    public function getPostLogoutRedirectUris()
898
    {
899 3
        return array_map(
900 3
            function ($value) {
901
                return self::canonicalizeUri($value);
902 3
            },
903 3
            is_array($this->post_logout_redirect_uris) ? $this->post_logout_redirect_uris : []
0 ignored issues
show
introduced by
The condition is_array($this->post_logout_redirect_uris) is always false.
Loading history...
904
        );
905
    }
906
907
    /**
908
     * @param array
909
     * @return ClientMetadata
910
     */
911 3
    public function setPostLogoutRedirectUris($post_logout_redirect_uris)
912
    {
913 3
        $this->post_logout_redirect_uris = $post_logout_redirect_uris;
914
915 3
        return $this;
916
    }
917
918
    /**
919
     * Add trailing slashes
920
     */
921
    public static function canonicalizeUri($uri)
922
    {
923
        $parsed = parse_url($uri);
924
        if (array_key_exists('path', $parsed) === false) {
925
            $parsed['path'] = '/';
926
        }
927
        $unparsed = self::unparseUrl($parsed);
928
929
        return $unparsed;
930
    }
931
932
    private static function unparseUrl($parsed_url)
933
    {
934
        $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'].'://' : '';
935
        $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
936
        $port = isset($parsed_url['port']) ? ':'.$parsed_url['port'] : '';
937
        $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
938
        $pass = isset($parsed_url['pass']) ? ':'.$parsed_url['pass'] : '';
939
        $pass = ($user || $pass) ? "$pass@" : '';
940
        $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
941
        $query = isset($parsed_url['query']) ? '?'.$parsed_url['query'] : '';
942
        $fragment = isset($parsed_url['fragment']) ? '#'.$parsed_url['fragment']
943
            : '';
944
945
        return "$scheme$user$pass$host$port$path$query$fragment";
946
    }
947
}
948