Skip to content

Commit

Permalink
Merge pull request #250 from silinternational/feature/use-use
Browse files Browse the repository at this point in the history
Use more use statements
  • Loading branch information
briskt authored Jul 10, 2024
2 parents eeb1d92 + 34484e5 commit 43655a4
Show file tree
Hide file tree
Showing 30 changed files with 141 additions and 92 deletions.
3 changes: 1 addition & 2 deletions development/UserPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use SimpleSAML\Error;
use SimpleSAML\Logger;
use SimpleSAML\Module\core\Auth\UserPassBase;
use SimpleSAML\Utils;

/**
* Example authentication source - username & password.
Expand All @@ -23,7 +22,7 @@
*
* @package SimpleSAMLphp
*/
class UserPass extends \SimpleSAML\Module\core\Auth\UserPassBase // GTIS
class UserPass extends UserPassBase // GTIS
{
/**
* Our users, stored in an associative array. The key of the array is "<username>:<password>",
Expand Down
3 changes: 2 additions & 1 deletion dockerbuild/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

use Sil\PhpEnv\Env;
use Sil\PhpEnv\EnvVarNotFoundException;
use SimpleSAML\Utils;

$httpUtils = new \SimpleSAML\Utils\HTTP();
$httpUtils = new Utils\HTTP();

/*
* Get config settings from ENV vars or set defaults
Expand Down
2 changes: 1 addition & 1 deletion features/bootstrap/LoginContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function iShouldNotBeAllowedThrough()
function () use ($authenticator) {
$authenticator->getUserAttributes();
},
\Exception::class,
Exception::class,
'The call to getUserAttributes() should have thrown an exception.'
);
}
Expand Down
2 changes: 1 addition & 1 deletion features/bootstrap/SilDiscoContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function iLogInUsingMyIdpCredentials($idp)
break;

default:
throw new \Exception('credential name not recognized');
throw new Exception('credential name not recognized');
}
$this->iLogIn();
}
Expand Down
7 changes: 4 additions & 3 deletions modules/expirychecker/src/Auth/Process/ExpiryDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SimpleSAML\Module\expirychecker\Auth\Process;

use Exception;
use Psr\Log\LoggerInterface;
use Sil\Psr3Adapters\Psr3SamlLogger;
use SimpleSAML\Auth\ProcessingChain;
Expand Down Expand Up @@ -132,7 +133,7 @@ protected function getDaysLeftBeforeExpiry(int $expiryTimestamp): int
* expiration date (as a string) is stored.
* @param array $state The state data.
* @return int The expiration timestamp.
* @throws \Exception
* @throws Exception
*/
protected function getExpiryTimestamp(string $expiryDateAttr, array $state): int
{
Expand All @@ -141,7 +142,7 @@ protected function getExpiryTimestamp(string $expiryDateAttr, array $state): int
// Ensure that EVERY user login provides a usable password expiration date.
$expiryTimestamp = strtotime($expiryDateString) ?: null;
if (empty($expiryTimestamp)) {
throw new \Exception(sprintf(
throw new Exception(sprintf(
"We could not understand the expiration date (%s, from %s) for "
. "the user's password, so we do not know whether their "
. "password is still valid.",
Expand Down Expand Up @@ -178,7 +179,7 @@ protected function initLogger(array $config): void
$loggerClass = $config['loggerClass'] ?? Psr3SamlLogger::class;
$this->logger = new $loggerClass();
if (!$this->logger instanceof LoggerInterface) {
throw new \Exception(sprintf(
throw new Exception(sprintf(
'The specified loggerClass (%s) does not implement '
. '\\Psr\\Log\\LoggerInterface.',
var_export($loggerClass, true)
Expand Down
2 changes: 1 addition & 1 deletion modules/mfa/public/prompt-for-mfa.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

// This condition should never return
ProcessingChain::resumeProcessing($state);
throw new \Exception('Failed to resume processing auth proc chain.');
throw new Exception('Failed to resume processing auth proc chain.');
}

$mfaId = filter_input(INPUT_GET, 'mfaId');
Expand Down
38 changes: 20 additions & 18 deletions modules/mfa/src/Auth/Process/Mfa.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SimpleSAML\Module\mfa\Auth\Process;

use Exception;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Sil\Idp\IdBroker\Client\BaseClient;
use Sil\PhpEnv\Env;
Expand Down Expand Up @@ -52,7 +54,7 @@ class Mfa extends ProcessingFilter
*
* @param array $config Configuration information about this filter.
* @param mixed $reserved For future use.
* @throws \Exception
* @throws Exception
*/
public function __construct(array $config, mixed $reserved)
{
Expand Down Expand Up @@ -98,12 +100,12 @@ protected function loadValuesFromConfig(array $config, array $attributes): void
* @param string $attribute The name of the attribute.
* @param mixed $value The value to check.
* @param LoggerInterface $logger The logger.
* @throws \Exception
* @throws Exception
*/
public static function validateConfigValue(string $attribute, mixed $value, LoggerInterface $logger): void
{
if (empty($value) || !is_string($value)) {
$exception = new \Exception(sprintf(
$exception = new Exception(sprintf(
'The value we have for %s (%s) is empty or is not a string',
$attribute,
var_export($value, true)
Expand Down Expand Up @@ -184,13 +186,13 @@ protected static function getIdBrokerClient(array $idBrokerConfig): IdBrokerClie
* @param array[] $mfaOptions The available MFA options.
* @param int $mfaId The ID of the desired MFA option.
* @return array The MFA option to use.
* @throws \InvalidArgumentException
* @throws \Exception
* @throws InvalidArgumentException
* @throws Exception
*/
public static function getMfaOptionById(array $mfaOptions, int $mfaId): array
{
if (empty($mfaId)) {
throw new \Exception('No MFA ID was provided.');
throw new Exception('No MFA ID was provided.');
}

foreach ($mfaOptions as $mfaOption) {
Expand All @@ -199,7 +201,7 @@ public static function getMfaOptionById(array $mfaOptions, int $mfaId): array
}
}

throw new \Exception(
throw new Exception(
'No MFA option has an ID of ' . var_export($mfaId, true)
);
}
Expand All @@ -211,13 +213,13 @@ public static function getMfaOptionById(array $mfaOptions, int $mfaId): array
* @param string $userAgent The User-Agent sent by the user's browser, used
* for detecting WebAuthn support.
* @return array The MFA option to use.
* @throws \InvalidArgumentException
* @throws \Exception
* @throws InvalidArgumentException
* @throws Exception
*/
public static function getMfaOptionToUse(array $mfaOptions, string $userAgent): array
{
if (empty($mfaOptions)) {
throw new \Exception('No MFA options were provided.');
throw new Exception('No MFA options were provided.');
}

$recentMfa = self::getMostRecentUsedMfaOption($mfaOptions);
Expand Down Expand Up @@ -292,7 +294,7 @@ public static function getNumBackupCodesUserHad(array $mfaOptions): int
*
* @param string $mfaType The desired MFA type, such as 'webauthn', 'totp', or 'backupcode'.
* @return string
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public static function getTemplateFor(string $mfaType): string
{
Expand All @@ -305,7 +307,7 @@ public static function getTemplateFor(string $mfaType): string
$template = $mfaOptionTemplates[$mfaType] ?? null;

if ($template === null) {
throw new \InvalidArgumentException(sprintf(
throw new InvalidArgumentException(sprintf(
'No %s MFA template is available.',
var_export($mfaType, true)
), 1507219338);
Expand Down Expand Up @@ -515,14 +517,14 @@ public static function validateMfaSubmission(

if ($numBackupCodesRemaining <= 0) {
self::redirectToOutOfBackupCodesMessage($state, $employeeId);
throw new \Exception('Failed to send user to out-of-backup-codes page.');
throw new Exception('Failed to send user to out-of-backup-codes page.');
} elseif ($numBackupCodesRemaining < 4) {
self::redirectToLowOnBackupCodesNag(
$state,
$employeeId,
$numBackupCodesRemaining
);
throw new \Exception('Failed to send user to low-on-backup-codes page.');
throw new Exception('Failed to send user to low-on-backup-codes page.');
}
}

Expand All @@ -537,7 +539,7 @@ public static function validateMfaSubmission(

// The following function call will never return.
ProcessingChain::resumeProcessing($state);
throw new \Exception('Failed to resume processing auth proc chain.');
throw new Exception('Failed to resume processing auth proc chain.');
}

/**
Expand Down Expand Up @@ -647,7 +649,7 @@ protected function redirectToMfaNeededMessage(array &$state, string $employeeId,
* @param array $state The state data.
* @param string $employeeId The Employee ID of the user account.
* @param array $mfaOptions Array of MFA options
* @throws \Exception
* @throws Exception
*/
protected function redirectToMfaPrompt(array &$state, string $employeeId, array $mfaOptions): void
{
Expand Down Expand Up @@ -884,7 +886,7 @@ public static function getManagerEmail(array $state): ?string
*
* @param array[] $mfaOptions The available MFA options.
* @return array The manager MFA.
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public static function getManagerMfa(array $mfaOptions): ?array
{
Expand Down Expand Up @@ -958,7 +960,7 @@ protected static function updateStateWithNewMfaData(array &$state, LoggerInterfa

try {
$newMfaOptions = $idBrokerClient->mfaList($state['employeeId']);
} catch (\Exception $e) {
} catch (Exception $e) {
$log['status'] = 'failed: id-broker exception';
$logger->error(json_encode($log));
return;
Expand Down
9 changes: 5 additions & 4 deletions modules/profilereview/src/Auth/Process/ProfileReview.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SimpleSAML\Module\profilereview\Auth\Process;

use Exception;
use Psr\Log\LoggerInterface;
use Sil\Psr3Adapters\Psr3SamlLogger;
use SimpleSAML\Auth\ProcessingFilter;
Expand Down Expand Up @@ -38,7 +39,7 @@ class ProfileReview extends ProcessingFilter
*
* @param array $config Configuration information about this filter.
* @param mixed $reserved For future use.
* @throws \Exception
* @throws Exception
*/
public function __construct(array $config, mixed $reserved)
{
Expand All @@ -61,7 +62,7 @@ public function __construct(array $config, mixed $reserved)
/**
* @param $config
* @param $attributes
* @throws \Exception
* @throws Exception
*/
protected function loadValuesFromConfig(array $config, array $attributes): void
{
Expand All @@ -82,12 +83,12 @@ protected function loadValuesFromConfig(array $config, array $attributes): void
* @param string $attribute The name of the attribute.
* @param mixed $value The value to check.
* @param LoggerInterface $logger The logger.
* @throws \Exception
* @throws Exception
*/
public static function validateConfigValue($attribute, $value, $logger)
{
if (empty($value) || !is_string($value)) {
$exception = new \Exception(sprintf(
$exception = new Exception(sprintf(
'The value we have for %s (%s) is empty or is not a string',
$attribute,
var_export($value, true)
Expand Down
2 changes: 1 addition & 1 deletion modules/silauth/public/status.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
$t->getMessage(),
$t->getCode()
);
\http_response_code(500);
http_response_code(500);
}
7 changes: 4 additions & 3 deletions modules/silauth/src/Auth/Source/auth/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SimpleSAML\Module\silauth\Auth\Source\auth;

use Exception;
use Psr\Log\LoggerInterface;
use SimpleSAML\Module\silauth\Auth\Source\auth\AuthError;
use SimpleSAML\Module\silauth\Auth\Source\auth\IdBroker;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function __construct(
$username,
$password
);
} catch (\Exception $e) {
} catch (Exception $e) {
$logger->critical(json_encode([
'event' => 'Problem communicating with ID Broker',
'errorCode' => $e->getCode(),
Expand Down Expand Up @@ -211,12 +212,12 @@ public static function getSecondsUntilUnblocked(
* // ...
* ]
* </pre>
* @throws \Exception
* @throws Exception
*/
public function getUserAttributes(): array
{
if ($this->userAttributes === null) {
throw new \Exception(
throw new Exception(
"You cannot get the user's attributes until you have authenticated the user.",
1482270373
);
Expand Down
6 changes: 4 additions & 2 deletions modules/silauth/src/Auth/Source/captcha/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SimpleSAML\Module\silauth\Auth\Source\captcha;

use ReCaptcha\ReCaptcha;
use RuntimeException;
use SimpleSAML\Module\silauth\Auth\Source\http\Request;

class Captcha
Expand All @@ -16,13 +18,13 @@ public function __construct(?string $secret = null)
public function isValidIn(Request $request): bool
{
if (empty($this->secret)) {
throw new \RuntimeException('No captcha secret available.', 1487342411);
throw new RuntimeException('No captcha secret available.', 1487342411);
}

$captchaResponse = $request->getCaptchaResponse();
$ipAddress = $request->getMostLikelyIpAddress();

$recaptcha = new \ReCaptcha\ReCaptcha($this->secret);
$recaptcha = new ReCaptcha($this->secret);
$rcResponse = $recaptcha->verify($captchaResponse, $ipAddress);

return $rcResponse->isSuccess();
Expand Down
9 changes: 5 additions & 4 deletions modules/silauth/src/Auth/Source/config/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace SimpleSAML\Module\silauth\Auth\Source\config;

use SimpleSAML\Module\silauth\Auth\Source\text\Text;
use yii\console\Application;
use yii\console\Application as ConsoleApplication;
use yii\web\Application as WebApplication;

class ConfigManager
{
Expand Down Expand Up @@ -78,11 +79,11 @@ private static function initializeYiiClass(): void
}
}

public static function getYii2ConsoleApp(array $customConfig): Application
public static function getYii2ConsoleApp(array $customConfig): ConsoleApplication
{
self::initializeYiiClass();
$mergedYii2Config = self::getMergedYii2Config($customConfig);
return new Application($mergedYii2Config);
return new ConsoleApplication($mergedYii2Config);
}

public static function initializeYii2WebApp(array $customConfig = []): void
Expand All @@ -93,7 +94,7 @@ public static function initializeYii2WebApp(array $customConfig = []): void
* here, since we don't want Yii to handle the HTTP request. We just
* want the Yii classes available for use (including database
* models). */
$app = new \yii\web\Application(self::getMergedYii2Config($customConfig));
$app = new WebApplication(self::getMergedYii2Config($customConfig));

/*
* Initialize the Yii logger. It doesn't want to initialize itself for some reason.
Expand Down
3 changes: 2 additions & 1 deletion modules/silauth/src/Auth/Source/http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SimpleSAML\Module\silauth\Auth\Source\http;

use InvalidArgumentException;
use IP;
use IPBlock;
use SimpleSAML\Module\silauth\Auth\Source\text\Text;
Expand Down Expand Up @@ -197,7 +198,7 @@ public static function sanitizeInputString(int $inputType, string $variableName)
public function trustIpAddress(string $ipAddress): void
{
if (!self::isValidIpAddress($ipAddress)) {
throw new \InvalidArgumentException(sprintf(
throw new InvalidArgumentException(sprintf(
'%s is not a valid IP address.',
var_export($ipAddress, true)
));
Expand Down
Loading

0 comments on commit 43655a4

Please sign in to comment.