From a7814b071c79bc98947baac7aead601fd897fcb7 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 10 Feb 2015 15:44:21 -0800 Subject: [PATCH] Add auth.querypublickeys to retrieve public keys Summary: Fixes T6484. I primarily need this to synchronize device public keys in the Phabricator cluster so the new stuff in T2783 works. Although, actually, maybe I don't really need it. But I wrote it anyway and it's desirable to have sooner or later. Test Plan: Ran method. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T6484 Differential Revision: https://secure.phabricator.com/D11163 --- src/__phutil_library_map__.php | 4 + .../PhabricatorAuthConduitAPIMethod.php | 19 +++++ ...torAuthQueryPublicKeysConduitAPIMethod.php | 77 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/applications/auth/conduit/PhabricatorAuthConduitAPIMethod.php create mode 100644 src/applications/auth/conduit/PhabricatorAuthQueryPublicKeysConduitAPIMethod.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 81f347c2c9..b2dc43008a 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1333,6 +1333,7 @@ phutil_register_library_map(array( 'PhabricatorAuthApplication' => 'applications/auth/application/PhabricatorAuthApplication.php', 'PhabricatorAuthAuthFactorPHIDType' => 'applications/auth/phid/PhabricatorAuthAuthFactorPHIDType.php', 'PhabricatorAuthAuthProviderPHIDType' => 'applications/auth/phid/PhabricatorAuthAuthProviderPHIDType.php', + 'PhabricatorAuthConduitAPIMethod' => 'applications/auth/conduit/PhabricatorAuthConduitAPIMethod.php', 'PhabricatorAuthConfirmLinkController' => 'applications/auth/controller/PhabricatorAuthConfirmLinkController.php', 'PhabricatorAuthController' => 'applications/auth/controller/PhabricatorAuthController.php', 'PhabricatorAuthDAO' => 'applications/auth/storage/PhabricatorAuthDAO.php', @@ -1379,6 +1380,7 @@ phutil_register_library_map(array( 'PhabricatorAuthProviderConfigQuery' => 'applications/auth/query/PhabricatorAuthProviderConfigQuery.php', 'PhabricatorAuthProviderConfigTransaction' => 'applications/auth/storage/PhabricatorAuthProviderConfigTransaction.php', 'PhabricatorAuthProviderConfigTransactionQuery' => 'applications/auth/query/PhabricatorAuthProviderConfigTransactionQuery.php', + 'PhabricatorAuthQueryPublicKeysConduitAPIMethod' => 'applications/auth/conduit/PhabricatorAuthQueryPublicKeysConduitAPIMethod.php', 'PhabricatorAuthRegisterController' => 'applications/auth/controller/PhabricatorAuthRegisterController.php', 'PhabricatorAuthRevokeTokenController' => 'applications/auth/controller/PhabricatorAuthRevokeTokenController.php', 'PhabricatorAuthSSHKey' => 'applications/auth/storage/PhabricatorAuthSSHKey.php', @@ -4551,6 +4553,7 @@ phutil_register_library_map(array( 'PhabricatorAuthApplication' => 'PhabricatorApplication', 'PhabricatorAuthAuthFactorPHIDType' => 'PhabricatorPHIDType', 'PhabricatorAuthAuthProviderPHIDType' => 'PhabricatorPHIDType', + 'PhabricatorAuthConduitAPIMethod' => 'ConduitAPIMethod', 'PhabricatorAuthConfirmLinkController' => 'PhabricatorAuthController', 'PhabricatorAuthController' => 'PhabricatorController', 'PhabricatorAuthDAO' => 'PhabricatorLiskDAO', @@ -4599,6 +4602,7 @@ phutil_register_library_map(array( 'PhabricatorAuthProviderConfigQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorAuthProviderConfigTransaction' => 'PhabricatorApplicationTransaction', 'PhabricatorAuthProviderConfigTransactionQuery' => 'PhabricatorApplicationTransactionQuery', + 'PhabricatorAuthQueryPublicKeysConduitAPIMethod' => 'PhabricatorAuthConduitAPIMethod', 'PhabricatorAuthRegisterController' => 'PhabricatorAuthController', 'PhabricatorAuthRevokeTokenController' => 'PhabricatorAuthController', 'PhabricatorAuthSSHKey' => array( diff --git a/src/applications/auth/conduit/PhabricatorAuthConduitAPIMethod.php b/src/applications/auth/conduit/PhabricatorAuthConduitAPIMethod.php new file mode 100644 index 0000000000..07d2a4b157 --- /dev/null +++ b/src/applications/auth/conduit/PhabricatorAuthConduitAPIMethod.php @@ -0,0 +1,19 @@ + 'optional list', + 'objectPHIDs' => 'optional list', + 'keys' => 'optional list', + ) + self::getPagerParamTypes(); + } + + public function defineReturnType() { + return 'result-set'; + } + + public function defineErrorTypes() { + return array(); + } + + protected function execute(ConduitAPIRequest $request) { + $viewer = $request->getUser(); + + $query = id(new PhabricatorAuthSSHKeyQuery()) + ->setViewer($viewer); + + $ids = $request->getValue('ids'); + if ($ids !== null) { + $query->withIDs($ids); + } + + $object_phids = $request->getValue('objectPHIDs'); + if ($object_phids !== null) { + $query->withObjectPHIDs($object_phids); + } + + $keys = $request->getValue('keys'); + if ($keys !== null) { + $key_objects = array(); + foreach ($keys as $key) { + $key_objects[] = PhabricatorAuthSSHPublicKey::newFromRawKey($key); + } + + $query->withKeys($key_objects); + } + + $pager = $this->newPager($request); + $public_keys = $query->executeWithCursorPager($pager); + + $data = array(); + foreach ($public_keys as $public_key) { + $data[] = array( + 'id' => $public_key->getID(), + 'name' => $public_key->getName(), + 'objectPHID' => $public_key->getObjectPHID(), + 'isTrusted' => (bool)$public_key->getIsTrusted(), + 'key' => $public_key->getEntireKey(), + ); + } + + $results = array( + 'data' => $data, + ); + + return $this->addPagerResults($results, $pager); + } + +}