Skip to content

Commit d584259

Browse files
committed
Revert "Merge remote-tracking branch 'origin/master' into dev/hashed-password"
This reverts commit 5341ffc, reversing changes made to eaa27c9.
1 parent 5341ffc commit d584259

5 files changed

Lines changed: 53 additions & 6 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ Route::get('/', [
155155

156156
*Note:* inline credentials always take president over the `very_basic_auth.php`-configuration file.
157157

158+
### Generating hash for password
159+
160+
If you want to generate a hash for your password, you can use the `artisan`-command:
161+
162+
``` bash
163+
php artisan very-basic-auth:generate-password
164+
```
165+
166+
It will ask you for a password, and then automatically insert the hash into your `.env`-file
167+
168+
158169
## Testing
159170

160171
``` bash

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"require": {
2020
"php": "^8.0 || ^8.1 || ^8.2",
2121
"illuminate/support": "^9.0 || ^10.0 || ^11.0 || ^12.0",
22+
"laravel/prompts": "^0.3.5",
2223
"squizlabs/php_codesniffer": "^3.5"
2324
},
2425
"require-dev": {

src/Http/Middleware/VeryBasicAuth.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Http\JsonResponse;
77
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Hash;
89
use Olssonm\VeryBasicAuth\Handlers\ResponseHandler;
910
use Symfony\Component\HttpFoundation\Response;
1011

@@ -20,15 +21,15 @@ public function __construct(ResponseHandler $responseHandler)
2021
/**
2122
* Handle an incoming request
2223
*
23-
* @param mixed $username
24-
* @param mixed $password
24+
* @param mixed $username
25+
* @param mixed $password
2526
*/
2627
public function handle(Request $request, Closure $next, $username = null, $password = null): Response
2728
{
2829
$active = (count(array_intersect([
29-
'*',
30-
app()->environment(),
31-
], config('very_basic_auth.envs'))) > 0);
30+
'*',
31+
app()->environment(),
32+
], config('very_basic_auth.envs'))) > 0);
3233

3334
// Check if middleware is in use in current environment
3435
if ($active) {

src/VeryBasicAuthServiceProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Olssonm\VeryBasicAuth;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Olssonm\VeryBasicAuth\Console\PasswordGenerateCommand;
67
use Olssonm\VeryBasicAuth\Handlers\DefaultResponseHandler;
78
use Olssonm\VeryBasicAuth\Handlers\ResponseHandler;
89

@@ -25,7 +26,7 @@ class VeryBasicAuthServiceProvider extends ServiceProvider
2526
/**
2627
* Constructor
2728
*
28-
* @param \Illuminate\Contracts\Foundation\Application $app
29+
* @param \Illuminate\Contracts\Foundation\Application $app
2930
* @return void
3031
*/
3132
public function __construct($app)
@@ -52,6 +53,13 @@ public function boot(\Illuminate\Routing\Router $router)
5253

5354
// Register middleware
5455
$router->aliasMiddleware('auth.very_basic', \Olssonm\VeryBasicAuth\Http\Middleware\VeryBasicAuth::class);
56+
57+
// Register commands
58+
if ($this->app->runningInConsole()) {
59+
$this->commands([
60+
PasswordGenerateCommand::class,
61+
]);
62+
}
5563
}
5664

5765
/**

tests/VeryBasicAuthTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@
5555
expect($response->getContent())->toEqual(config('very_basic_auth.error_message'));
5656
});
5757

58+
test('request with incorrect credentials fails - hashed password', function () {
59+
60+
config()->set('very_basic_auth.user', 'test');
61+
config()->set('very_basic_auth.password', app()->make('hash')->make('test'));
62+
63+
$response = withHeaders([
64+
'PHP_AUTH_USER' => str_random(20),
65+
'PHP_AUTH_PW' => str_random(20),
66+
])->get('/');
67+
68+
expect($response->getStatusCode())->toEqual(401);
69+
});
70+
5871
test('request with incorrect credentials fails - json', function () {
5972
$response = withHeaders([
6073
'PHP_AUTH_USER' => str_random(20),
@@ -96,6 +109,19 @@
96109
expect($response->getContent())->toEqual('ok');
97110
});
98111

112+
test('request with correct credentials passes - hashed password', function () {
113+
config()->set('very_basic_auth.user', 'test');
114+
config()->set('very_basic_auth.password', app()->make('hash')->make('test'));
115+
116+
$response = withHeaders([
117+
'PHP_AUTH_USER' => 'test',
118+
'PHP_AUTH_PW' => 'test',
119+
])->get('/');
120+
121+
expect($response->getStatusCode())->toEqual(200);
122+
expect($response->getContent())->toEqual('ok');
123+
});
124+
99125
test('environments', function () {
100126
config()->set('very_basic_auth.envs', ['production']);
101127
get('/')->assertStatus(200);

0 commit comments

Comments
 (0)