Skip to content

Commit

Permalink
Initial refactoring step
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiang committed Jan 10, 2025
1 parent 735bdf1 commit d53608f
Show file tree
Hide file tree
Showing 37 changed files with 350 additions and 487 deletions.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
"autoload": {
"psr-4": {
"Fabiang\\Sasl\\": "src/"
},
"files": ["src/throwable.php"]
}
},
"autoload-dev": {
"psr-4": {
"Fabiang\\Sasl\\Behat\\": "tests/features/bootstrap",
"Fabiang\\Sasl\\": "tests/src"
},
"files": ["tests/compat.php"]
}
},
"require": {
"php": "^5.3.3 || ^7.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0"
Expand Down
29 changes: 8 additions & 21 deletions src/Authentication/AbstractAuthentication.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -48,33 +50,23 @@ abstract class AbstractAuthentication
{
/**
* Use random devices.
*
* @var bool
*/
public static $useDevRandom = true;
public static bool $useDevRandom = true;

/**
* Options object.
*
* @var Options
*/
protected $options;
protected Options $options;

/**
*
* @param Options $options
*/
public function __construct(Options $options)
{
$this->options = $options;
}

/**
* Get options object.
*
* @return Options
*/
public function getOptions()
public function getOptions(): Options
{
return $this->options;
}
Expand All @@ -84,7 +76,7 @@ public function getOptions()
*
* @return string The cnonce value
*/
protected function generateCnonce()
protected function generateCnonce(): string
{
foreach (array('/dev/urandom', '/dev/random') as $file) {
if (true === static::$useDevRandom && is_readable($file)) {
Expand All @@ -102,10 +94,8 @@ protected function generateCnonce()

/**
* Generate downgrade protection string
*
* @return string
*/
protected function generateDowngradeProtectionVerification()
protected function generateDowngradeProtectionVerification(): string
{
$downgradeProtectionOptions = $this->options->getDowngradeProtection();

Expand All @@ -127,12 +117,9 @@ protected function generateDowngradeProtectionVerification()
}

/**
* @param string $a
* @param string $b
* @return int
* @link https://datatracker.ietf.org/doc/html/rfc4790#page-22
*/
private function sortOctetCollation($a, $b)
private function sortOctetCollation(string $a, string $b): int
{
if ($a == $b) {
return 0;
Expand Down
6 changes: 4 additions & 2 deletions src/Authentication/Anonymous.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -55,10 +57,10 @@ class Anonymous extends AbstractAuthentication implements AuthenticationInterfac
*
* We could have some logic here for the second option, but this
* would by no means create something interpretable.
* @param string $challenge
*
* @return string The unaltered input token
*/
public function createResponse($challenge = null)
public function createResponse(?string $challenge = null): string|false
{
return $this->options->getAuthcid();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Authentication/AuthenticationInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -48,7 +50,6 @@ interface AuthenticationInterface
* Create response.
*
* @param string $challenge Response challenge. Not every authentication method requires this value.
* @return string|false
*/
public function createResponse($challenge = null);
public function createResponse(?string $challenge = null): string|false;
}
2 changes: 2 additions & 0 deletions src/Authentication/ChallengeAuthenticationInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Authentication/CramMD5.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -57,7 +59,7 @@ class CramMD5 extends AbstractAuthentication implements ChallengeAuthenticationI
* @return string The string to pass back to the server, of the form
* "<user> <digest>". This is NOT base64_encoded.
*/
public function createResponse($challenge = null)
public function createResponse(?string $challenge = null): string|false
{
return $this->options->getAuthcid() . ' ' . hash_hmac('md5', $challenge, $this->options->getSecret());
}
Expand Down
33 changes: 16 additions & 17 deletions src/Authentication/DigestMD5.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -55,7 +57,7 @@ class DigestMD5 extends AbstractAuthentication implements ChallengeAuthenticatio
* @param string $challenge The digest challenge sent by the server
* @return string The digest response (NOT base64 encoded)
*/
public function createResponse($challenge = null)
public function createResponse(?string $challenge = null): string|false
{
$parsedChallenge = $this->parseChallenge($challenge);
$authzidString = '';
Expand Down Expand Up @@ -110,9 +112,8 @@ public function createResponse($challenge = null)
* @param string $challenge The digest challenge
* @return array The parsed challenge as an assoc
* array in the form "directive => value".
* @access private
*/
private function parseChallenge($challenge)
private function parseChallenge(string $challenge): array
{
/**
* Defaults and required directives
Expand Down Expand Up @@ -144,12 +145,8 @@ private function parseChallenge($challenge)

/**
* Check found token.
*
* @param array $tokens
* @param string $key
* @param string $value
*/
private function checkToken(array &$tokens, $key, $value)
private function checkToken(array &$tokens, string $key, string $value): void
{
// Ignore these as per rfc2831
if ($key !== 'opaque' && $key !== 'domain') {
Expand All @@ -166,20 +163,15 @@ private function checkToken(array &$tokens, $key, $value)

// Any other multiple instance = failure
} else {
return array();
return;
}
} else {
$tokens[$key] = $this->trim($value);
}
}
}

/**
*
* @param string $string
* @return string
*/
private function trim($string)
private function trim(string $string): string
{
return trim($string, '"');
}
Expand All @@ -197,8 +189,15 @@ private function trim($string)
* @return string The response= part of the digest response
* @access private
*/
private function getResponseValue($authcid, $pass, $realm, $nonce, $cnonce, $digest_uri, $authzid = '')
{
private function getResponseValue(
string $authcid,
string $pass,
string $realm,
string $nonce,
string $cnonce,
string $digest_uri,
string $authzid = ''
): string {
if ($authzid == '') {
$A1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce);
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/Authentication/External.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -47,10 +49,9 @@ class External extends AbstractAuthentication implements AuthenticationInterface
/**
* Returns EXTERNAL response
*
* @param string $challenge
* @return string EXTERNAL Response
*/
public function createResponse($challenge = null)
public function createResponse(?string $challenge = null): string|false
{
return $this->options->getAuthcid();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Authentication/Login.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -52,7 +54,7 @@ class Login extends AbstractAuthentication implements AuthenticationInterface
*
* @return string LOGIN string
*/
public function createResponse($challenge = null)
public function createResponse(?string $challenge = null): string|false
{
return sprintf('LOGIN %s %s', $this->options->getAuthcid(), $this->options->getSecret());
}
Expand Down
4 changes: 3 additions & 1 deletion src/Authentication/Plain.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Sasl library.
*
Expand Down Expand Up @@ -49,7 +51,7 @@ class Plain extends AbstractAuthentication implements AuthenticationInterface
* @return string PLAIN Response
*/
public function createResponse($challenge = null)
public function createResponse(?string $challenge = null): string|false
{
return $this->options->getAuthzid() . chr(0)
. $this->options->getAuthcid() . chr(0) . $this->options->getSecret();
Expand Down
Loading

0 comments on commit d53608f

Please sign in to comment.