mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-16 03:42:41 +01:00
61b26255bb
Summary: Ref T4122. These classes provide typed, checked access to credentials, so you can say "give me this password, and throw if anything is funky". Test Plan: Used in next revision. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T4122 Differential Revision: https://secure.phabricator.com/D7625
66 lines
1.4 KiB
PHP
66 lines
1.4 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->getCredentialType();
|
|
if ($type->getProvides() !== $provides_type) {
|
|
throw new Exception(
|
|
pht(
|
|
'Credential "%s" must provide "%s", but provides "%s"!',
|
|
'K'.$credential->getID(),
|
|
$provides_type,
|
|
$type->getProvides()));
|
|
}
|
|
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
}
|