Repository navigation
fix(integration-discord): preserva metadata entre OAuth e ETL #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1f6921d
fix(integration-discord): preserva dados da identidade
henrique-leme 3922c46
fix(integration-discord): preserva avatar ausente
henrique-leme 290eb85
fix(integration-discord): mantém construtor compatível
henrique-leme 9cece5e
fix(identity): conclui conexão após unificação
henrique-leme fc6329d
refactor(identity): simplifica merge OAuth pendente
henrique-leme 31e2771
Merge branch '4.x' into story/479-normaliza-identidade-discord
danielhe4rt d324544
Merge branch '4.x' into story/479-normaliza-identidade-discord
danielhe4rt af3e16a
refactor(identity): tipa conexão OAuth e merge pendente com DTOs
danielhe4rt cb6f0f3
refactor(integration-discord): tipa o usuário Discord da identidade c…
danielhe4rt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app-modules/identity/src/Auth/Actions/ConfirmOAuthMerge.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Identity\Auth\Actions; | ||
|
|
||
| use He4rt\Identity\Auth\DTOs\PendingOAuthMergeDTO; | ||
| use He4rt\Identity\ExternalIdentity\Models\ExternalIdentity; | ||
| use He4rt\Identity\User\Models\User; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| final readonly class ConfirmOAuthMerge | ||
| { | ||
| public function __construct( | ||
| private MergeAccountsAction $mergeAccounts, | ||
| private PersistOAuthConnection $persistConnection, | ||
| ) {} | ||
|
|
||
| public function execute(User $currentUser, PendingOAuthMergeDTO $pending): ?User | ||
| { | ||
| return DB::transaction(function () use ($currentUser, $pending): ?User { | ||
| $targetUser = User::query() | ||
| ->lockForUpdate() | ||
| ->find($pending->conflictingUserId); | ||
|
|
||
| if (!$targetUser instanceof User || $targetUser->is($currentUser)) { | ||
| return null; | ||
| } | ||
|
|
||
| $conflictingIdentity = ExternalIdentity::query() | ||
| ->where('model_type', (new User)->getMorphClass()) | ||
| ->where('model_id', $targetUser->id) | ||
| ->where('provider', $pending->connection->provider) | ||
| ->where('external_account_id', $pending->connection->providerId) | ||
| ->lockForUpdate() | ||
| ->first(); | ||
|
|
||
| if (!$conflictingIdentity instanceof ExternalIdentity) { | ||
| return null; | ||
| } | ||
|
|
||
| $this->mergeAccounts->execute($currentUser, $targetUser); | ||
|
|
||
| $this->persistConnection->execute( | ||
| owner: $targetUser, | ||
| connection: $pending->connection, | ||
| connectedBy: $targetUser->id, | ||
| ); | ||
|
|
||
| return $targetUser->refresh(); | ||
| }); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
app-modules/identity/src/Auth/Actions/PersistOAuthConnection.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Identity\Auth\Actions; | ||
|
|
||
| use He4rt\Identity\Auth\DTOs\OAuthConnectionDTO; | ||
| use He4rt\Identity\ExternalIdentity\Enums\CredentialsType; | ||
| use He4rt\Identity\ExternalIdentity\Events\ExternalIdentityConnected; | ||
| use He4rt\Identity\ExternalIdentity\Models\ExternalIdentity; | ||
| use He4rt\Identity\User\Models\User; | ||
|
|
||
| final class PersistOAuthConnection | ||
| { | ||
| public function execute(User $owner, OAuthConnectionDTO $connection, ?string $connectedBy): ExternalIdentity | ||
| { | ||
| /** @var ExternalIdentity $identity */ | ||
| $identity = $owner->providers()->firstOrNew([ | ||
| 'provider' => $connection->provider, | ||
| 'external_account_id' => $connection->providerId, | ||
| ]); | ||
|
|
||
| $identity->forceFill([ | ||
| 'type' => $connection->provider->getType(), | ||
| 'credentials_type' => CredentialsType::OAuth2, | ||
| 'credentials' => $connection->credentials, | ||
| 'metadata' => array_replace($identity->metadata ?? [], $connection->metadata), | ||
| 'connected_at' => now(), | ||
| 'disconnected_at' => null, | ||
| 'connected_by' => $connectedBy, | ||
| ])->save(); | ||
|
|
||
| event(new ExternalIdentityConnected($identity)); | ||
|
|
||
| return $identity; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Identity\Auth\DTOs; | ||
|
|
||
| use He4rt\Identity\ExternalIdentity\Data\ClientAccessManager; | ||
| use He4rt\Identity\ExternalIdentity\Enums\IdentityProvider; | ||
|
|
||
| final readonly class OAuthConnectionDTO | ||
| { | ||
| /** | ||
| * @param array<string, mixed> $metadata | ||
| */ | ||
| public function __construct( | ||
| public IdentityProvider $provider, | ||
| public string $providerId, | ||
| public ClientAccessManager $credentials, | ||
| public array $metadata, | ||
| ) {} | ||
|
|
||
| public static function fromOAuth(OAuthUserDTO $oauthUser, OAuthAccessDTO $access): self | ||
| { | ||
| return new self( | ||
| provider: $oauthUser->provider, | ||
| providerId: $oauthUser->providerId, | ||
| credentials: $access->toClientAccessManager(), | ||
| metadata: $oauthUser->toMetadata(), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app-modules/identity/src/Auth/DTOs/PendingOAuthMergeDTO.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Identity\Auth\DTOs; | ||
|
|
||
| use He4rt\Identity\ExternalIdentity\Data\ClientAccessManager; | ||
| use He4rt\Identity\ExternalIdentity\Enums\CredentialsType; | ||
| use He4rt\Identity\ExternalIdentity\Enums\IdentityProvider; | ||
|
|
||
| final readonly class PendingOAuthMergeDTO | ||
| { | ||
| public function __construct( | ||
| public string $conflictingUserId, | ||
| public OAuthConnectionDTO $connection, | ||
| ) {} | ||
|
|
||
| public static function fromSession(mixed $payload): ?self | ||
| { | ||
| if (!is_array($payload)) { | ||
| return null; | ||
| } | ||
|
|
||
| $conflictingUserId = $payload['conflicting_user_id'] ?? null; | ||
| $providerValue = $payload['provider'] ?? null; | ||
| $providerId = $payload['provider_id'] ?? null; | ||
| $credentials = $payload['credentials'] ?? null; | ||
| $metadata = $payload['metadata'] ?? null; | ||
|
|
||
| if ( | ||
| !is_string($conflictingUserId) | ||
| || !is_string($providerValue) | ||
| || !is_string($providerId) | ||
| || !is_array($credentials) | ||
| || !is_array($metadata) | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| $accessToken = $credentials['access_token'] ?? null; | ||
| $refreshToken = $credentials['refresh_token'] ?? null; | ||
| $expiresIn = $credentials['expires_in'] ?? null; | ||
| $provider = IdentityProvider::tryFrom($providerValue); | ||
|
|
||
| $hasValidTokens = is_string($accessToken) | ||
| && is_string($refreshToken) | ||
| && (is_string($expiresIn) || $expiresIn === null); | ||
| $isOAuthProvider = $provider instanceof IdentityProvider | ||
| && $provider->getCredentialsType() === CredentialsType::OAuth2; | ||
|
|
||
| if (!$hasValidTokens || !$isOAuthProvider) { | ||
| return null; | ||
| } | ||
|
|
||
| /** @var array<string, mixed> $metadata */ | ||
| return new self( | ||
| conflictingUserId: $conflictingUserId, | ||
| connection: new OAuthConnectionDTO( | ||
| provider: $provider, | ||
| providerId: $providerId, | ||
| credentials: ClientAccessManager::make( | ||
| accessToken: $accessToken, | ||
| refreshToken: $refreshToken, | ||
| expiresIn: $expiresIn, | ||
| ), | ||
| metadata: $metadata, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{ | ||
| * conflicting_user_id: string, | ||
| * provider: string, | ||
| * provider_id: string, | ||
| * credentials: array{access_token: string|null, refresh_token: string|null, expires_in: int|string|null}, | ||
| * metadata: array<string, mixed>, | ||
| * } | ||
| */ | ||
| public function toSession(): array | ||
| { | ||
| return [ | ||
| 'conflicting_user_id' => $this->conflictingUserId, | ||
| 'provider' => $this->connection->provider->value, | ||
| 'provider_id' => $this->connection->providerId, | ||
| 'credentials' => [ | ||
| 'access_token' => $this->connection->credentials->accessToken, | ||
| 'refresh_token' => $this->connection->credentials->refreshToken, | ||
| 'expires_in' => $this->connection->credentials->expiresIn, | ||
| ], | ||
| 'metadata' => $this->connection->metadata, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.