1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-15 19:32:40 +01:00
phorge-phorge/src/applications/passphrase/keys/PassphraseAbstractKey.php
epriestley 51fb1ca16d Migrate repositories to use Passphrase for credential management
Summary: Fixes T4122. Ref T2230. Instead of storing credentials on each repository, store them in Passphrase. This allows easy creation/management of many repositories which share credentials.

Test Plan:
  - Upgraded repositories.
  - Created and edited repositories.
  - Pulled HTTP and SSH repositories.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2230, T4122

Differential Revision: https://secure.phabricator.com/D7629
2013-11-22 15:23:33 -08:00

74 lines
1.7 KiB
PHP

<?php
abstract class PassphraseAbstractKey extends Phobject {
private $credential;
protected function requireCredential() {
if (!$this->credential) {
throw new Exception(pht("Credential is required!"));
}
return $this->credential;
}
private function loadCredential(
$phid,
PhabricatorUser $viewer) {
$credential = id(new PassphraseCredentialQuery())
->setViewer($viewer)
->withPHIDs(array($phid))
->needSecrets(true)
->executeOne();
if (!$credential) {
throw new Exception(pht('Failed to load credential "%s"!', $phid));
}
return $credential;
}
private function validateCredential(
PassphraseCredential $credential,
$provides_type) {
$type = $credential->getCredentialTypeImplementation();
if (!$type) {
throw new Exception(
pht(
'Credential "%s" is of unknown type "%s"!',
'K'.$credential->getID(),
$credential->getCredentialType()));
}
if ($type->getProvidesType() !== $provides_type) {
throw new Exception(
pht(
'Credential "%s" must provide "%s", but provides "%s"!',
'K'.$credential->getID(),
$provides_type,
$type->getProvidesType()));
}
}
protected function loadAndValidateFromPHID(
$phid,
PhabricatorUser $viewer,
$type) {
$credential = $this->loadCredential($phid, $viewer);
$this->validateCredential($credential, $type);
$this->credential = $credential;
return $this;
}
public function getUsernameEnvelope() {
$credential = $this->requireCredential();
return new PhutilOpaqueEnvelope($credential->getUsername());
}
}