Coder Social home page Coder Social logo

laravel-stagefront's Introduction

Laravel StageFront

GitHub release Laravel License Build Status Code Quality Total Downloads

ko-fi

Quickly add password protection to a staging site.

Shielding a staging or demo website from the public usually involves setting op authentication separate from the actual project. This isn't always easy or is cumbersome at the least.

It doesn't have to be!

By installing StageFront with composer, adding the middleware and setting 3 variables in your .env file you are ready to go. As you will discover below, you also have a bunch more options available.

Login Screen

✅ Requirements

📦 Installation

☑️ Require the package via Composer:

composer require codezero/laravel-stagefront

Laravel will automatically register the ServiceProvider and routes.

When StageFront is disabled, its routes will not be registered.

☑️ Install Middleware

To activate the middleware, add it to the web middleware group in app/Http/Kernel.php, right after the StartSession middleware:

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Session\Middleware\StartSession::class, // <= after this
        \CodeZero\StageFront\Middleware\RedirectIfStageFrontIsEnabled::class,
        //...
    ],
];

In Laravel 6+ you need to add the middleware to the $middlewarePriority array in app/Http/Kernel.php, right after the StartSession middleware.

protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class, // <= after this
    \CodeZero\StageFront\Middleware\RedirectIfStageFrontIsEnabled::class,
    //...
];

Now you just need to set some .env variables and you are up and running!

⌨️ Quick Setup

Set some options in your .env file or publish the configuration file.

See an example .env file.

Enable StageFront and choose a login and password:

Option Type Default
STAGEFRONT_ENABLED bool false
STAGEFRONT_LOGIN string stagefront
STAGEFRONT_PASSWORD string stagefront
STAGEFRONT_ENCRYPTED bool false

By default StageFront is disabled and uses a plain text password when it's enabled. If you set STAGEFRONT_ENCRYPTED to true the password should be a hashed value. You can generate this using Laravel's \Hash::make('your password') function.

Artisan Commands for Quick Setup

You can also update the credentials in the .env file with our artisan command:

php artisan stagefront:credentials <username> <password> --encrypt

If you don't enter a username or password, the command will ask for your input step by step:

php artisan stagefront:credentials

Next, you can enable or disable StageFront:

php artisan stagefront:enable
php artisan stagefront:disable

👥 Database Logins

If you have existing users in the database and want to use those credentials, you can set STAGEFRONT_DATABASE to true. The above login and password settings will then be ignored.

Option Type Default
STAGEFRONT_DATABASE bool false
STAGEFRONT_DATABASE_WHITELIST string null
STAGEFRONT_DATABASE_TABLE string users
STAGEFRONT_DATABASE_LOGIN_FIELD string email
STAGEFRONT_DATABASE_PASSWORD_FIELD string password

If you want to grant access to just a few of those users, you can whitelist them by setting STAGEFRONT_DATABASE_WHITELIST to a comma separated string: '[email protected],[email protected]'. In the config file itself you can also use an array of e-mail addresses.

By default the users table is used with the email and password field names. But you can change this if you are using some other table or fields.

🔖 IP Whitelist

You can add a comma separated list of IP's to grant these users easier or exclusive access to your staging site. For example: '1.2.3.4,1.2.3.4'. In the config file itself you can also use an array of IP's.

Option Type Default
STAGEFRONT_IP_WHITELIST string null
STAGEFRONT_IP_WHITELIST_ONLY bool false
STAGEFRONT_IP_WHITELIST_REQUIRE_LOGIN bool false

When you add IP's to your whitelist, the default behavior is that these users will have instant access to the site, while someone with another IP will be presented with the normal login form.

To exclusively allow whitelisted IP's to access your site, set STAGEFRONT_IP_WHITELIST_ONLY to true. Users from other IP's will now get a 403 - Forbidden error.

To crank up security, you may also require whitelisted IP's to go through the login form. Set STAGEFRONT_IP_WHITELIST_REQUIRE_LOGIN to true to set this up.

⚙️ Other Options

☑️ Change Route URL

By default a GET and POST route will be registered with the /stagefront URL.

You can change the URL by setting this option:

Option Type Default
STAGEFRONT_URL string stagefront

It runs under the web middleware since it uses the session to keep you logged in.

You can change the middleware if needed in the configuration file.

☑️ Throttle Login Attempts

To prevent malicious users from brute forcing passwords, login attempts will be throttled unless you disable it. You can change the number of failed attempts per minute to allow, and the delay (in minutes) that users have to wait after reaching the maximum failed attempts.

Option Type Default
STAGEFRONT_THROTTLE bool true
STAGEFRONT_THROTTLE_TRIES integer 3 (per minute)
STAGEFRONT_THROTTLE_DELAY integer 5 (minutes)

When you tried to login too many times, Laravel's 429 error page will be shown. You can easily modify this by creating a 429.blade.php view in resources/views/errors. To save you a little time, I have included a localized template you can include in that page:

@include('stagefront::429')

If you want to include a different partial for other throttled pages, you can check the request:

@if (request()->is(config('stagefront.url')))
    @include('stagefront::429')
@else
    @include('your.partial.view')
@endif

Text in this view can be changed via the translation files.

Throttle Screen

☑️ Ignore URLs

If for any reason you wish to disable StageFront on specific routes, you can add these to the ignore_urls array in the configuration file. You can use wildcards if needed. You can't set this in the .env file.

