|
| 1 | +# Authentication |
| 2 | + |
| 3 | +The Authentication component allows you to to implement authentication methods which can simply update the request |
| 4 | +with authentication detail (for example by adding an `Authorization` header). |
| 5 | +This is useful when you have to send multiple requests to the same endpoint. Using an authentication implementation, |
| 6 | +these details can be separated from the actual requests. |
| 7 | + |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +``` |
| 12 | +$ composer require php-http/message |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +## Authentication methods |
| 17 | + |
| 18 | +General usage looks like the following: |
| 19 | + |
| 20 | +``` php |
| 21 | +$authentication = new AuthenticationMethod(); |
| 22 | + |
| 23 | +/** @var Psr\Http\Message\RequestInterface */ |
| 24 | +$authentication->authenticate($request); |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +### Basic Auth |
| 29 | + |
| 30 | +This authentication method accepts two parameters: username and password. Getters/Setters are provided by |
| 31 | +`Http\Message\Authentication\UserPasswordPair` trait. |
| 32 | + |
| 33 | +``` php |
| 34 | +use Http\Message\Authentication\BasicAuth; |
| 35 | + |
| 36 | +$authentication = new BasicAuth('username', 'password'); |
| 37 | + |
| 38 | +// These details can be set later as well |
| 39 | +// There are also getters with the appropriate method names |
| 40 | +$authentication->setUsername('username'); |
| 41 | +$authentication->setPassword('password'); |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +### Bearer |
| 46 | + |
| 47 | +This authentication method accepts one parameter: a token. |
| 48 | + |
| 49 | +``` php |
| 50 | +use Http\Message\Authentication\Bearer; |
| 51 | + |
| 52 | +$authentication = new Bearer('token'); |
| 53 | + |
| 54 | +// Token can be set later as well |
| 55 | +$authentication->setToken('token'); |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | +### Chain |
| 60 | + |
| 61 | +This authentication method accepts one parameter: an array of other authentication methods. |
| 62 | + |
| 63 | +The idea behind this authentication method is that in some cases you might need to |
| 64 | +authenticate the request with multiple methods. |
| 65 | + |
| 66 | +For example it's a common practice to protect development APIs with Basic Auth and the regular token auth as well |
| 67 | +to protect the API from unnecessary processing. |
| 68 | + |
| 69 | + |
| 70 | +``` php |
| 71 | +use Http\Message\Authentication\Chain; |
| 72 | + |
| 73 | +$authenticationChain = [ |
| 74 | + new AuthenticationMethod1(), |
| 75 | + new AuthenticationMethod2(), |
| 76 | +]; |
| 77 | + |
| 78 | +$authentication = new Chain($authenticationChain); |
| 79 | + |
| 80 | +// Authentication chain can be modified later |
| 81 | +$authenticationChain = $authentication->getAuthenticationChain(); |
| 82 | + |
| 83 | +array_pop($authenticationChain); |
| 84 | + |
| 85 | +$authentication->setAuthenticationChain($authenticationChain); |
| 86 | + |
| 87 | +$authentication->clearAuthenticationChain(); |
| 88 | + |
| 89 | +$authentication->addAuthentication(new AuthenticationMethod3()); |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +### Matching |
| 94 | + |
| 95 | +With this authentication method you can conditionally add authentication details to your request by passing a callable |
| 96 | +to it. When a request is passed, the callable is called and used as a boolean value in order to decide whether |
| 97 | +the request should be authenticated or not. |
| 98 | +It also accepts an authentication method instance which does the actual authentication when the condition is |
| 99 | +fulfilled. |
| 100 | + |
| 101 | +For example a common use case is to authenticate requests sent to certain paths. |
| 102 | + |
| 103 | + |
| 104 | +``` php |
| 105 | +use Http\Message\Authentication\Mathing; |
| 106 | + |
| 107 | +$authentication = new Mathing(new AuthenticationMethod1(), function () { return true; }); |
| 108 | + |
| 109 | +// There are also getters with the appropriate method names |
| 110 | +$authentication->setAuthentication(new AuthenticationMethod2()); |
| 111 | +$authentication->setMatcher(function () { return false; }); |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | +In order to ease creating matchers for URLs/paths, there is a static factory method for this purpose. |
| 116 | +The first argument is an authentication method, the second is a regexp to match against the URL. |
| 117 | + |
| 118 | + |
| 119 | +``` php |
| 120 | +use Http\Message\Authentication\Mathing; |
| 121 | + |
| 122 | +$authentication = Matching::createUrlMatcher(new AuthenticationMethod(), '\/api'); |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | +### Query Params |
| 127 | + |
| 128 | +Add authentication details as URL Query params: |
| 129 | + |
| 130 | +`http://api.example.com/endpoint?access_token=9zh987g86fg87gh978hg9g79` |
| 131 | + |
| 132 | + |
| 133 | +``` php |
| 134 | +use Http\Authentication\QueryParams; |
| 135 | + |
| 136 | +$authentication = new QueryParams([ |
| 137 | + 'access_token' => '9zh987g86fg87gh978hg9g79', |
| 138 | +]); |
| 139 | +``` |
| 140 | + |
| 141 | +!!! warning "Warning:" |
| 142 | + Using query parameters for authentication is not safe. |
| 143 | + Only use it when absolutely necessary. |
| 144 | + |
| 145 | + |
| 146 | +### WSSE |
| 147 | + |
| 148 | +This method implements [WSSE Authentication](http://www.xml.com/pub/a/2003/12/17/dive.html). |
| 149 | +Just like Basic Auth, it also accepts a username-password pair and exposes the same methods as well. |
| 150 | + |
| 151 | + |
| 152 | +``` php |
| 153 | +use Http\Message\Authentication\Wsse; |
| 154 | + |
| 155 | +$authentication = new Wsse('username', 'password'); |
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | +## Implement your own |
| 160 | + |
| 161 | +Implementing an authentication method is easy: only one method needs to be implemented. |
| 162 | + |
| 163 | +``` php |
| 164 | +use Http\Message\Authentication\Authentication; |
| 165 | +use Psr\Http\Message\RequestInterface; |
| 166 | + |
| 167 | +class MyAuth implements Authentication |
| 168 | +{ |
| 169 | + public function authenticate(RequestInterface $request) |
| 170 | + { |
| 171 | + // do something with the request |
| 172 | + |
| 173 | + // keep in mind that the request is immutable! |
| 174 | + return $request; |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | +## Integration with HTTPlug |
| 181 | + |
| 182 | +Normally requests must be authenticated "by hand" which is not really convenient. |
| 183 | + |
| 184 | +If you use HTTPlug, you can integrate this component into the client using the |
| 185 | +[authentication plugin](/httplug/plugins/authentication). |
0 commit comments