Changeset View
Changeset View
Standalone View
Standalone View
src/applications/auth/factor/PhabricatorAuthFactorTOTP.php
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | $form->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
->setLabel(pht('TOTP Code')) | ->setLabel(pht('TOTP Code')) | ||||
->setName('totpcode') | ->setName('totpcode') | ||||
->setValue($code) | ->setValue($code) | ||||
->setError($e_code)); | ->setError($e_code)); | ||||
} | } | ||||
public function renderValidateFactorForm( | |||||
PhabricatorAuthFactorConfig $config, | |||||
AphrontFormView $form, | |||||
PhabricatorUser $viewer, | |||||
$validation_result) { | |||||
if (!$validation_result) { | |||||
$validation_result = array(); | |||||
} | |||||
$form->appendChild( | |||||
id(new AphrontFormTextControl()) | |||||
->setName($this->getParameterName($config, 'totpcode')) | |||||
->setLabel(pht('App Code')) | |||||
->setCaption(pht('Factor Name: %s', $config->getFactorName())) | |||||
->setValue(idx($validation_result, 'value')) | |||||
->setError(idx($validation_result, 'error', true))); | |||||
} | |||||
public function processValidateFactorForm( | |||||
PhabricatorAuthFactorConfig $config, | |||||
PhabricatorUser $viewer, | |||||
AphrontRequest $request) { | |||||
$code = $request->getStr($this->getParameterName($config, 'totpcode')); | |||||
$key = new PhutilOpaqueEnvelope($config->getFactorSecret()); | |||||
if (self::verifyTOTPCode($viewer, $key, $code)) { | |||||
return array( | |||||
'error' => null, | |||||
'value' => $code, | |||||
'valid' => true, | |||||
); | |||||
} else { | |||||
return array( | |||||
'error' => strlen($code) ? pht('Invalid') : pht('Required'), | |||||
'value' => $code, | |||||
'valid' => false, | |||||
); | |||||
} | |||||
} | |||||
public static function generateNewTOTPKey() { | public static function generateNewTOTPKey() { | ||||
return strtoupper(Filesystem::readRandomCharacters(16)); | return strtoupper(Filesystem::readRandomCharacters(16)); | ||||
} | } | ||||
public static function verifyTOTPCode( | public static function verifyTOTPCode( | ||||
PhabricatorUser $user, | PhabricatorUser $user, | ||||
PhutilOpaqueEnvelope $key, | PhutilOpaqueEnvelope $key, | ||||
$code) { | $code) { | ||||
▲ Show 20 Lines • Show All 72 Lines • Show Last 20 Lines |