| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Webauthn\Util; |
| 6 | |
| 7 | use Webauthn\CredentialRecord; |
| 8 | use Webauthn\PublicKeyCredentialSource; |
| 9 | |
| 10 | /** |
| 11 | * Utility class to convert between PublicKeyCredentialSource and CredentialRecord. |
| 12 | * |
| 13 | * This class provides helper methods to facilitate migration from the deprecated |
| 14 | * PublicKeyCredentialSource class to the new CredentialRecord class. |
| 15 | */ |
| 16 | final class CredentialRecordConverter |
| 17 | { |
| 18 | /** |
| 19 | * Converts a CredentialRecord (or subclass) to a pure CredentialRecord. |
| 20 | * |
| 21 | * Since PublicKeyCredentialSource extends CredentialRecord, this method |
| 22 | * can accept either type. If the input is already a pure CredentialRecord, |
| 23 | * it is returned as-is. Otherwise, a new CredentialRecord is created. |
| 24 | * |
| 25 | * @param CredentialRecord $source The source credential to convert |
| 26 | * @return CredentialRecord The converted credential record |
| 27 | */ |
| 28 | public static function toCredentialRecord(CredentialRecord $source): CredentialRecord |
| 29 | { |
| 30 | // Since PublicKeyCredentialSource now extends CredentialRecord, |
| 31 | // any PublicKeyCredentialSource is already a CredentialRecord. |
| 32 | // If it's already a pure CredentialRecord (not a subclass), return as-is. |
| 33 | // Otherwise, create a new CredentialRecord to "strip" the subclass. |
| 34 | if ($source::class === CredentialRecord::class) { |
| 35 | return $source; |
| 36 | } |
| 37 | |
| 38 | return CredentialRecord::create( |
| 39 | $source->publicKeyCredentialId, |
| 40 | $source->type, |
| 41 | $source->transports, |
| 42 | $source->attestationType, |
| 43 | $source->trustPath, |
| 44 | $source->aaguid, |
| 45 | $source->credentialPublicKey, |
| 46 | $source->userHandle, |
| 47 | $source->counter, |
| 48 | $source->otherUI, |
| 49 | $source->backupEligible, |
| 50 | $source->backupStatus, |
| 51 | $source->uvInitialized, |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Converts a CredentialRecord to a PublicKeyCredentialSource. |
| 57 | * |
| 58 | * This method creates a PublicKeyCredentialSource from a CredentialRecord. |
| 59 | * Note that PublicKeyCredentialSource is deprecated and should only be used |
| 60 | * for backward compatibility. |
| 61 | * |
| 62 | * @param CredentialRecord $record The credential record to convert |
| 63 | * @return PublicKeyCredentialSource The converted credential source |
| 64 | * @deprecated since 5.3, PublicKeyCredentialSource is deprecated. Use CredentialRecord directly. |
| 65 | */ |
| 66 | public static function toPublicKeyCredentialSource(CredentialRecord $record): PublicKeyCredentialSource |
| 67 | { |
| 68 | // If already a PublicKeyCredentialSource, return as-is |
| 69 | if ($record instanceof PublicKeyCredentialSource) { |
| 70 | return $record; |
| 71 | } |
| 72 | |
| 73 | return new PublicKeyCredentialSource( |
| 74 | $record->publicKeyCredentialId, |
| 75 | $record->type, |
| 76 | $record->transports, |
| 77 | $record->attestationType, |
| 78 | $record->trustPath, |
| 79 | $record->aaguid, |
| 80 | $record->credentialPublicKey, |
| 81 | $record->userHandle, |
| 82 | $record->counter, |
| 83 | $record->otherUI, |
| 84 | $record->backupEligible, |
| 85 | $record->backupStatus, |
| 86 | $record->uvInitialized, |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Converts an array of CredentialRecord (or subclass) instances to pure CredentialRecord instances. |
| 92 | * |
| 93 | * @param CredentialRecord[] $sources Array of credential records |
| 94 | * @return CredentialRecord[] Array of credential records |
| 95 | */ |
| 96 | public static function toCredentialRecords(array $sources): array |
| 97 | { |
| 98 | return array_map(self::toCredentialRecord(...), $sources); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Converts an array of CredentialRecord instances to PublicKeyCredentialSource instances. |
| 103 | * |
| 104 | * @param CredentialRecord[] $records Array of credential records |
| 105 | * @return PublicKeyCredentialSource[] Array of credential sources |
| 106 | * @deprecated since 5.3, PublicKeyCredentialSource is deprecated. Use CredentialRecord directly. |
| 107 | */ |
| 108 | public static function toPublicKeyCredentialSources(array $records): array |
| 109 | { |
| 110 | return array_map(self::toPublicKeyCredentialSource(...), $records); |
| 111 | } |
| 112 | } |