|
| 1 | +Firewall Authentication Middleware |
| 2 | +================================== |
| 3 | + |
| 4 | +A [Stack][0] middleware providing a simple, configurable firewall concept for |
| 5 | +[STACK-2 Authentication][1] compatible middlewares. |
| 6 | + |
| 7 | + |
| 8 | +Installation |
| 9 | +------------ |
| 10 | + |
| 11 | +Through [Composer][2] as [dflydev/stack-firewall][3]. |
| 12 | + |
| 13 | + |
| 14 | +Usage |
| 15 | +----- |
| 16 | + |
| 17 | +The Firewall middleware is a thin layer over [dflydev/stack-authentication][4] |
| 18 | +based STACK-2 Authentication middlewares. |
| 19 | + |
| 20 | +A **firewall* is defined as an array of associatve arrays representing paths |
| 21 | +for which an authentication middleware should be concerned. |
| 22 | + |
| 23 | +If a requested path does not match a firewalled path, the firewall delegates the |
| 24 | +request to the next layer immediately. |
| 25 | + |
| 26 | +If a requested path matches and authentication is missing or invalid and |
| 27 | +anonymous requests are allowed, the request is allowed through the firewall |
| 28 | +without setting the `stack.authn.token`. |
| 29 | + |
| 30 | +If a requested path matches and authentication is missing or inavlid and |
| 31 | +anonymous requests are NOT allowed, the firewall will challenge immediately. |
| 32 | + |
| 33 | +If no firewall is defined, the assumed configuration is: |
| 34 | + |
| 35 | + [['path' => '/']] |
| 36 | + |
| 37 | +This effectively means that by default the firewall will match all requests |
| 38 | +and will not allow anonymous requests resulting in returning a challenge. |
| 39 | + |
| 40 | + |
| 41 | +```php |
| 42 | +<?php |
| 43 | + |
| 44 | +use Symfony\Component\HttpFoundation\Response; |
| 45 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 46 | + |
| 47 | +// The firewall is an array of associative arrays containing the rules for which |
| 48 | +// the authentication middleware should be concerned. |
| 49 | +$firewall = [ |
| 50 | + ['path' => '/', 'anonymous' => true], |
| 51 | + ['path' => '/protected'], |
| 52 | +]; |
| 53 | + |
| 54 | +$challenge = function (Response $response) { |
| 55 | + // Assumptions that can be made: |
| 56 | + // * 401 status code |
| 57 | + // * WWW-Authenticate header with a value of "Stack" |
| 58 | + // |
| 59 | + // Expectations: |
| 60 | + // * MAY set WWW-Authenticate header to another value |
| 61 | + // * MAY return a brand new response (does not have to be |
| 62 | + // the original response) |
| 63 | + // * MUST return a response |
| 64 | + return $response; |
| 65 | +}; |
| 66 | + |
| 67 | +$authenticate = function (HttpKernelInterface $app, $anonymous) { |
| 68 | + // Assumptions that can be made: |
| 69 | + // * The $app can be delegated to at any time |
| 70 | + // * The anonymous boolean indicates whether or not we |
| 71 | + // SHOULD allow anonymous requests through or if we |
| 72 | + // should challenge immediately. |
| 73 | + // * Additional state, like $request, $type, and $catch |
| 74 | + // should be passed via use statement if they are needed. |
| 75 | + // |
| 76 | + // Expectations: |
| 77 | + // * SHOULD set 'stack.authn.token' attribute on the request |
| 78 | + // when authentication is successful. |
| 79 | + // * MAY delegate to the passed $app |
| 80 | + // * MAY return a custom response of any status (for example |
| 81 | + // returning a 302 or 400 status response is allowed) |
| 82 | + // * MUST return a response |
| 83 | +}; |
| 84 | + |
| 85 | +return (new Firewall($app, [ |
| 86 | + 'challenge' => $challenge, |
| 87 | + 'authenticate' => $authenticate, |
| 88 | + 'firewall' => $firewall, |
| 89 | + ])) |
| 90 | + ->handle($request, $type, $catch); |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +License |
| 95 | +------- |
| 96 | + |
| 97 | +MIT, see LICENSE. |
| 98 | + |
| 99 | + |
| 100 | +Community |
| 101 | +--------- |
| 102 | + |
| 103 | +If you have questions or want to help out, join us in the **#stackphp** or |
| 104 | +**#dflydev** channels on **irc.freenode.net**. |
| 105 | + |
| 106 | + |
| 107 | +[0]: http://stackphp.com/ |
| 108 | +[1]: http://stackphp.com/specs/STACK-2/ |
| 109 | +[2]: http://getcomposer.org |
| 110 | +[3]: https://packagist.org/packages/dflydev/stack-firewall |
0 commit comments