Path: blob/master/src/applications/conduit/method/ConduitGetCertificateConduitAPIMethod.php
12256 views
<?php12final class ConduitGetCertificateConduitAPIMethod extends ConduitAPIMethod {34public function getAPIMethodName() {5return 'conduit.getcertificate';6}78public function shouldRequireAuthentication() {9return false;10}1112public function shouldAllowUnguardedWrites() {13// This method performs logging and is on the authentication pathway.14return true;15}1617public function getMethodDescription() {18return pht('Retrieve certificate information for a user.');19}2021protected function defineParamTypes() {22return array(23'token' => 'required string',24'host' => 'required string',25);26}2728protected function defineReturnType() {29return 'dict<string, any>';30}3132protected function defineErrorTypes() {33return array(34'ERR-BAD-TOKEN' => pht('Token does not exist or has expired.'),35'ERR-RATE-LIMIT' => pht(36'You have made too many invalid token requests recently. Wait before '.37'making more.'),38);39}4041protected function execute(ConduitAPIRequest $request) {42$failed_attempts = PhabricatorUserLog::loadRecentEventsFromThisIP(43PhabricatorConduitCertificateFailureUserLogType::LOGTYPE,4460 * 5);4546if (count($failed_attempts) > 5) {47$this->logFailure($request);48throw new ConduitException('ERR-RATE-LIMIT');49}5051$token = $request->getValue('token');52$info = id(new PhabricatorConduitCertificateToken())->loadOneWhere(53'token = %s',54trim($token));5556if (!$info || $info->getDateCreated() < time() - (60 * 15)) {57$this->logFailure($request, $info);58throw new ConduitException('ERR-BAD-TOKEN');59} else {60$log = PhabricatorUserLog::initializeNewLog(61$request->getUser(),62$info->getUserPHID(),63PhabricatorConduitCertificateUserLogType::LOGTYPE)64->save();65}6667$user = id(new PhabricatorUser())->loadOneWhere(68'phid = %s',69$info->getUserPHID());70if (!$user) {71throw new Exception(pht('Certificate token points to an invalid user!'));72}7374return array(75'username' => $user->getUserName(),76'certificate' => $user->getConduitCertificate(),77);78}7980private function logFailure(81ConduitAPIRequest $request,82PhabricatorConduitCertificateToken $info = null) {8384$log = PhabricatorUserLog::initializeNewLog(85$request->getUser(),86$info ? $info->getUserPHID() : '-',87PhabricatorConduitCertificateFailureUserLogType::LOGTYPE)88->save();89}9091}929394