Skip to content

Commit 971a2d9

Browse files
committed
KeyEditor: Add expire() method
1 parent db2c276 commit 971a2d9

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

Crypt/GPG/KeyEditor.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,39 @@ public function deleteUserId(Crypt_GPG_UserId $userid, $by_email = false)
220220
return $this;
221221
}
222222

223+
/**
224+
* Set expiration date for a primary key (`expire`).
225+
*
226+
* @param string|int $period Validation period: Either of:
227+
* - 0 or an empty string for no expiration
228+
* - <n> - expiration in days
229+
* - <n>w - expiration in weeks
230+
* - <n>m - expiration in months
231+
* - <n>y - expiration in years
232+
*
233+
* @return Crypt_GPG_KeyEditor The current object, for fluent interface.
234+
*/
235+
public function expire($period = '')
236+
{
237+
if ($period === '') {
238+
$period = '0';
239+
}
240+
241+
if (!preg_match('/^[0-9]+[wmy]?$/i', (string) $period)) {
242+
$this->_close();
243+
throw new Crypt_GPG_Exception('Failed to set expiration. Invalid period specification.');
244+
}
245+
246+
$handlers = [
247+
'keygen.valid' => (string) $period,
248+
'passphrase.enter' => $this->passphrase,
249+
];
250+
251+
$this->_write('expire')->_read($handlers, ['keyedit.prompt']);
252+
253+
return $this;
254+
}
255+
223256
/**
224257
* Change a key passphrase (`passwd`).
225258
*

tests/KeyEditorTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ public function testDeleteUserIdUnknown()
183183
$keyEditor->edit('second-keypair@example.com', 'test2')->deleteUserId($user)->save();
184184
}
185185

186+
/**
187+
* Test `expire` command
188+
*/
189+
public function testExpire()
190+
{
191+
$keyEditor = $this->gpg->getKeyEditor();
192+
193+
// Test setting an expiration date (one year from today)
194+
$keyEditor->edit('second-keypair@example.com', 'test2')->expire('1y')->save();
195+
196+
$keys = $this->gpg->getKeys('second-keypair@example.com');
197+
$primary = $keys[0]->getPrimaryKey();
198+
$this->assertSame((date('Y') + 1) . date('-m-d'), $primary->getExpirationDateTime()->format('Y-m-d'));
199+
200+
// Test unsetting an expiration date
201+
$keyEditor->edit('second-keypair@example.com', 'test2')->expire(0)->save();
202+
203+
$keys = $this->gpg->getKeys('second-keypair@example.com');
204+
$primary = $keys[0]->getPrimaryKey();
205+
$this->assertNull($primary->getExpirationDateTime());
206+
207+
// Test invalid period
208+
$this->expectException('Crypt_GPG_Exception');
209+
$keyEditor->edit('second-keypair@example.com', 'test2')->expire('-1')->save();
210+
}
211+
186212
/**
187213
* Test `passwd` command
188214
*/
@@ -218,8 +244,9 @@ public function testRevokeUserId()
218244
$keys = $this->gpg->getKeys('second-keypair@example.com');
219245
$userIds = $keys[0]->getUserIds();
220246
$this->assertCount(2, $userIds);
221-
$userIds = array_filter($userIds, function ($id) use ($user1) { return $id->getEmail() == $user1->getEmail(); });
247+
$userIds = array_values(array_filter($userIds, function ($id) use ($user1) { return $id->getEmail() == $user1->getEmail(); }));
222248
$this->assertCount(1, $userIds);
249+
$this->assertTrue($userIds[0]->isRevoked());
223250

224251
// Test revoking the last user
225252
$user = new Crypt_GPG_UserId('Second Keypair Test Key (do not encrypt important data with this key) <second-keypair@example.com>');

0 commit comments

Comments
 (0)