Skip to content

Commit 14171e7

Browse files
committed
Document authentication, closes #32, closes php-http/authentication#1
1 parent f76ccf6 commit 14171e7

File tree

2 files changed

+178
-1
lines changed

2 files changed

+178
-1
lines changed

docs/components/authentication.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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/authentication
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\Authentication\UserPasswordPair` trait.
32+
33+
``` php
34+
use Http\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\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\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\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\Authentication\Mathing;
121+
122+
$authentication = Matching::createUrlMatcher(new AuthenticationMethod(), '\/api');
123+
```
124+
125+
### WSSE
126+
127+
This method implements [WSSE Authentication](http://www.xml.com/pub/a/2003/12/17/dive.html).
128+
Just like Basic Auth, it also accepts a username-password pair and exposes the same methods as well.
129+
130+
131+
``` php
132+
use Http\Authentication\Wsse;
133+
134+
$authentication = new Wsse('username', 'password');
135+
```
136+
137+
138+
## Implement your own
139+
140+
Implementing an authentication method is easy: only one method needs to be implemented.
141+
142+
``` php
143+
use Http\Authentication\Authentication;
144+
use Psr\Http\Message\RequestInterface;
145+
146+
class MyAuth implements Authentication
147+
{
148+
public function authenticate(RequestInterface $request)
149+
{
150+
// do something with the request
151+
152+
// keep in mind that the request is immutable!
153+
return $request;
154+
}
155+
}
156+
```
157+
158+
159+
## Integration with HTTPlug
160+
161+
Normally requests must be authenticated "by hand" which is not really convenient.
162+
163+
If you use HTTPlug, you can integrate this component into the client using the
164+
[authentication plugin](/httplug/plugins/authentication).
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
# Authentication Plugin
22

3-
TODO: explain the authentication plugin
3+
This plugin uses the [authentication component](/components/authentication) to authenticate all requests sent through
4+
the client.
5+
6+
7+
## Usage
8+
9+
``` php
10+
use Http\Plugins\PluginClient;
11+
use Http\Plugins\AuthenticationPlugin;
12+
13+
$pluginClient = new PluginClient(new HttpClient(), [new AuthenticationPlugin(new AuthenticationMethod()]);
14+
```
15+
16+
Check the [component documentation](/components/authentication) for the list of available authentication methods.

0 commit comments

Comments
 (0)