2017 © Pedro PelĂĄez
 

library slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

image

jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  • Saturday, January 23, 2016
  • by jeremykendall
  • Repository
  • 30 Watchers
  • 241 Stars
  • 11,406 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 43 Forks
  • 13 Open issues
  • 16 Versions
  • 3 % Grown

The README.md

Slim Auth Build Status Coverage Status Dependencies Status

Slim Auth is an authorization and authentication library for the Slim Framework. Authentication is provided by the Zend Framework Zend\Authentication component, and authorization by the Zend Framework Zend\Permissions\Acl component., (*1)

Fair Warning: Documentation Mostly Complete

Slim Auth is fully functional and production ready (I've used it in production in multiple projects), but this documentation is incomplete. (Current status of the documentation is ~90% complete.), (*2)

If you're familiar with Zend\Authentication and Zend\Permissions\Acl, you'll be able to implement the library without any trouble. Otherwise, you might want to wait for the docs to be completed (no ETA) or open a GitHub issue with any questions or problems you encounter., (*3)

Caveat emptor and all that., (*4)

Slim SessionCookie No Longer Recomended

TL;DR: You will experience unexpected behavior if you use Zend\Authentication\Storage\Session as your auth storage and Slim\Middleware\SessionCookie to provide encrypted cookies when your Slim version is >= 2.6., (*5)

Earlier versions of this documentation (and the sample implementation) demonstrated the use of Slim's SessionCookie Middleware as a way to handle session storage in concert with Zend Session. As of Slim 2.6.0, Zend Session and Slim's SessionCookie middleware no longer play well together, and I've opted for a Zend Session only approach., (*6)

Requirements

Slim Auth works with all versions of Slim 2 >= 2.4.2. Slim Auth has not been tested against the upcoming Slim 3 release., (*7)

Example Implementation

I've put together an example implementation to demonstrate the library in action. The example implementation can be found here., (*8)

Installation

Installation is provided via [Composer][11]., (*9)

First, install Composer., (*10)

curl -s https://getcomposer.org/installer | php

Then install Slim Auth with the following Composer command., (*11)

composer require jeremykendall/slim-auth

Finally, add this line at the top of your application’s index.php file:, (*12)

require 'vendor/autoload.php';

Preparing Your App For Slim Auth

Database

Your database should have a user table, and that table must have a role column. The contents of the role column should be a string and correspond to the roles in your ACL. The table name and all other column names are up to you., (*13)

Here's an example schema for a user table. If you don't already have a user table, feel free to use this one:, (*14)

CREATE TABLE IF NOT EXISTS [users] (
    [id] INTEGE NOT NULL PRIMARY KEY,
    [username] VARCHAR(50) NOT NULL,
    [role] VARCHAR(50) NOT NULL,
    [password] VARCHAR(255) NULL
);

ACL

An Access Control List, or ACL, defines the set of rules that determines which group of users have access to which routes within your Slim application. Below is a very simple example ACL. Please pay special attention to the comments., (*15)

Please refer to the Zend\Permissions\Acl documentation for complete details on using the Zend Framework ACL component., (*16)

namespace Example;

use Zend\Permissions\Acl\Acl as ZendAcl;

class Acl extends ZendAcl
{
    public function __construct()
    {
        // APPLICATION ROLES
        $this->addRole('guest');
        // member role "extends" guest, meaning the member role will get all of 
        // the guest role permissions by default
        $this->addRole('member', 'guest');
        $this->addRole('admin');

        // APPLICATION RESOURCES
        // Application resources == Slim route patterns
        $this->addResource('/');
        $this->addResource('/login');
        $this->addResource('/logout');
        $this->addResource('/member');
        $this->addResource('/admin');

        // APPLICATION PERMISSIONS
        // Now we allow or deny a role's access to resources. The third argument
        // is 'privilege'. We're using HTTP method as 'privilege'.
        $this->allow('guest', '/', 'GET');
        $this->allow('guest', '/login', array('GET', 'POST'));
        $this->allow('guest', '/logout', 'GET');

        $this->allow('member', '/member', 'GET');

        // This allows admin access to everything
        $this->allow('admin');
    }
}

The Guest Role

Please note the guest role. You must use the name guest as the role assigned to unauthenticated users. The other role names are yours to choose., (*17)

Acl "Privileges"

IMPORTANT: The third argument to Acl::allow(), 'privileges', is either a string or an array, and should be an HTTP verb or HTTP verbs respectively. By adding the third argument, you are restricting route access by HTTP method. If you do not provide an HTTP verb or verbs, you are allowing access to the specified route via all HTTP methods. Be extremely vigilant here. You wouldn't want to accidentally allow a 'guest' role access to an admin DELETE route simply because you forgot to explicitly deny the DELETE route., (*18)

Configuring Slim Auth: Defaults

Now that you have a user database table with a role column and an ACL, you're ready to configure Slim Auth and add it to your application., (*19)

First, add use statements for the PasswordValidator (from the Password Validator library), the PDO adapter, and the Slim Auth Bootstrap., (*20)

use JeremyKendall\Password\PasswordValidator;
use JeremyKendall\Slim\Auth\Adapter\Db\PdoAdapter;
use JeremyKendall\Slim\Auth\Bootstrap;

Next, create your Slim application., (*21)

$app = new \Slim\Slim();

Authentication Adapter

From the Zend Authentication documentation:, (*22)

Zend\Authentication adapters are used to authenticate against a particular type of authentication service, such as LDAP, RDBMS, or file-based storage., (*23)

Slim Auth provides an RDBMS authentication adapter for [PDO][12]. The constructor accepts five required arguments:, (*24)

  • A \PDO instance
  • The name of the user table
  • The name of the identity, or username, column
  • The name of the credential, or password, column
  • An instance of JeremyKendall\Password\PasswordValidator
$db = new \PDO(<database connection info>);
$adapter = new PdoAdapter(
    $db, 
    <user table name>, 
    <identity column name>, 
    <credential column name>, 
    new PasswordValidator()
);

NOTE: Please refer to the Password Validator documentation for more information on the proper use of the library. If you choose not to use the Password Validator library, you will need to create your own authentication adapter., (*25)

Putting it all Together

Now it's time to instantiate your ACL and bootstrap Slim Auth., (*26)

$acl = new \Namespace\For\Your\Acl();
$authBootstrap = new Bootstrap($app, $adapter, $acl);
$authBootstrap->bootstrap();

Login Route

You'll need a login route, of course, and it's important that you name your route login using Slim's Route Names feature., (*27)

$app->map('/login', function() {})->via('GET', 'POST')->name('login');

This allows you to use whatever route pattern you like for your login route. Slim Auth will redirect users to the correct route using Slim's urlFor() Route Helper., (*28)

Here's a sample login route:, (*29)

// Login route MUST be named 'login'
$app->map('/login', function () use ($app) {
    $username = null;

    if ($app->request()->isPost()) {
        $username = $app->request->post('username');
        $password = $app->request->post('password');

        $result = $app->authenticator->authenticate($username, $password);

        if ($result->isValid()) {
            $app->redirect('/');
        } else {
            $messages = $result->getMessages();
            $app->flashNow('error', $messages[0]);
        }
    }

    $app->render('login.twig', array('username' => $username));
})->via('GET', 'POST')->name('login');

Logout Route

As authentication stores the authenticated user's identity, logging out consists of nothing more than clearing that identity. Clearing the identity is handled by Authenticator::logout., (*30)

$app->get('/logout', function () use ($app) {
    $app->authenticator->logout();
    $app->redirect('/');
});

And Done

That should get you most of the way. I'll complete documentation as soon as I'm able, but can't currently commit to an ETA. Again, please feel free to open and issue with any questions you might have regarding implementation., (*31)

Thanks for considering Slim Auth for your project., (*32)

The Versions

23/01 2016

dev-slim-3.x

dev-slim-3.x https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

23/01 2016

2.0.0-alpha+005

2.0.0.0-alpha https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

19/01 2016

dev-slim-2.x

dev-slim-2.x https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

18/01 2016

1.0.1

1.0.1.0 https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

04/01 2016

dev-feature/auth-handler

dev-feature/auth-handler https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

03/01 2016

1.0.0

1.0.0.0 https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

03/01 2016

dev-develop

dev-develop https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

03/01 2016

2.0.0-alpha

2.0.0.0-alpha https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

26/03 2015

dev-master

9999999-dev https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

25/03 2015

0.0.7-alpha

0.0.7.0-alpha https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

25/03 2015

0.0.6-alpha

0.0.6.0-alpha https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

14/03 2014

0.0.5-alpha

0.0.5.0-alpha https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

14/03 2014

0.0.4-alpha

0.0.4.0-alpha https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

12/01 2014

0.0.3-alpha

0.0.3.0-alpha https://github.com/jeremykendall/slim-auth

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

21/12 2013

0.0.2-alpha

0.0.2.0-alpha

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework

15/12 2013

0.0.1-alpha

0.0.1.0-alpha

Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization auth zf2 zend zend framework slim slim framework