@@ -15,6 +15,29 @@ $ composer require php-http/message
1515
1616## Authentication methods
1717
18+ Method | Parameters | Behavior
19+ ---------------- | ------------------------------------------------- | --------
20+ [ Basic Auth] [ 1 ] | Username and password | ` Authorization ` header of the HTTP specification
21+ Bearer | Token | ` Authorization ` header of the HTTP specification
22+ [ WSSE] [ 2 ] | Username and password | ` Authorization ` header of the HTTP specification
23+ Query Params | Array of param-value pairs | URI parameters
24+ Chain | Array of authentication instances | Behaviors of the underlying authentication methods
25+ Matching | An authentication instance and a matcher callback | Behavior of the underlying authentication method if the matcher callback passes
26+
27+ [ 1 ] : https://en.wikipedia.org/wiki/Basic_access_authentication
28+ [ 2 ] : http://www.xml.com/pub/a/2003/12/17/dive.html
29+
30+
31+ ## Integration with HTTPlug
32+
33+ Normally requests must be authenticated "by hand" which is not really convenient.
34+
35+ If you use HTTPlug, you can integrate this component into the client using the
36+ [ authentication plugin] ( /httplug/plugins/authentication ) .
37+
38+
39+ ## Examples
40+
1841General usage looks like the following:
1942
2043``` php
@@ -27,38 +50,51 @@ $authentication->authenticate($request);
2750
2851### Basic Auth
2952
30- This authentication method accepts two parameters: username and password. Getters/Setters are provided by
31- ` Http\Message\Authentication\UserPasswordPair ` trait.
32-
3353``` php
3454use Http\Message\Authentication\BasicAuth;
3555
3656$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');
4257```
4358
4459
4560### Bearer
4661
47- This authentication method accepts one parameter: a token.
48-
4962``` php
5063use Http\Message\Authentication\Bearer;
5164
5265$authentication = new Bearer('token');
66+ ```
67+
68+
69+ ### WSSE
70+
71+ ``` php
72+ use Http\Message\Authentication\Wsse;
5373
54- // Token can be set later as well
55- $authentication->setToken('token');
74+ $authentication = new Wsse('username', 'password');
5675```
5776
5877
59- ### Chain
78+ ### Query Params
79+
80+ ` http://api.example.com/endpoint?access_token=9zh987g86fg87gh978hg9g79 `
81+
82+
83+ ``` php
84+ use Http\Authentication\QueryParams;
85+
86+ $authentication = new QueryParams([
87+ 'access_token' => '9zh987g86fg87gh978hg9g79',
88+ ]);
89+ ```
90+
91+ !!! danger "Warning:"
92+ Using query parameters for authentication is not safe.
93+ Only use it when this is the only authentication method offered by a third party application.
6094
61- This authentication method accepts one parameter: an array of other authentication methods.
95+
96+
97+ ### Chain
6298
6399The idea behind this authentication method is that in some cases you might need to
64100authenticate the request with multiple methods.
@@ -76,17 +112,6 @@ $authenticationChain = [
76112];
77113
78114$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());
90115```
91116
92117
@@ -102,60 +127,31 @@ For example a common use case is to authenticate requests sent to certain paths.
102127
103128
104129``` php
105- use Http\Message\Authentication\Mathing;
130+ use Http\Message\Authentication\Matching;
131+ use Psr\Http\Message\RequestInterface;
106132
107- $authentication = new Mathing(new AuthenticationMethod1(), function () { return true; });
133+ $authentication = new Matching(
134+ new AuthenticationMethod1(),
135+ function (RequestInterface $request) {
136+ $path = $request->getUri()->getPath();
108137
109- // There are also getters with the appropriate method names
110- $authentication->setAuthentication(new AuthenticationMethod2());
111- $authentication->setMatcher(function () { return false; } );
138+ return 0 === strpos($path, '/api');
139+ }
140+ );
112141```
113142
114143
115- In order to ease creating matchers for URLs/paths, there is a static factory method for this purpose.
144+ In order to ease creating matchers for URLs/paths, there is a static factory method for this purpose: ` createUrlMatcher `
116145The first argument is an authentication method, the second is a regexp to match against the URL.
117146
118147
119148``` php
120- use Http\Message\Authentication\Mathing ;
149+ use Http\Message\Authentication\Matching ;
121150
122151$authentication = Matching::createUrlMatcher(new AuthenticationMethod(), '\/api');
123152```
124153
125154
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- !!! danger "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-
159155## Implement your own
160156
161157Implementing an authentication method is easy: only one method needs to be implemented.
@@ -170,16 +166,8 @@ class MyAuth implements Authentication
170166 {
171167 // do something with the request
172168
173- // keep in mind that the request is immutable!
169+ // keep in mind that the request is immutable - return the updated version of the request with the authentication information added to it.
174170 return $request;
175171 }
176172}
177173```
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