1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Zed\Oauth\Business\Mapper; |
||
9 | |||
10 | use Generated\Shared\Transfer\OauthRefreshTokenTransfer; |
||
11 | use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; |
||
12 | use Spryker\Zed\Oauth\Dependency\Service\OauthToUtilEncodingServiceInterface; |
||
13 | |||
14 | class OauthRefreshTokenMapper implements OauthRefreshTokenMapperInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected const CUSTOMER_REFERENCE = 'customer_reference'; |
||
20 | |||
21 | /** |
||
22 | * @var \Spryker\Zed\Oauth\Dependency\Service\OauthToUtilEncodingServiceInterface |
||
23 | */ |
||
24 | protected $utilEncodingService; |
||
25 | |||
26 | /** |
||
27 | * @var array<\Spryker\Zed\OauthExtension\Dependency\Plugin\OauthUserIdentifierFilterPluginInterface> |
||
28 | */ |
||
29 | protected $oauthUserIdentifierFilterPlugins; |
||
30 | |||
31 | /** |
||
32 | * @param \Spryker\Zed\Oauth\Dependency\Service\OauthToUtilEncodingServiceInterface $utilEncodingService |
||
33 | * @param array<\Spryker\Zed\OauthExtension\Dependency\Plugin\OauthUserIdentifierFilterPluginInterface> $oauthUserIdentifierFilterPlugins |
||
34 | */ |
||
35 | public function __construct( |
||
36 | OauthToUtilEncodingServiceInterface $utilEncodingService, |
||
37 | array $oauthUserIdentifierFilterPlugins = [] |
||
38 | ) { |
||
39 | $this->utilEncodingService = $utilEncodingService; |
||
40 | $this->oauthUserIdentifierFilterPlugins = $oauthUserIdentifierFilterPlugins; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param \League\OAuth2\Server\Entities\RefreshTokenEntityInterface $refreshTokenEntity |
||
45 | * @param \Generated\Shared\Transfer\OauthRefreshTokenTransfer $oauthRefreshTokenTransfer |
||
46 | * |
||
47 | * @return \Generated\Shared\Transfer\OauthRefreshTokenTransfer |
||
48 | */ |
||
49 | public function mapRefreshTokenEntityToOauthRefreshTokenTransfer( |
||
50 | RefreshTokenEntityInterface $refreshTokenEntity, |
||
51 | OauthRefreshTokenTransfer $oauthRefreshTokenTransfer |
||
52 | ): OauthRefreshTokenTransfer { |
||
53 | /** @var string $encodedUserIdentifier */ |
||
54 | $encodedUserIdentifier = $refreshTokenEntity->getAccessToken()->getUserIdentifier(); |
||
55 | $userIdentifier = $this->utilEncodingService->decodeJson($encodedUserIdentifier, true); |
||
56 | $filteredUserIdentifier = $this->filterUserIdentifier($userIdentifier); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
57 | $encodedUserIdentifier = (string)$this->utilEncodingService->encodeJson($filteredUserIdentifier); |
||
58 | |||
59 | $oauthRefreshTokenTransfer->setIdentifier($refreshTokenEntity->getIdentifier()) |
||
60 | ->setCustomerReference($userIdentifier[static::CUSTOMER_REFERENCE] ?? null) |
||
61 | ->setUserIdentifier($encodedUserIdentifier) |
||
62 | ->setExpiresAt($refreshTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s')) |
||
63 | ->setIdOauthClient($refreshTokenEntity->getAccessToken()->getClient()->getIdentifier()) |
||
64 | ->setScopes($this->utilEncodingService->encodeJson($refreshTokenEntity->getAccessToken()->getScopes())); |
||
65 | |||
66 | return $oauthRefreshTokenTransfer; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param array $userIdentifier |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | protected function filterUserIdentifier(array $userIdentifier): array |
||
75 | { |
||
76 | foreach ($this->oauthUserIdentifierFilterPlugins as $oauthUserIdentifierFilterPlugin) { |
||
77 | $userIdentifier = $oauthUserIdentifierFilterPlugin->filter($userIdentifier); |
||
78 | } |
||
79 | |||
80 | return $userIdentifier; |
||
81 | } |
||
82 | } |
||
83 |