Skip to content

Commit

Permalink
Add Laravel 11 Support (#64)
Browse files Browse the repository at this point in the history
* fix: editor config for github actions workflow

chore: update composer to support Laravel 11

chore: update workflow to test L11, L10 and L9

* WIP

* Fix Tests

* Fix missing class import

* Fix Tests

* WIP Change to pest functions

* Fix typo

* Fix tests to use Pest functions

* Fix

* Fix

* Remove lint script
  • Loading branch information
sweptsquash authored Feb 22, 2024
1 parent 9efadad commit 64dde90
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 281 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
83 changes: 44 additions & 39 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
name: Run tests

on: [push, pull_request]
on:
push:
pull_request:

jobs:
php-tests:
runs-on: ubuntu-latest

strategy:
matrix:
include:
- php: 8.2
illuminate: ^10.0
- php: 8.1
illuminate: ^9.0
- php: 8.1
illuminate: ^8.0
- php: 8.0
illuminate: ^8.0
- php: 8.0
illuminate: ^7.0
- php: 7.4
illuminate: ^7.0

name: PHP ${{ matrix.php }} - Illuminate ${{ matrix.illuminate }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Update composer
run: composer self-update --2

- name: Install dependencies
run: composer require "illuminate/support:${{ matrix.illuminate }}" --no-interaction --no-progress --no-suggest

- name: Execute tests
run: composer test
php-tests:
runs-on: ubuntu-latest

strategy:
matrix:
php: [8.3, 8.2, 8.1, 8.0]
laravel: [11.*, 10.*, 9.*]
dependency-version: [prefer-lowest, prefer-stable]
exclude:
- laravel: 11.*
php: 8.1
- laravel: 11.*
php: 8.0
- laravel: 10.*
php: 8.0
- laravel: 9.*
php: 8.3
- laravel: 9.*
php: 8.2

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
tools: composer:v2
coverage: none

- name: Install dependencies
run: |
composer require "illuminate/support:${{ matrix.laravel }}" --no-interaction --no-progress --no-suggest
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress
- name: Execute tests
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
composer.lock
.DS_Store
.phpunit.result.cache
.phpunit.cache
phpunit-output
39 changes: 20 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
{
"name": "olssonm/l5-very-basic-auth",
"description": "Laravel stateless HTTP basic auth without the need for a database",
"license": "MIT",
"keywords": [
"olssonm",
"laravel",
"authentication",
"http basic auth"
],
"homepage": "https://github.com/olssonm/l5-very-basic-auth",
"license": "MIT",
"authors": [
{
"name": "Marcus Olsson",
"email": "[email protected]",
"homepage": "https://marcusolsson.me"
}
],
"homepage": "https://github.com/olssonm/l5-very-basic-auth",
"require": {
"illuminate/support": "^7.0|^8.0|^9.0|^10.0",
"php": "~7.4|^8.0",
"php": "^8.0 || ^8.1 || ^8.2",
"illuminate/support": "^9.0 || ^10.0 || ^11.0",
"squizlabs/php_codesniffer": "^3.5"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": ">=5.0",
"laravel/helpers": "^1.1",
"pestphp/pest": "^1.0",
"pestphp/pest-plugin-laravel": "^1.2"
"orchestra/testbench": "^7.0 || ^8.0 || ^9.0",
"pestphp/pest": "^1.0 || ^2.0",
"pestphp/pest-plugin-laravel": "^1.2 || ^2.0",
"phpunit/phpunit": "^9.0 || ^10.5"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"Olssonm\\VeryBasicAuth\\": "src"
Expand All @@ -38,26 +40,25 @@
"Olssonm\\VeryBasicAuth\\Tests\\": "tests"
}
},
"scripts": {
"phpsniff": "vendor/bin/phpcs --standard=\"PSR12\" ./src --ignore=./src/resources/*",
"phpfix": "vendor/bin/phpcbf --standard=\"PSR12\" ./src --ignore=./src/resources/*",
"test": "vendor/bin/pest"
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
},
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "6.x-dev"
"dev-master": "11.x-dev"
},
"laravel": {
"providers": [
"Olssonm\\VeryBasicAuth\\VeryBasicAuthServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
"scripts": {
"phpfix": "vendor/bin/phpcbf --standard=\"PSR12\" ./src --ignore=./src/resources/*",
"phpsniff": "vendor/bin/phpcs --standard=\"PSR12\" ./src --ignore=./src/resources/*",
"test": "vendor/bin/pest"
}
}
35 changes: 18 additions & 17 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="false"
processIsolation="false"
stopOnFailure="false"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<file>./tests/VeryBasicAuthTests.php</file>
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<source>
<include>
<directory>src</directory>
<exclude>
<directory suffix=".blade.php">src</directory>
</exclude>
</whitelist>
</filter>
</include>
<exclude>
<directory suffix=".blade.php">src</directory>
</exclude>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Handlers/DefaultResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __invoke(Request $request)
'WWW-Authenticate' => sprintf(
'Basic realm="%s", charset="UTF-8"',
config('very_basic_auth.realm', 'Basic Auth')
)
),
];

