From 63d755723bcf531b51dc90bbabde5371c3a38fbf Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 14 Oct 2015 18:34:55 -0700 Subject: [PATCH] Add a "Token" Credential type Summary: Ref T9456. This is just a convenience type for things like API tokens, to make it harder for users to make mistakes and keep SSH keys out of the dropdown for "choose your API token". Test Plan: {F879820} Reviewers: chad Reviewed By: chad Subscribers: joshuaspence Maniphest Tasks: T9456 Differential Revision: https://secure.phabricator.com/D14284 --- src/__phutil_library_map__.php | 2 + .../PassphraseCredentialViewController.php | 10 ++--- .../PassphraseTokenCredentialType.php | 37 +++++++++++++++++++ .../PassphraseCredentialTransactionEditor.php | 2 +- .../passphrase/keys/PassphraseAbstractKey.php | 6 +-- .../query/PassphraseCredentialQuery.php | 11 ++++++ .../storage/PassphraseCredential.php | 10 +++++ 7 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/applications/passphrase/credentialtype/PassphraseTokenCredentialType.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 02a011ec72..5ef56bd01a 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1645,6 +1645,7 @@ phutil_register_library_map(array( 'PassphraseSSHPrivateKeyTextCredentialType' => 'applications/passphrase/credentialtype/PassphraseSSHPrivateKeyTextCredentialType.php', 'PassphraseSchemaSpec' => 'applications/passphrase/storage/PassphraseSchemaSpec.php', 'PassphraseSecret' => 'applications/passphrase/storage/PassphraseSecret.php', + 'PassphraseTokenCredentialType' => 'applications/passphrase/credentialtype/PassphraseTokenCredentialType.php', 'PasteConduitAPIMethod' => 'applications/paste/conduit/PasteConduitAPIMethod.php', 'PasteCreateConduitAPIMethod' => 'applications/paste/conduit/PasteCreateConduitAPIMethod.php', 'PasteCreateMailReceiver' => 'applications/paste/mail/PasteCreateMailReceiver.php', @@ -5949,6 +5950,7 @@ phutil_register_library_map(array( 'PassphraseSSHPrivateKeyTextCredentialType' => 'PassphraseSSHPrivateKeyCredentialType', 'PassphraseSchemaSpec' => 'PhabricatorConfigSchemaSpec', 'PassphraseSecret' => 'PassphraseDAO', + 'PassphraseTokenCredentialType' => 'PassphraseCredentialType', 'PasteConduitAPIMethod' => 'ConduitAPIMethod', 'PasteCreateConduitAPIMethod' => 'PasteConduitAPIMethod', 'PasteCreateMailReceiver' => 'PhabricatorMailReceiver', diff --git a/src/applications/passphrase/controller/PassphraseCredentialViewController.php b/src/applications/passphrase/controller/PassphraseCredentialViewController.php index 46bb13a688..db31964773 100644 --- a/src/applications/passphrase/controller/PassphraseCredentialViewController.php +++ b/src/applications/passphrase/controller/PassphraseCredentialViewController.php @@ -14,20 +14,16 @@ final class PassphraseCredentialViewController extends PassphraseController { return new Aphront404Response(); } - $type = PassphraseCredentialType::getTypeByConstant( - $credential->getCredentialType()); - if (!$type) { - throw new Exception(pht('Credential has invalid type "%s"!', $type)); - } + $type = $credential->getImplementation(); $timeline = $this->buildTransactionTimeline( $credential, new PassphraseCredentialTransactionQuery()); $timeline->setShouldTerminate(true); - $title = pht('%s %s', 'K'.$credential->getID(), $credential->getName()); + $title = pht('%s %s', $credential->getMonogram(), $credential->getName()); $crumbs = $this->buildApplicationCrumbs(); - $crumbs->addTextCrumb('K'.$credential->getID()); + $crumbs->addTextCrumb($credential->getMonogram()); $crumbs->setBorder(true); $header = $this->buildHeaderView($credential); diff --git a/src/applications/passphrase/credentialtype/PassphraseTokenCredentialType.php b/src/applications/passphrase/credentialtype/PassphraseTokenCredentialType.php new file mode 100644 index 0000000000..d057d61eba --- /dev/null +++ b/src/applications/passphrase/credentialtype/PassphraseTokenCredentialType.php @@ -0,0 +1,37 @@ +getCredentialTypeImplementation(); + $credential_type = $object->getImplementation(); if (!$credential_type->shouldRequireUsername()) { break; } diff --git a/src/applications/passphrase/keys/PassphraseAbstractKey.php b/src/applications/passphrase/keys/PassphraseAbstractKey.php index da56de15af..e3d902236d 100644 --- a/src/applications/passphrase/keys/PassphraseAbstractKey.php +++ b/src/applications/passphrase/keys/PassphraseAbstractKey.php @@ -32,13 +32,13 @@ abstract class PassphraseAbstractKey extends Phobject { PassphraseCredential $credential, $provides_type) { - $type = $credential->getCredentialTypeImplementation(); + $type = $credential->getImplementation(); if (!$type) { throw new Exception( pht( 'Credential "%s" is of unknown type "%s"!', - 'K'.$credential->getID(), + $credential->getMonogram(), $credential->getCredentialType())); } @@ -46,7 +46,7 @@ abstract class PassphraseAbstractKey extends Phobject { throw new Exception( pht( 'Credential "%s" must provide "%s", but provides "%s"!', - 'K'.$credential->getID(), + $credential->getMonogram(), $provides_type, $type->getProvidesType())); } diff --git a/src/applications/passphrase/query/PassphraseCredentialQuery.php b/src/applications/passphrase/query/PassphraseCredentialQuery.php index 9411fa8a77..3a78ac0d13 100644 --- a/src/applications/passphrase/query/PassphraseCredentialQuery.php +++ b/src/applications/passphrase/query/PassphraseCredentialQuery.php @@ -89,6 +89,17 @@ final class PassphraseCredentialQuery } } + foreach ($page as $key => $credential) { + $type = PassphraseCredentialType::getTypeByConstant( + $credential->getCredentialType()); + if (!$type) { + unset($page[$key]); + continue; + } + + $credential->attachImplementation(clone $type); + } + return $page; } diff --git a/src/applications/passphrase/storage/PassphraseCredential.php b/src/applications/passphrase/storage/PassphraseCredential.php index 8384f5bd52..88bc4595bd 100644 --- a/src/applications/passphrase/storage/PassphraseCredential.php +++ b/src/applications/passphrase/storage/PassphraseCredential.php @@ -25,6 +25,7 @@ final class PassphraseCredential extends PassphraseDAO protected $spacePHID; private $secret = self::ATTACHABLE; + private $implementation = self::ATTACHABLE; public static function initializeNewCredential(PhabricatorUser $actor) { $app = id(new PhabricatorApplicationQuery()) @@ -98,6 +99,15 @@ final class PassphraseCredential extends PassphraseDAO return PassphraseCredentialType::getTypeByConstant($type); } + public function attachImplementation(PassphraseCredentialType $impl) { + $this->implementation = $impl; + return $this; + } + + public function getImplementation() { + return $this->assertAttached($this->implementation); + } + /* -( PhabricatorApplicationTransactionInterface )------------------------- */