|
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\RemoteClaimsBundle\Parser; |
|
12
|
|
|
|
|
13
|
|
|
use Emarref\Jwt\Jwt; |
|
14
|
|
|
use Emarref\Jwt\Token; |
|
15
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface; |
|
16
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface; |
|
17
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimParserInterface; |
|
18
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\TagUri; |
|
19
|
|
|
|
|
20
|
|
|
class RemoteClaimParser implements RemoteClaimParserInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param array|object|string $claimMetadata |
|
24
|
|
|
* @param RemoteClaimInterface $claim |
|
25
|
|
|
* @param ClaimProviderInterface|null $provider |
|
26
|
|
|
* @return RemoteClaimInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function parseClaim( |
|
29
|
|
|
$claimMetadata, |
|
30
|
|
|
RemoteClaimInterface $claim, |
|
31
|
|
|
ClaimProviderInterface $provider = null |
|
32
|
|
|
) { |
|
33
|
|
|
$claimMetadata = self::normalizeData($claimMetadata); |
|
34
|
|
|
$claimName = TagUri::createFromString($claimMetadata->claim_name); |
|
35
|
|
|
|
|
36
|
|
|
$claim |
|
37
|
|
|
->setName($claimName) |
|
38
|
|
|
->setDisplayName($claimMetadata->claim_display_name) |
|
39
|
|
|
->setDescription($claimMetadata->claim_description) |
|
40
|
|
|
->setRecommendedScope($claimMetadata->claim_provider_recommended_scope) |
|
41
|
|
|
->setEssentialScope($claimMetadata->claim_provider_essential_scope); |
|
42
|
|
|
|
|
43
|
|
|
if ($provider) { |
|
44
|
|
|
$claim->setProvider(self::parseClaimProvider($claimMetadata->claim_provider, $provider)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $claim; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function parseClaimProvider($claimProviderMetadata, ClaimProviderInterface $provider) |
|
51
|
|
|
{ |
|
52
|
|
|
$claimProviderMetadata = self::normalizeData($claimProviderMetadata); |
|
53
|
|
|
|
|
54
|
|
|
$provider |
|
55
|
|
|
->setClientId($claimProviderMetadata->client_id) |
|
56
|
|
|
->setName($claimProviderMetadata->client_name) |
|
57
|
|
|
->setRedirectUris($claimProviderMetadata->redirect_uris); |
|
58
|
|
|
|
|
59
|
|
|
return $provider; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function parseJwt($jwt, RemoteClaimInterface $claim, ClaimProviderInterface $provider = null) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$jwt instanceof Token) { |
|
65
|
|
|
$jwt = (new Jwt())->deserialize($jwt); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return self::parseClaim($jwt->getPayload()->jsonSerialize(), $claim, $provider); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string|array|object $data |
|
73
|
|
|
* @return object |
|
74
|
|
|
*/ |
|
75
|
|
|
private static function normalizeData($data) |
|
76
|
|
|
{ |
|
77
|
|
|
if (is_string($data)) { |
|
78
|
|
|
return json_decode($data, false); |
|
79
|
|
|
} elseif (is_array($data)) { |
|
80
|
|
|
return (object)$data; |
|
81
|
|
|
} elseif (!is_object($data)) { |
|
82
|
|
|
throw new \InvalidArgumentException("The metadata should be a string, array or object"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $data; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|