|
| 1 | +STACK-2 Authentication Middlewares |
| 2 | +================================== |
| 3 | + |
| 4 | +A collection of [Stack][0] middlewares designed to help Stack Authentication middleware implementors adhere to the [STACK-2 Authentication][1] conventions. |
| 5 | + |
| 6 | + |
| 7 | +Installation |
| 8 | +------------ |
| 9 | + |
| 10 | +Through [Composer][2] as [dflydev/stack-authentication][3]. |
| 11 | + |
| 12 | + |
| 13 | +Middlewares |
| 14 | +----------- |
| 15 | + |
| 16 | +### Authentication Middleware |
| 17 | + |
| 18 | +The Authentication middleware takes care of setting up the handling of an inbound request by taking care of some [STACK-2 Authentication][2] housekeeping tasks: |
| 19 | + |
| 20 | + * If the `stack.authn.token` is set, it wraps the application in `WwwAuthenticateStackChallenge` and delegates. |
| 21 | + * If the there is an `authorization` header, it returns the result of then **authenticate** callback. |
| 22 | + * If anonymous requests are received and anonymous requests are allowed, it wraps the application in `WwwAuthenticateStackChallenge` and delegates. |
| 23 | + * Otherwise, it returns the result of the **challenge** callback. |
| 24 | + |
| 25 | +#### Usage |
| 26 | + |
| 27 | + use Symfony\Component\HttpFoundation\Response; |
| 28 | + use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 29 | + |
| 30 | + $challenge = function (Response $response) { |
| 31 | + // Assumptions that can be made: |
| 32 | + // * 401 status code |
| 33 | + // * WWW-Authenticate header with a value of "Stack" |
| 34 | + // |
| 35 | + // Expectations: |
| 36 | + // * MAY set WWW-Authenticate header to another value |
| 37 | + // * MAY return a brand new response (does not have to be |
| 38 | + // the original response) |
| 39 | + // * MUST return a response |
| 40 | + return $response; |
| 41 | + }; |
| 42 | + |
| 43 | + $authenticate = function (HttpKernelInterface $app, $anonymous) { |
| 44 | + // Assumptions that can be made: |
| 45 | + // * The $app can be delegated to at any time |
| 46 | + // * The anonymous boolean indicates whether or not we |
| 47 | + // SHOULD allow anonymous requests through or if we |
| 48 | + // should challenge immediately. |
| 49 | + // * Additional state, like $request, $type, and $catch |
| 50 | + // should be passed via use statement if they are needed. |
| 51 | + // |
| 52 | + // Expectations: |
| 53 | + // * SHOULD set 'stack.authn.token' attribute on the request |
| 54 | + // when authentication is successful. |
| 55 | + // * MAY delegate to the passed $app |
| 56 | + // * MAY return a custom response of any status (for example |
| 57 | + // returning a 302 or 400 status response is allowed) |
| 58 | + // * MUST return a response |
| 59 | + }; |
| 60 | + |
| 61 | + return (new Authentication($app, [ |
| 62 | + 'challenge' => $challenge, |
| 63 | + 'authenticate' => $authenticate, |
| 64 | + 'anonymous' => true, // default: false |
| 65 | + ])) |
| 66 | + ->handle($request, $type, $catch); |
| 67 | + |
| 68 | +### WwwAuthenticateStackChallenge Middleware |
| 69 | + |
| 70 | +The WwwAuthenticateStackChallenge middleware takes care of setting up the handling of an outbound response by taking care of some [STACK-2 Authentication][2] housekeeping tasks: |
| 71 | + |
| 72 | + * If the response has a 401 status code and has a WWW-Authenticate header with the value of Stack, it returns the result of the **challenge** callback. |
| 73 | + * Otherwise the original response from the delegated app is returned. |
| 74 | + |
| 75 | + |
| 76 | +#### Usage |
| 77 | + |
| 78 | + use Symfony\Component\HttpFoundation\Response; |
| 79 | + |
| 80 | + $challenge = function (Response $response) { |
| 81 | + // Assumptions that can be made: |
| 82 | + // * 401 status code |
| 83 | + // * WWW-Authenticate header with a value of "Stack" |
| 84 | + // |
| 85 | + // Expectations: |
| 86 | + // * MAY set WWW-Authenticate header to another value |
| 87 | + // * MAY return a brand new response (does not have to be |
| 88 | + // the original response) |
| 89 | + // * MUST return a response |
| 90 | + return $response; |
| 91 | + }; |
| 92 | + |
| 93 | + return (new WwwAuthenticateStackChallenge($app, $challenge)) |
| 94 | + ->handle($request, $type, $catch); |
| 95 | + |
| 96 | + |
| 97 | +License |
| 98 | +------- |
| 99 | + |
| 100 | +MIT, see LICENSE. |
| 101 | + |
| 102 | + |
| 103 | +Community |
| 104 | +--------- |
| 105 | + |
| 106 | +If you have questions or want to help out, join us in **#stackphp** or **#dflydev** channels on **irc.freenode.net**. |
| 107 | + |
| 108 | + |
| 109 | +[0]: http://stackphp.com/ |
| 110 | +[1]: http://stackphp.com/specs/STACK-2/ |
| 111 | +[2]: http://getcomposer.org |
| 112 | +[3]: https://packagist.org/packages/dflydev/stack-authentication |
0 commit comments