2014-08-16 14:41:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PassphraseQueryConduitAPIMethod
|
|
|
|
extends PassphraseConduitAPIMethod {
|
|
|
|
|
|
|
|
public function getAPIMethodName() {
|
|
|
|
return 'passphrase.query';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMethodDescription() {
|
|
|
|
return pht('Query credentials.');
|
|
|
|
}
|
|
|
|
|
2015-05-06 00:59:44 +02:00
|
|
|
public function newQueryObject() {
|
|
|
|
return new PassphraseCredentialQuery();
|
|
|
|
}
|
|
|
|
|
2015-04-13 00:59:07 +02:00
|
|
|
protected function defineParamTypes() {
|
2014-08-16 14:41:03 +02:00
|
|
|
return array(
|
2015-05-06 00:59:44 +02:00
|
|
|
'ids' => 'optional list<int>',
|
|
|
|
'phids' => 'optional list<phid>',
|
|
|
|
'needSecrets' => 'optional bool',
|
|
|
|
'needPublicKeys' => 'optional bool',
|
|
|
|
);
|
2014-08-16 14:41:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 00:59:07 +02:00
|
|
|
protected function defineReturnType() {
|
2014-08-16 14:41:03 +02:00
|
|
|
return 'list<dict>';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(ConduitAPIRequest $request) {
|
2015-05-06 00:59:44 +02:00
|
|
|
$query = $this->newQueryForRequest($request);
|
2014-08-16 14:41:03 +02:00
|
|
|
|
|
|
|
if ($request->getValue('ids')) {
|
|
|
|
$query->withIDs($request->getValue('ids'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->getValue('phids')) {
|
|
|
|
$query->withPHIDs($request->getValue('phids'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->getValue('needSecrets')) {
|
|
|
|
$query->needSecrets(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pager = $this->newPager($request);
|
|
|
|
$credentials = $query->executeWithCursorPager($pager);
|
|
|
|
|
|
|
|
$results = array();
|
|
|
|
foreach ($credentials as $credential) {
|
|
|
|
$type = PassphraseCredentialType::getTypeByConstant(
|
|
|
|
$credential->getCredentialType());
|
|
|
|
if (!$type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$public_key = null;
|
|
|
|
if ($request->getValue('needPublicKeys') && $type->hasPublicKey()) {
|
|
|
|
$public_key = $type->getPublicKey(
|
|
|
|
$request->getUser(),
|
|
|
|
$credential);
|
|
|
|
}
|
|
|
|
|
2015-05-06 00:59:44 +02:00
|
|
|
$material = array();
|
|
|
|
|
2016-05-18 21:31:20 +02:00
|
|
|
$is_locked = $credential->getIsLocked();
|
|
|
|
$allow_api = ($credential->getAllowConduit() && !$is_locked);
|
|
|
|
|
2014-08-16 14:41:03 +02:00
|
|
|
$secret = null;
|
|
|
|
if ($request->getValue('needSecrets')) {
|
2016-05-18 21:31:20 +02:00
|
|
|
if ($allow_api) {
|
2015-05-06 00:59:44 +02:00
|
|
|
$secret = $credential->getSecret();
|
|
|
|
if ($secret) {
|
|
|
|
$secret = $secret->openEnvelope();
|
|
|
|
} else {
|
|
|
|
$material['destroyed'] = pht(
|
|
|
|
'The private material for this credential has been '.
|
|
|
|
'destroyed.');
|
|
|
|
}
|
2014-08-16 14:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($credential->getCredentialType()) {
|
2015-06-14 06:11:55 +02:00
|
|
|
case PassphraseSSHPrivateKeyFileCredentialType::CREDENTIAL_TYPE:
|
2016-11-15 18:07:45 +01:00
|
|
|
if ($secret !== null) {
|
2014-08-16 14:41:03 +02:00
|
|
|
$material['file'] = $secret;
|
|
|
|
}
|
|
|
|
if ($public_key) {
|
|
|
|
$material['publicKey'] = $public_key;
|
|
|
|
}
|
|
|
|
break;
|
2015-06-14 06:11:55 +02:00
|
|
|
case PassphraseSSHGeneratedKeyCredentialType::CREDENTIAL_TYPE:
|
|
|
|
case PassphraseSSHPrivateKeyTextCredentialType::CREDENTIAL_TYPE:
|
2016-11-15 18:07:45 +01:00
|
|
|
if ($secret !== null) {
|
2014-08-16 14:41:03 +02:00
|
|
|
$material['privateKey'] = $secret;
|
|
|
|
}
|
|
|
|
if ($public_key) {
|
|
|
|
$material['publicKey'] = $public_key;
|
|
|
|
}
|
|
|
|
break;
|
2015-06-14 06:11:55 +02:00
|
|
|
case PassphrasePasswordCredentialType::CREDENTIAL_TYPE:
|
2016-11-15 18:07:45 +01:00
|
|
|
if ($secret !== null) {
|
2014-08-16 14:41:03 +02:00
|
|
|
$material['password'] = $secret;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-15 18:07:45 +01:00
|
|
|
case PassphraseTokenCredentialType::CREDENTIAL_TYPE:
|
|
|
|
if ($secret !== null) {
|
|
|
|
$material['token'] = $secret;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-16 14:41:03 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 21:31:20 +02:00
|
|
|
if (!$allow_api) {
|
2014-08-16 14:41:03 +02:00
|
|
|
$material['noAPIAccess'] = pht(
|
2015-05-06 00:59:44 +02:00
|
|
|
'This private material for this credential is not accessible via '.
|
|
|
|
'API calls.');
|
2014-08-16 14:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$results[$credential->getPHID()] = array(
|
|
|
|
'id' => $credential->getID(),
|
|
|
|
'phid' => $credential->getPHID(),
|
|
|
|
'type' => $credential->getCredentialType(),
|
|
|
|
'name' => $credential->getName(),
|
2015-05-06 00:59:44 +02:00
|
|
|
'description' => $credential->getDescription(),
|
2014-08-16 14:41:03 +02:00
|
|
|
'uri' =>
|
|
|
|
PhabricatorEnv::getProductionURI('/'.$credential->getMonogram()),
|
|
|
|
'monogram' => $credential->getMonogram(),
|
|
|
|
'username' => $credential->getUsername(),
|
|
|
|
'material' => $material,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array(
|
|
|
|
'data' => $results,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->addPagerResults($result, $pager);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|