Git Product home page Git Product logo

minim's Introduction

Minim

Minimal single-user auth in PHP.

Logo

Every so often, you build a website that needs:

  • to run without a database
  • to have an administrator backend
  • to be accessible by one user only

Minim is designed for this purpose; to be a secure, single-user authentication system that doesn't do anything silly like leak the users password (or store it in plain text) or operate over insecure (non-HTTPS) connections unless you want it to.

Installation

Install Minim via Composer like this:

composer require semibreve/minim

Or alternatively, if you're using the PHAR (make sure the php.exe executable is in your PATH):

php composer.phar require semibreve/minim

Configuration

Minim will require you to create a configuration file that looks something like this:

# Don't commit this file to source control, it contains your secret settings.

admin_email: [email protected] # The e-mail address of the user, used as a username.
admin_password_hash: $2y$10$x8.kXrWv4lXFpObosuwQ0uoiQAUeFAlEL.oi0tN5pnM.72hoK9e8K # The user's password hash.
secret_key: 7WCPTI3of3cp # The secret key the application uses for symmetric encryption
token_length: 32 # The length, in bytes, of any generated authentication tokens.
token_ttl: 1200 # The time to live for authentication tokens, in seconds.
cookie_name: minim_auth # The name of the authentication cookie.
session_file_name: /var/www/minim/token.dat # The name of the session file on-disk.
cookie_ssl_only: false # Whether or not cookies are enabled for HTTPS only. If enabled, non-HTTPS requests will fail.
cookie_http_only: true # Whether to restrict cookies to HTTP only and disallow access by client-side script.

The above file specifies some default credentials:

Email: [email protected]
Password: demo

These must be changed before you go into production, so you need to do the following:

  • Copy the demo configuration file above into your project. Make sure it is ignored by any version control systems.
  • Open it up in your favorite text editor.
  • Change the admin_email field to your email address
  • Change the admin_password_hash field to the bcrypt hash of a password of your choice. Generate the hash using the bundled minim-genhash utility by invoking php vendor/bin/minim-genhash <password> from the project root.
  • Change the secret_key field to a randomly-generated string at least 12 characters long.
  • Change the salt field to a randomly-generated string at least 12 characters long.
  • The default value of 32 for the token_length field should be okay for most applications.
  • The default value for the token_ttl field of 1200 seconds (20 minutes) should be okay for most applications.
  • Change the session_file_name field to the absolute path of a writable file on your server that Minim can read and write, but that your server will not serve.
  • Change cookie_ssl_only field to true if you're operating over HTTPS. If you're not, take a long hard look at your application and ask yourself why you're considering asking for user credentials over an insecure connection when amazing, free tools like Let's Encrypt exist.
  • Leave cookie_http_only as true to make the authentication cookie readable only over HTTP and not by client-side script.

To see an example usage of Minim, check out the demo repository.

Usage

Load your Minim configuration file like this:

$auth = new Authenticator(new Configuration('my-config-file.yml'));

From here you can log the user in:

$auth->authenticate('email', 'password'); // Authenticate user, true on success false on failure.

Or redirect away from a page based on whether they're logged in or not:

// Check if user is authenticated.
if (!$auth->isAuthenticated()) {
    header('Location: /forbidden.php'); // Not logged in, go to jail.
    die();
}

Limitations

Don't rely on Minim to be secure out of the box and always perform your own penetration testing.

minim's People

Contributors

lambdacasserole avatar

Watchers

 avatar  avatar

minim's Issues

Hashing algorithm

Hey, nice project. I understand the motivation as I run a few self-hosted things just for myself, too. Only one of them runs on PHP though. I could still give this a try for that.

I would suggest switching to bcrypt for your hashing algorithm though, as it's designed for password hashing with a work factor that can be increased as computers get faster. Most likely it's not actually a concern for one account's password on some self-hosted applications, but IMO it's worth trying to use the best things for hobby projects ๐Ÿ˜‰.

PHP has a built-in set of functions (as of 5.5): http://php.net/password. There is also a library for backwards compatibility if needed: https://github.com/ircmaxell/password_compat. Read http://blog.ircmaxell.com/2012/12/seven-ways-to-screw-up-bcrypt.html to make sure you're doing it right ๐Ÿ˜„

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.