-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor gpg import to use machine readable colon format
Signed-off-by: Aleksei Khudiakov <aleksey@xerkus.pro>
- Loading branch information
Showing
2 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Gpg\Value; | ||
|
||
use Laminas\AutomaticReleases\Gpg\SecretKeyId; | ||
use Psl\Str; | ||
|
||
use function in_array; | ||
use function str_contains; | ||
|
||
final readonly class ColonFormattedKeyRecord | ||
{ | ||
private const FIELD_TYPE = 0; | ||
private const FIELD_KEYID = 4; | ||
private const FIELD_CAPABILITIES = 11; | ||
|
||
private function __construct( | ||
private bool $isSubkey, | ||
private bool $hasSecretKey, | ||
private SecretKeyId $keyId, | ||
private string $capabilities, | ||
) { | ||
} | ||
|
||
public static function fromRecordLine(string $recordLine): self|null | ||
{ | ||
$record = Str\split($recordLine, ':'); | ||
$type = $record[self::FIELD_TYPE] ?? ''; | ||
if (! in_array($type, ['pub', 'sec', 'sub', 'ssb'])) { | ||
return null; | ||
} | ||
|
||
$isSubkey = in_array($type, ['sub', 'ssb']); | ||
$hasSecretKey = in_array($type, ['sec', 'ssb']); | ||
$keyId = SecretKeyId::fromBase16String($record[self::FIELD_KEYID] ?? ''); | ||
$capabilities = $record[self::FIELD_CAPABILITIES] ?? ''; | ||
|
||
return new self($isSubkey, $hasSecretKey, $keyId, $capabilities); | ||
} | ||
|
||
public function isPrimaryKey(): bool | ||
{ | ||
return ! $this->isSubkey; | ||
} | ||
|
||
public function isSubkey(): bool | ||
{ | ||
return $this->isSubkey; | ||
} | ||
|
||
public function hasSecretKey(): bool | ||
{ | ||
return $this->hasSecretKey; | ||
} | ||
|
||
public function keyId(): SecretKeyId | ||
{ | ||
return $this->keyId; | ||
} | ||
|
||
public function hasSignCapability(): bool | ||
{ | ||
return str_contains($this->capabilities, 's'); | ||
} | ||
} |