pagemachine / authorized-keys
Read, edit and write the SSH authorized_keys file
Installs: 41 012
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 5
Forks: 2
Open Issues: 1
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- ergebnis/composer-normalize: ^2.9
- mikey179/vfsstream: ^1.6
- php-parallel-lint/php-console-highlighter: ^1.0.0
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.3
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.2
README
Read, edit and write the SSH authorized_keys
file.
Installation
composer require pagemachine/authorized-keys
Usage
To access the authorized_keys
file an instance of Pagemachine\AuthorizedKeys\AuthorizedKeys
must be created, either directly passing the file content to the constructor or using the static fromFile()
method:
$path = '/home/foo/.ssh/authorized_keys'; $authorizedKeys = AuthorizedKeys::fromFile($path);
You can easily iterate all keys in the file, comments and empty lines will be skipped:
foreach ($authorizedKeys as $key) {
// Do something with $key
}
To add a key, create an instance of Pagemachine\AuthorizedKeys\PublicKey
and add it to the file:
// ... load $authorizedKeys ... $key = new PublicKey('ssh-rsa AAA...'); $authorizedKeys->addKey($key);
Notice that each key is only added once, if you add it again, only its options, type and comment are updated accordingly. Thus it's safe to call this method in any case to ensure keys are present.
To remove a key, wrap it in PublicKey
and remove it from the file:
// ... load $authorizedKeys ... $key = new PublicKey('ssh-rsa AAA...'); $authorizedKeys->removeKey($key);
To close things off, write back the file, comments and empty lines will be left unchanged:
$authorizedKeys->toFile($path);
The permissions of the file will be changed to 0600, namely readable and writable by the owner but nobody else.
Testing
All tests can be executed with the shipped Docker Compose definition:
docker compose run --rm app composer build