// View
Expand All @@ -22,7 +22,7 @@ public function __invoke(Request $request)
// If the request want's JSON, else view
if ($request->wantsJson()) {
return response()->json([
'message' => config('very_basic_auth.error_message')
'message' => config('very_basic_auth.error_message'),
], 401, $header);
} elseif (isset($view)) {
return response()->view($view, [], 401)
Expand Down
15 changes: 5 additions & 10 deletions src/Http/Middleware/VeryBasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Olssonm\VeryBasicAuth\Http\Middleware;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Closure;
use Olssonm\VeryBasicAuth\Handlers\DefaultResponseHandler;
use Illuminate\Http\Request;
use Olssonm\VeryBasicAuth\Handlers\ResponseHandler;
use Symfony\Component\HttpFoundation\Response;

class VeryBasicAuth
{
Expand All @@ -20,11 +19,8 @@ public function __construct(ResponseHandler $responseHandler)
/**
* Handle an incoming request
*
* @param Request $request
* @param Closure $next
* @param mixed $username
* @param mixed $password
* @return \Symfony\Component\HttpFoundation\Response
* @param mixed $username
* @param mixed $password
*/
public function handle(Request $request, Closure $next, $username = null, $password = null): Response
{
Expand All @@ -38,7 +34,7 @@ public function handle(Request $request, Closure $next, $username = null, $passw
$authUsername = $username ?? config('very_basic_auth.user');
$authPassword = $password ?? config('very_basic_auth.password');

if (!$authUsername && !$authPassword) {
if (! $authUsername && ! $authPassword) {
return $next($request);
} elseif ($request->getUser() !== $authUsername || $request->getPassword() !== $authPassword) {
return $this->deniedResponse($request);
Expand All @@ -51,7 +47,6 @@ public function handle(Request $request, Closure $next, $username = null, $passw
/**
* Return a error response
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
private function deniedResponse(Request $request): Response
Expand Down
1 change: 0 additions & 1 deletion src/VeryBasicAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Olssonm\VeryBasicAuth;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Olssonm\VeryBasicAuth\Handlers\DefaultResponseHandler;
use Olssonm\VeryBasicAuth\Handlers\ResponseHandler;

Expand Down
59 changes: 30 additions & 29 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
<?php

/**
* Configuration for the "HTTP Very Basic Auth"-middleware
*/
return [
// Username
'user' => env('BASIC_AUTH_USERNAME', ''),

// Password
'password' => env('BASIC_AUTH_PASSWORD', ''),

// Environments where the middleware is active. Use "*" to protect all envs
'envs' => [
'*'
],

// Response handler for the error responses
'response_handler' => \Olssonm\VeryBasicAuth\Handlers\DefaultResponseHandler::class,

// Message to display if the user "opts out"/clicks "cancel"
'error_message' => 'You have to supply your credentials to access this resource.',

// Message to display in the auth dialiog in some browsers (mainly Internet Explorer).
// Realm is also used to define a "space" that should share credentials.
'realm' => 'Basic Auth',

// If you prefer to use a view with your error message you can uncomment "error_view".
// This will supersede your default response message
// 'error_view' => 'very_basic_auth::default'
];
/**
* Configuration for the "HTTP Very Basic Auth"-middleware
*/

return [
// Username
'user' => env('BASIC_AUTH_USERNAME', ''),

// Password
'password' => env('BASIC_AUTH_PASSWORD', ''),

// Environments where the middleware is active. Use "*" to protect all envs
'envs' => [
'*',
],

// Response handler for the error responses
'response_handler' => \Olssonm\VeryBasicAuth\Handlers\DefaultResponseHandler::class,

// Message to display if the user "opts out"/clicks "cancel"
'error_message' => 'You have to supply your credentials to access this resource.',

// Message to display in the auth dialiog in some browsers (mainly Internet Explorer).
// Realm is also used to define a "space" that should share credentials.
'realm' => 'Basic Auth',

// If you prefer to use a view with your error message you can uncomment "error_view".
// This will supersede your default response message
// 'error_view' => 'very_basic_auth::default'
];
18 changes: 16 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?php

namespace Olssonm\VeryBasicAuth\Tests;
use Illuminate\Support\Facades\Route;
use Olssonm\VeryBasicAuth\Http\Middleware\VeryBasicAuth;
use Olssonm\VeryBasicAuth\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);
uses(TestCase::class)
->beforeEach(function () {
// Set default config for testing
config()->set('very_basic_auth.user', 'test');
config()->set('very_basic_auth.password', 'test');

Route::get('/', fn () => 'ok')->middleware(VeryBasicAuth::class)->name('default');
Route::get('/test', fn () => 'ok')->middleware(VeryBasicAuth::class);
Route::get('/inline', fn () => 'ok')->middleware(
sprintf('auth.very_basic:%s,%s', config('very_basic_auth.user'), config('very_basic_auth.password'))
)->name('inline');
})
->in(__DIR__);
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function setUp(): void
protected function getPackageProviders($app)
{
return [
VeryBasicAuthServiceProvider::class
VeryBasicAuthServiceProvider::class,
];
}

Expand Down
Loading

0 comments on commit 64dde90

Please sign in to comment.