PHP Classes

File: src/Commands/Login.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Airship barge   src/Commands/Login.php   Download  
File: src/Commands/Login.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Airship barge
Build extensions for the Airship CMS
Author: By
Last change:
Date: 5 years ago
Size: 2,377 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
Airship\Barge\Commands;

use
Airship\Barge as Base;

/**
 * Class Login
 *
 * Log in, get stored public keys and salts.
 *
 * @package Airship\Barge\Commands
 */
class Login extends Base\Command
{
    public
$essential = false;
    public
$name = 'Login';
    public
$description = 'Authenticate to the Skyport.';
    public
$display = 2;
   
   
/**
     * Execute the login command
     *
     * @param array $args - CLI arguments
     * @echo
     * @return null
     */
   
public function fire(array $args = [])
    {
       
$username = \count($args) > 0
           
? $args[0]
            :
$this->prompt("Please enter your username: ");
       
       
$password = $this->silentPrompt("Password:");
       
        list (
$skyport, $publicKey) = $this->getSkyport();

       
// This should be sent over HTTPS only.
       
$result = Base\HTTP::postSignedJSON(
           
$skyport.'login',
           
$publicKey,
            [
               
'name' => $username,
               
'password' => $password
           
]
        );

       
// Wipe from memory as soon as we're done using it.
       
\Sodium\memzero($password);

        if (isset(
$result['error'])) {
            echo
$result['error'], "\n";
            exit(
255);
        }

       
// Let's store the result in our local config
       
if (!\array_key_exists('suppliers', $this->config)) {
           
$this->config['suppliers'] = [];
        }
        if (\
array_key_exists($username, $this->config['suppliers'])) {
           
$this->config['suppliers'][$username]['token'] = $result['token'];
            foreach (
$result['signing_keys'] as $res_key) {
               
$found = false;
                foreach (
$this->config['suppliers'][$username]['signing_keys'] as $key) {
                    if (
$key['public_key'] === $res_key['public_key']) {
                       
$found = true;
                        break;
                    }
                }
               
// If we loaded our salt into the skyport, import it:
               
if (!$found && isset($res_key['salt'])) {
                   
$this->config['suppliers'][$username]['signing_keys'][] = $res_key;
                }
            }
            echo
'Authentication successful', "\n";
        } else {
           
$this->config['suppliers'][$username] = $result;
        }
    }
}