TenantAtlas/apps/platform/app/Services/Providers/ProviderIdentityResolver.php
ahmido 023274c46c feat: normalize provider connection scope contracts (#339)
## Summary
- normalize provider-neutral target-scope and identity contracts across provider connection resolution, operation-start gating, verification reporting, and boundary configuration
- align provider connection resource, onboarding, tenant summaries, and operation follow-up on the same shared scope contract while keeping Microsoft-specific profile details in provider-owned metadata
- add Spec 281 artifacts and focused feature/browser coverage for the new provider-scope contract
- move the tenant dashboard context-chip rail into Filament header widgets so the metadata row renders directly under the page subtitle

## Validation
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/Providers/ProviderConnectionTargetScopeNeutralityTest.php tests/Feature/Providers/ProviderIdentityResolutionNeutralityTest.php tests/Feature/Providers/ProviderOperationStartGateTargetScopeContextTest.php tests/Feature/Filament/ProviderConnectionResourceScopeSummaryTest.php tests/Feature/Onboarding/ManagedTenantOnboardingProviderConnectionScopeTest.php tests/Feature/Guards/ProviderConnectionMicrosoftScopeLeakGuardTest.php`
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Browser/Spec281ProviderConnectionScopeSmokeTest.php`
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/Dashboard/TenantDashboardProductizationSummaryTest.php`
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Browser/Dashboard/TenantDashboardProductizationSmokeTest.php`
- `cd apps/platform && ./vendor/bin/sail bin pint --dirty --format agent`

## Notes
- Filament remains on v5 with Livewire v4-compatible surfaces only.
- Provider registration location is unchanged; Laravel 11+ providers stay in `apps/platform/bootstrap/providers.php`.
- `ProviderConnectionResource` remains non-globally-searchable and still exposes View/Edit pages.
- No new asset registration was added; deploy-time `filament:assets` expectations are unchanged.
- No new destructive action path was introduced; existing server authorization and confirmation handling remain in place where applicable.

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #339
2026-05-07 19:28:42 +00:00

157 lines
6.9 KiB
PHP

<?php
namespace App\Services\Providers;
use App\Models\ProviderConnection;
use App\Models\ProviderCredential;
use App\Support\Providers\ProviderConnectionType;
use App\Support\Providers\ProviderCredentialSource;
use App\Support\Providers\ProviderReasonCodes;
use App\Support\Providers\TargetScope\ProviderConnectionTargetScopeDescriptor;
use App\Support\Providers\TargetScope\ProviderConnectionTargetScopeNormalizer;
use InvalidArgumentException;
use RuntimeException;
final class ProviderIdentityResolver
{
public function __construct(
private readonly PlatformProviderIdentityResolver $platformResolver,
private readonly CredentialManager $credentials,
private readonly ProviderConnectionTargetScopeNormalizer $targetScopeNormalizer,
) {}
public function resolve(ProviderConnection $connection): ProviderIdentityResolution
{
$targetScopeIdentifier = trim((string) $connection->entra_tenant_id);
$connectionType = $this->resolveConnectionType($connection);
$targetScopeResult = $this->targetScopeNormalizer->normalizeConnection($connection);
$targetScope = $targetScopeResult['target_scope'] ?? null;
$providerContextDetails = $this->targetScopeNormalizer->contextualIdentityDetailsForConnection($connection);
if ($connectionType === null) {
return ProviderIdentityResolution::blocked(
connectionType: ProviderConnectionType::Platform,
credentialSource: 'unknown',
reasonCode: ProviderReasonCodes::ProviderConnectionTypeInvalid,
message: 'Provider connection type is invalid.',
targetScope: $targetScope instanceof ProviderConnectionTargetScopeDescriptor ? $targetScope : null,
providerContextDetails: $providerContextDetails,
);
}
if ($targetScopeResult['status'] !== ProviderConnectionTargetScopeNormalizer::STATUS_NORMALIZED) {
return ProviderIdentityResolution::blocked(
connectionType: $connectionType,
credentialSource: $connectionType === ProviderConnectionType::Platform ? 'platform_config' : ProviderCredentialSource::DedicatedManual->value,
reasonCode: ProviderReasonCodes::ProviderConnectionInvalid,
message: $targetScopeResult['message'] ?? 'Provider connection target scope is invalid.',
targetScope: $targetScope instanceof ProviderConnectionTargetScopeDescriptor ? $targetScope : null,
providerContextDetails: $providerContextDetails,
);
}
if ((bool) $connection->migration_review_required) {
return ProviderIdentityResolution::blocked(
connectionType: $connectionType,
credentialSource: $connectionType === ProviderConnectionType::Platform ? 'platform_config' : ProviderCredentialSource::LegacyMigrated->value,
reasonCode: ProviderReasonCodes::ProviderConnectionReviewRequired,
message: 'Provider connection requires migration review before use.',
targetScope: $targetScope instanceof ProviderConnectionTargetScopeDescriptor ? $targetScope : null,
providerContextDetails: $providerContextDetails,
);
}
if ($connectionType === ProviderConnectionType::Platform) {
return $this->platformResolver->resolve(
targetScopeIdentifier: $targetScopeIdentifier,
targetScope: $targetScope instanceof ProviderConnectionTargetScopeDescriptor ? $targetScope : null,
providerContextDetails: $providerContextDetails,
);
}
return $this->resolveDedicatedIdentity(
connection: $connection,
targetScopeIdentifier: $targetScopeIdentifier,
targetScope: $targetScope instanceof ProviderConnectionTargetScopeDescriptor ? $targetScope : null,
providerContextDetails: $providerContextDetails,
);
}
private function resolveConnectionType(ProviderConnection $connection): ?ProviderConnectionType
{
$value = $connection->connection_type;
if ($value instanceof ProviderConnectionType) {
return $value;
}
if (! is_string($value)) {
return null;
}
return ProviderConnectionType::tryFrom(trim($value));
}
private function resolveDedicatedIdentity(
ProviderConnection $connection,
string $targetScopeIdentifier,
?ProviderConnectionTargetScopeDescriptor $targetScope = null,
array $providerContextDetails = [],
): ProviderIdentityResolution {
try {
$credentials = $this->credentials->getClientCredentials($connection);
} catch (InvalidArgumentException|RuntimeException $exception) {
return ProviderIdentityResolution::blocked(
connectionType: ProviderConnectionType::Dedicated,
credentialSource: $this->credentialSource($connection),
reasonCode: $exception instanceof InvalidArgumentException
? ProviderReasonCodes::DedicatedCredentialInvalid
: ProviderReasonCodes::DedicatedCredentialMissing,
message: $exception->getMessage(),
targetScope: $targetScope,
providerContextDetails: $providerContextDetails,
);
}
if (! $targetScope instanceof ProviderConnectionTargetScopeDescriptor) {
$targetScope = ProviderConnectionTargetScopeDescriptor::fromInput(
provider: 'microsoft',
scopeKind: ProviderConnectionTargetScopeDescriptor::SCOPE_KIND_TENANT,
scopeIdentifier: $targetScopeIdentifier !== '' ? $targetScopeIdentifier : 'organizations',
);
}
return ProviderIdentityResolution::resolved(
connectionType: ProviderConnectionType::Dedicated,
targetScope: $targetScope,
effectiveClientId: $credentials['client_id'],
credentialSource: $this->credentialSource($connection),
clientSecret: $credentials['client_secret'],
authorityTenant: $targetScope->scopeIdentifier,
redirectUri: trim((string) route('admin.consent.callback')),
providerContextDetails: $providerContextDetails,
);
}
private function credentialSource(ProviderConnection $connection): string
{
$credential = $connection->credential;
if (! $credential instanceof ProviderCredential) {
return ProviderCredentialSource::DedicatedManual->value;
}
$source = $credential->source;
if ($source instanceof ProviderCredentialSource) {
return $source->value;
}
if (is_string($source) && $source !== '') {
return $source;
}
return ProviderCredentialSource::DedicatedManual->value;
}
}