Path: blob/master/src/applications/passphrase/conduit/PassphraseQueryConduitAPIMethod.php
12256 views
<?php12final class PassphraseQueryConduitAPIMethod3extends PassphraseConduitAPIMethod {45public function getAPIMethodName() {6return 'passphrase.query';7}89public function getMethodDescription() {10return pht('Query credentials.');11}1213public function newQueryObject() {14return new PassphraseCredentialQuery();15}1617protected function defineParamTypes() {18return array(19'ids' => 'optional list<int>',20'phids' => 'optional list<phid>',21'needSecrets' => 'optional bool',22'needPublicKeys' => 'optional bool',23);24}2526protected function defineReturnType() {27return 'list<dict>';28}2930protected function execute(ConduitAPIRequest $request) {31$query = $this->newQueryForRequest($request);3233if ($request->getValue('ids')) {34$query->withIDs($request->getValue('ids'));35}3637if ($request->getValue('phids')) {38$query->withPHIDs($request->getValue('phids'));39}4041if ($request->getValue('needSecrets')) {42$query->needSecrets(true);43}4445$pager = $this->newPager($request);46$credentials = $query->executeWithCursorPager($pager);4748$results = array();49foreach ($credentials as $credential) {50$type = PassphraseCredentialType::getTypeByConstant(51$credential->getCredentialType());52if (!$type) {53continue;54}5556$public_key = null;57if ($request->getValue('needPublicKeys') && $type->hasPublicKey()) {58$public_key = $type->getPublicKey(59$request->getUser(),60$credential);61}6263$material = array();6465$is_locked = $credential->getIsLocked();66$allow_api = ($credential->getAllowConduit() && !$is_locked);6768$secret = null;69if ($request->getValue('needSecrets')) {70if ($allow_api) {71$secret = $credential->getSecret();72if ($secret) {73$secret = $secret->openEnvelope();74} else {75$material['destroyed'] = pht(76'The private material for this credential has been '.77'destroyed.');78}79}80}8182switch ($credential->getCredentialType()) {83case PassphraseSSHPrivateKeyFileCredentialType::CREDENTIAL_TYPE:84if ($secret !== null) {85$material['file'] = $secret;86}87if ($public_key) {88$material['publicKey'] = $public_key;89}90break;91case PassphraseSSHGeneratedKeyCredentialType::CREDENTIAL_TYPE:92case PassphraseSSHPrivateKeyTextCredentialType::CREDENTIAL_TYPE:93if ($secret !== null) {94$material['privateKey'] = $secret;95}96if ($public_key) {97$material['publicKey'] = $public_key;98}99break;100case PassphrasePasswordCredentialType::CREDENTIAL_TYPE:101if ($secret !== null) {102$material['password'] = $secret;103}104break;105case PassphraseTokenCredentialType::CREDENTIAL_TYPE:106if ($secret !== null) {107$material['token'] = $secret;108}109break;110}111112if (!$allow_api) {113$material['noAPIAccess'] = pht(114'This private material for this credential is not accessible via '.115'API calls.');116}117118$results[$credential->getPHID()] = array(119'id' => $credential->getID(),120'phid' => $credential->getPHID(),121'type' => $credential->getCredentialType(),122'name' => $credential->getName(),123'description' => $credential->getDescription(),124'uri' =>125PhabricatorEnv::getProductionURI('/'.$credential->getMonogram()),126'monogram' => $credential->getMonogram(),127'username' => $credential->getUsername(),128'material' => $material,129);130}131132$result = array(133'data' => $results,134);135136return $this->addPagerResults($result, $pager);137}138139}140141142