Skip to content

Commit

Permalink
Code cleanup and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
olssonm committed Jan 22, 2018
1 parent 7a08e35 commit ede0f0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/Http/Middleware/VeryBasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,22 @@ class VeryBasicAuth
public function handle($request, Closure $next)
{
// Load configuration
$veryBasicAuthUser = config('very_basic_auth.user');
$veryBasicAuthPass = config('very_basic_auth.password');
$veryBasicAuthEnvs = config('very_basic_auth.envs');
$veryBasicAuthMsg = config('very_basic_auth.error_message');
$veryBasicAuthView = config('very_basic_auth.error_view');
$config = config('very_basic_auth');

// Check if middleware is in use in current environment
if(in_array(app()->environment(), $veryBasicAuthEnvs)) {
if($request->getUser() != $veryBasicAuthUser || $request->getPassword() != $veryBasicAuthPass) {
if(in_array(app()->environment(), $config['envs'])) {
if($request->getUser() != $config['user'] || $request->getPassword() != $config['password']) {

$header = ['WWW-Authenticate' => 'Basic'];

// If view is available
if ($veryBasicAuthView) {
return response()->view($veryBasicAuthView, [], 401)
if (isset($config['error_view'])) {
return response()->view($config['error_view'], [], 401)
->withHeaders($header);
}

// Else return default message
return response($veryBasicAuthMsg, 401, $header);
return response($config['error_message'], 401, $header);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/VeryBasicAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
class VeryBasicAuthServiceProvider extends ServiceProvider
{

/**
* Path to config-file
* @var string
*/
protected $config;

/**
* Path to stub
* @var string
*/
protected $stub;

/**
Expand All @@ -28,7 +36,6 @@ public function __construct($app) {

/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot(\Illuminate\Routing\Router $router)
Expand All @@ -47,7 +54,6 @@ public function boot(\Illuminate\Routing\Router $router)

/**
* Register any package services.
*
* @return void
*/
public function register()
Expand All @@ -60,7 +66,7 @@ public function register()

/**
* Crates a new config-file with a random password
* @return str bytes written
* @return string bytes written
*/
private function createConfig()
{
Expand Down

0 comments on commit ede0f0c

Please sign in to comment.