Touch-up & Implemented authentication system

This commit is contained in:
Netkas 2023-09-28 21:56:15 -04:00
parent 09c89c16a0
commit 9cfca3281a
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
5 changed files with 55 additions and 19 deletions

View file

@ -128,8 +128,7 @@
while(true) while(true)
{ {
$password = Console::passwordInput('Password/Secret: '); if (!$entry->unlock(Console::passwordInput('Password/Secret: ')))
if (!$entry->unlock($password))
{ {
$tries++; $tries++;
if ($tries >= 3) if ($tries >= 3)
@ -138,7 +137,7 @@
return 1; return 1;
} }
Console::outError('Invalid password.', true, 1); Console::outError(sprintf('Invalid password, %d attempts remaining.', 3 - $tries));
} }
else else
{ {
@ -173,7 +172,7 @@
Console::out('Entries:'); Console::out('Entries:');
foreach($entries as $entry) foreach($entries as $entry)
{ {
Console::out(sprintf(' - %s (%s)', $entry->getName(), $entry->isEncrypted() ? ' (encrypted)' : '')); Console::out(sprintf(' - %s %s', $entry->getName(), $entry->isEncrypted() ? ' (encrypted)' : ''));
} }
Console::out('Total: ' . count($entries)); Console::out('Total: ' . count($entries));

View file

@ -30,6 +30,7 @@
use ncc\Exceptions\IOException; use ncc\Exceptions\IOException;
use ncc\Exceptions\OperationException; use ncc\Exceptions\OperationException;
use ncc\Exceptions\PathNotFoundException; use ncc\Exceptions\PathNotFoundException;
use ncc\Managers\CredentialManager;
use ncc\Managers\PackageManager; use ncc\Managers\PackageManager;
use ncc\Managers\RepositoryManager; use ncc\Managers\RepositoryManager;
use ncc\Objects\CliHelpSection; use ncc\Objects\CliHelpSection;
@ -116,20 +117,55 @@
} }
/** /**
* Installs a package from a local file or from a remote repository
*
* @param array $args * @param array $args
* @return int * @return int
* @throws ConfigurationException * @throws ConfigurationException
* @throws IOException * @throws IOException
* @throws OperationException * @throws OperationException
* @throws PathNotFoundException * @throws PathNotFoundException
* @throws Exception
*/ */
private static function installPackage(array $args): int private static function installPackage(array $args): int
{ {
$package = $args['package'] ?? $args['p'] ?? null; $package = $args['package'] ?? $args['p'] ?? null;
$authentication = $args['authentication'] ?? $args['a'] ?? null;
$authentication_entry = null;
$auto_yes = isset($args['y']); $auto_yes = isset($args['y']);
$repository_manager = new RepositoryManager(); $repository_manager = new RepositoryManager();
$package_manager = new PackageManager(); $package_manager = new PackageManager();
if($authentication !== null)
{
$entry = (new CredentialManager())->getVault()?->getEntry($authentication);
if($entry->isEncrypted())
{
$tries = 0;
while(true)
{
if (!$entry->unlock(Console::passwordInput('Password/Secret: ')))
{
$tries++;
if ($tries >= 3)
{
Console::outError('Too many failed attempts.', true, 1);
return 1;
}
Console::outError(sprintf('Incorrect password/secret, %d attempts remaining', 3 - $tries));
}
else
{
Console::out('Authentication successful.');
return 1;
}
}
}
$authentication_entry = $entry->getPassword();
}
if(preg_match(RegexPatterns::REMOTE_PACKAGE, $package) === 1) if(preg_match(RegexPatterns::REMOTE_PACKAGE, $package) === 1)
{ {
@ -147,7 +183,7 @@
return 0; return 0;
} }
$results = $package_manager->install($package_input); $results = $package_manager->install($package_input, $authentication_entry);
Console::out(sprintf('Installed %d packages', count($results))); Console::out(sprintf('Installed %d packages', count($results)));
return 0; return 0;
} }
@ -252,7 +288,7 @@
return 0; return 0;
} }
$package_manager->install($package_reader); Console::out(sprintf('Installed %d packages', count($package_manager->install($package_reader, $authentication_entry))));
return 0; return 0;
} }
@ -415,7 +451,7 @@
} }
Console::out(sprintf('Fixing missing dependency %s', $package)); Console::out(sprintf('Fixing missing dependency %s', $package));
$package_manager->install($source); Console::out(sprintf('Installed %d packages', count($package_manager->install($source))));
} }
return 0; return 0;