For example:

'ignore_urls' => [
    // ignores /john, but noting under /john
    '/john',
    // ignores everyting under /jane, but not /jane itself
    '/jane/*',
],

☑️ Ignore Domains

If for any reason you wish to disable StageFront on specific doamins, you can add these to the ignore_udomains array in the configuration file. You can't set this in the .env file.

For example:

'ignore_domains' => [
    'admin.domain.com',
],

☑️ Link Live Site

If you set the URL to your live site, a link will be shown underneath the login form.

Option Type Default
STAGEFRONT_LIVE_SITE string null

Make sure you enter the full URL, including https://.

☑️ Change App Name

By default, the app name that is configured in config/app.php is shown as a title on the login and throttle page. You can use a different title by setting this option:

Option Type Default
STAGEFRONT_APP_NAME string config('app.name')

📇 Publish Configuration File

You can also publish the configuration file.

php artisan vendor:publish --provider="CodeZero\StageFront\StageFrontServiceProvider" --tag="config"

Each option is documented.

📑 Translations and Views

You can publish the translations to quickly adjust the text on the login screen and the errors.

php artisan vendor:publish --provider="CodeZero\StageFront\StageFrontServiceProvider" --tag="lang"

If you want to customize the login page entirely, you can also publish the view.

php artisan vendor:publish --provider="CodeZero\StageFront\StageFrontServiceProvider" --tag="views"

Extra translations are always welcome. :)

📏 Laravel Debugbar

Laravel Debugbar will be disabled on the StageFront routes automatically if you use it in your project. This will hide any potential sensitive data from the public, if by accident Debugbar is running on your staging site. You can disable this feature by editing the middleware option in the configuration file.

🚧 Testing

composer test

☕️ Credits

🔓 Security

If you discover any security related issues, please e-mail me instead of using the issue tracker.

📑 Changelog

A complete list of all notable changes to this package can be found on the releases page.

📜 License

The MIT License (MIT). Please see License File for more information.

laravel-stagefront's People

Contributors

alexisserneels avatar ivanvermeyen avatar jamesking56 avatar pezhvak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-stagefront's Issues

Consistent 404s after login on Laravel 9

Hello!

I saw issue #4 and I thought it might solve my problem but unfortunately it doesn't help.

The problem

After installing the project via composer, I then add the middleware:

diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php
index 9fb429db..2a6c3058 100644
--- a/app/Http/Kernel.php
+++ b/app/Http/Kernel.php
@@ -35,6 +35,7 @@ class Kernel extends HttpKernel
             \App\Http\Middleware\EncryptCookies::class,
             \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
             \Illuminate\Session\Middleware\StartSession::class,
+            \CodeZero\StageFront\Middleware\RedirectIfStageFrontIsEnabled::class,
             // \Illuminate\Session\Middleware\AuthenticateSession::class,
             \Illuminate\View\Middleware\ShareErrorsFromSession::class,
             \App\Http\Middleware\VerifyCsrfToken::class,

(Note this is the only place to add it, there is no $middlewarePriority for Laravel 9 from what I can tell).

After this, I add these .env values

STAGEFRONT_ENABLED=TRUE
STAGEFRONT_LOGIN=test
STAGEFRONT_PASSWORD=password

The stagefront page loads successfully but any successful or unsuccessful logins get immediately redirected to a 404.
I have cleared and populated the cache with route:cache and ran into the same problems.

Thanks, and I hope you can help!

Routes won't be registered automatically

After successfully installing everything via composer require and setting up the Middleware and updating the .ENV variables with the Quick setup via php artisan. Everything worked fine. After clearing config, view and route cache I reloaded the website but nothing happened. I got a 404 error that the page was not found (/stagefront or /login). I checked the web.php routes file and it didn't have any new configuration or code added. I had to manually add the code from the repo under routes/routes.php to my web.php in order for Stagefront to work properly.

I don't think this is a proper solution nor the indented behaviour as your Readme states Routes should be registered automatically. I'm using Laravel 8.16.1 and "codezero/laravel-stagefront": "^2.3"

If you need any code or other info please let me know.

Debug bar appears on "249" error page

Laravel Version: 8.12
PHP version: 7.14

I'm trying to test the package, and it seems the debug bar is showing when the visitor reaches the max attempt of login tries.

image

Usage of bcrypt in Readme

If you wish to give an encrypted password to stagefront, the Readme tells you to use bcrypt() to generate this.

However, the code in the repository uses Hash::check() to check the password. Hash::check() in newer Laravel versions may use bcrypt, argon2 and argon2id.

Therefore, if your Laravel site is using argon2 or argon2id, these instructions are incorrect.

I did think we might be able to update this with instructions to use php artisan tinker but laravel/tinker has been split from Laravel and may not be installed.

A better solution might be for this package to provide an artisan command to encrypt the password (and optionally add to .env automatically)

IP Whitelist

Firstly, fantastic package!

One of my clients wanted to use an IP whitelist as well as the password form. This is so that they can whitelist their internal office static IP address, allowing employees inside of the office the ability to view the staging site without needing a login.

They also wanted the login for outside the office looking at the staging site.

I've used my own rolled package for my client now, but it would be great if this feature was included (at least with a separate env option to enable it).

Contribution

Hi, thanks for this repo, i would like to be a member of this if you will. there are somethings that i think would make this repo better than it is already. one would be adding stage_domain instead of turning it on or off in env

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.