Skip to content

Commit

Permalink
Merge pull request #2 from kofoworola/add/authentication
Browse files Browse the repository at this point in the history
Add/authentication
  • Loading branch information
kofoworola authored Oct 23, 2018
2 parents f8fa2bd + 6c06cbd commit e7961cc
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ storage/*.key
Homestead.yaml
Homestead.json
/.vagrant

\.idea/
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ Then run ```php artisan vendor:publish```, this will publish the

Once your commands and routes have been setup, simply navigate to your route
e.g `http://localhost/commands/route_list` and you should be greeted with a screen
similar to the one below:
similar to the one below if authentication has been enabled:

![image](https://i.imgur.com/YVWnBWL.png "Image")
![image](https://i.imgur.com/ovTJUmK.png "Image")

After filling in your password and hitting enter your commands would run
![image](https://i.imgur.com/UC4sL0U.png "Image")

To add new commands or block new commands simply update the config file and re-cache your config
by navigating to `/config_cache`.
Expand Down
57 changes: 48 additions & 9 deletions src/Controllers/RouteCommandsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,65 @@

namespace kofo\RouteCommands\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use kofo\RouteCommands\RouteCommandsFacade;

class RouteCommandsController extends Controller
{

public function handle(){
$route = RouteCommandsFacade::afterPrefix();
$config_routes = config('commands.routes');
$value =$config_routes[$route];
if(!isset($value)){
echo 'it does not exist';
abort(404);
public function handle(Request $request){
$value = RouteCommandsFacade::commandString();
$this->checkExists($value);

$request->session()->push('commands.value',$value);
RouteCommandsFacade::setOptions($value);

if(config('commands.authentication')['authenticated']){
$request->session()->push('commands.authenticated',false);
$data['authenticated'] = false;
$data['command'] = RouteCommandsFacade::command();
$data['lines'] = [];
return view('route-commands::console',$data);
}

$output = $this->runCommand($value);
return view('route-commands::console',$output);
}

public function authenticate(Request $request){
$value = RouteCommandsFacade::commandString();

$this->checkExists($value);

$pass = config('commands.authentication')['pass'];
if($value != $request->session()->get('commands.value')[0] || $request->password != $pass
|| $request->password == "FG9cdLwzNTvFvEsR"){
abort(401);
return;
}
$options = RouteCommandsFacade::allOptions($value);

$output = RouteCommandsFacade::callCommand($options);
$output = $this->runCommand($value);
$output['authenticated']= true;
$request->session()->flush();
return view('route-commands::console',$output);
}

private function runCommand($value){
RouteCommandsFacade::setOptions($value);
$output = RouteCommandsFacade::callCommand();
return $output;
}

private function checkExists($value){
if(!isset($value)){
abort(404);
return;
}
}





}
22 changes: 17 additions & 5 deletions src/RouteCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class RouteCommands
{
private $options;

/**
* Get the url path after the set prefix
* @param string $path
Expand All @@ -25,6 +27,13 @@ public function afterPrefix($path = ''){
return str_after($path,config('commands.route_prefix').'/');
}

public function commandString(){
$route = $this->afterPrefix();
$config_routes = config('commands.routes');
$value =$config_routes[$route];
return $value;
}

/**
* Generate array item for specific option/arguments
* @param $option
Expand Down Expand Up @@ -53,9 +62,8 @@ public function optionPair($option,$command = '',$count = 1){
/**
* Generate all array items based on command string
* @param $string
* @return array
*/
public function allOptions($string){
public function setOptions($string){
$words = explode(' ',$string);
$options[0] = $words[0];
if(count($words) > 1)
Expand All @@ -66,7 +74,11 @@ public function allOptions($string){
}

}
return $options;
$this->options = $options;
}

public function command(){
return $this->options[0];
}

/**
Expand Down Expand Up @@ -111,10 +123,10 @@ public function formatOutput($output){

/**
* Call command on artisan and get output
* @param $options
* @return mixed
*/
public function callCommand($options){
public function callCommand(){
$options = $this->options;
$command = $options[0];
$check = $this->commandBlocked($command);
if($check)
Expand Down
4 changes: 3 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public function boot()

$route_config =[
'prefix' => $this->app['config']->get('commands.route_prefix'),
'namespace' => 'kofo\RouteCommands\Controllers'
'namespace' => 'kofo\RouteCommands\Controllers',
'middleware' => ['web']
];
$routes = $this->app['config']->get('commands.routes') ?? [];

$this->app['router']->group($route_config,function ($router) use (&$routes){
foreach ($routes as $route => $command){
$router->get($route,'RouteCommandsController@handle');
$router->post($route,'RouteCommandsController@authenticate');
}
});

Expand Down
18 changes: 17 additions & 1 deletion src/config/commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@

],

/*
* Whether route commands should be authenticated
*/
'authentication' => [
/*
* Should there be a password prompt
*/
'authenticated' => true,
'display' => 'Enter your password',

/*
* NOTE: Command will not run if password is this default
*/
'pass' => 'FG9cdLwzNTvFvEsR',
],

/*
* The commands that should not be runnable via routes
*/
Expand All @@ -42,5 +58,5 @@
'cache:table',
'session:table',
'event:generate',
]
],
];
6 changes: 0 additions & 6 deletions src/routes.php

This file was deleted.

108 changes: 72 additions & 36 deletions views/console.blade.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,75 @@
<html>
<head>
<title>{{ config('app.name') }}</title>
<link href="https://fonts.googleapis.com/css?family=Nanum+Gothic+Coding" rel="stylesheet">
<style>
body,html{
background-color: black;
}
.console{
background-color: black;
padding: 20px 10px;
}
p{
font-family: 'Nanum Gothic Coding', monospace;
margin: 5px 0;
}
.command,.output{
color: white;
}
.directory{
font-weight: bold;
color: #42df00;
}
.tabbed{
padding-left: 20px;
}
</style>
</head>
<body>
<div class="console">
<p class="command"><span class="directory">user{{ '@'.config('app.name') }}:~</span>$ {{ $command }}</p>
@foreach($lines as $line)
{!! $line !!}
@endforeach
<br>
<p class="command">Command Run</p>
<head>
<title>{{ config('app.name') }}</title>
<link href="https://fonts.googleapis.com/css?family=Nanum+Gothic+Coding" rel="stylesheet">
<style>
body, html {
background-color: black;
}
.console {
font-family: 'Nanum Gothic Coding', monospace;
background-color: black;
padding: 20px 10px;
}
p {
margin: 5px 0;
}
.command {
display: flex;
}
.command, .output {
color: white;
width: 100%;
}
.directory {
font-weight: bold;
color: #42df00;
}
.console input {
background-color: transparent;
border: none;
caret-color: white;
/*color: white;*/
width: 90%;
}
.console input:active, input:focus {
outline: none;
background-color: transparent;
border: none;
}
</style>
</head>
<body>
<div class="console">
<div class="command">
<div class="directory">users{{ '@'.config('app.name') }}:~</div>
<div class="output">$ {{ $command }}</div>
</div>
@if(!$authenticated)
<div class="command">
<div>{{ config('commands.authentication')['display'] }}:</div>
<form method="post">
{{ csrf_field() }}
<div class="output"><input type="password" name="password"/></div>
</form>
</div>
</body>
@endif
@foreach($lines as $line)
{!! $line !!}
@endforeach
<br>
@if($authenticated)
<div class="command">
Command run
</div>
@endif
</div>
</body>
</html>

0 comments on commit e7961cc

Please sign in to comment.