View file

@ -170,7 +170,7 @@
* *
* @param string $package_name * @param string $package_name
* @param string|null $version * @param string|null $version
* @return void * @return array
* @throws IOException * @throws IOException
* @throws OperationException * @throws OperationException
*/ */
@ -481,6 +481,7 @@
* @return void * @return void
* @throws ConfigurationException * @throws ConfigurationException
* @throws IOException * @throws IOException
* @throws OperationException
*/ */
private function extractPackageContents(PackageReader $package_reader, string $package_path): void private function extractPackageContents(PackageReader $package_reader, string $package_path): void
{ {

View file

@ -24,6 +24,7 @@
namespace ncc\Objects; namespace ncc\Objects;
use InvalidArgumentException;
use ncc\Enums\Types\AuthenticationType; use ncc\Enums\Types\AuthenticationType;
use ncc\Enums\Versions; use ncc\Enums\Versions;
use ncc\Interfaces\AuthenticationInterface; use ncc\Interfaces\AuthenticationInterface;
@ -142,9 +143,9 @@
* Returns an existing entry from the vault * Returns an existing entry from the vault
* *
* @param string $name * @param string $name
* @return Entry|null * @return Entry
*/ */
public function getEntry(string $name): ?Entry public function getEntry(string $name): Entry
{ {
foreach($this->entries as $entry) foreach($this->entries as $entry)
{ {
@ -154,7 +155,7 @@
} }
} }
return null; throw new InvalidArgumentException(sprintf('Entry "%s" does not exist in the vault', $name));
} }
/** /**
@ -167,10 +168,6 @@
public function authenticate(string $name, string $password): bool public function authenticate(string $name, string $password): bool
{ {
$entry = $this->getEntry($name); $entry = $this->getEntry($name);
if($entry === null)
{
return false;
}
if(($entry->getPassword() === null) && $entry->isEncrypted() && !$entry->isCurrentlyDecrypted()) if(($entry->getPassword() === null) && $entry->isEncrypted() && !$entry->isCurrentlyDecrypted())
{ {

View file

@ -24,10 +24,12 @@
namespace ncc\Objects\Vault; namespace ncc\Objects\Vault;
use Exception;
use ncc\Defuse\Crypto\Crypto; use ncc\Defuse\Crypto\Crypto;
use ncc\Defuse\Crypto\Exception\EnvironmentIsBrokenException; use ncc\Defuse\Crypto\Exception\EnvironmentIsBrokenException;
use ncc\Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException; use ncc\Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException;
use ncc\Enums\Types\AuthenticationType; use ncc\Enums\Types\AuthenticationType;
use ncc\Exceptions\ConfigurationException;
use ncc\Extensions\ZiProto\ZiProto; use ncc\Extensions\ZiProto\ZiProto;
use ncc\Interfaces\AuthenticationInterface; use ncc\Interfaces\AuthenticationInterface;
use ncc\Interfaces\BytecodeObjectInterface; use ncc\Interfaces\BytecodeObjectInterface;
@ -202,7 +204,7 @@
* *
* @param string $password * @param string $password
* @return bool * @return bool
* @noinspection PhpUnhandledExceptionInspection * @throws Exception
*/ */
public function unlock(string $password): bool public function unlock(string $password): bool
{ {
@ -327,13 +329,13 @@
} }
/** /**
* @return AuthenticationInterface|null * @return AuthenticationInterface
*/ */
public function getPassword(): ?AuthenticationInterface public function getPassword(): AuthenticationInterface
{ {
if(!$this->currently_decrypted) if(!$this->currently_decrypted)
{ {
return null; throw new RuntimeException(sprintf('Cannot get password for entry "%s" because it is currently encrypted', $this->name));
} }
return $this->password; return $this->password;
@ -379,6 +381,7 @@
* *
* @param array $data * @param array $data
* @return Entry * @return Entry
* @throws ConfigurationException
*/ */
public static function fromArray(array $data): self public static function fromArray(array $data): self
{ {