@@ -15,6 +15,29 @@ $ composer require php-http/message
15
15
16
16
## Authentication methods
17
17
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
+
18
41
General usage looks like the following:
19
42
20
43
``` php
@@ -27,38 +50,51 @@ $authentication->authenticate($request);
27
50
28
51
### Basic Auth
29
52
30
- This authentication method accepts two parameters: username and password. Getters/Setters are provided by
31
- ` Http\Message\Authentication\UserPasswordPair ` trait.
32
-
33
53
``` php
34
54
use Http\Message\Authentication\BasicAuth;
35
55
36
56
$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
57
```
43
58
44
59
45
60
### Bearer
46
61
47
- This authentication method accepts one parameter: a token.
48
-
49
62
``` php
50
63
use Http\Message\Authentication\Bearer;
51
64
52
65
$authentication = new Bearer('token');
66
+ ```
67
+
68
+
69
+ ### WSSE
70
+
71
+ ``` php
72
+ use Http\Message\Authentication\Wsse;
53
73
54
- // Token can be set later as well
55
- $authentication->setToken('token');
74
+ $authentication = new Wsse('username', 'password');
56
75
```
57
76
58
77
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.
60
94
61
- This authentication method accepts one parameter: an array of other authentication methods.
95
+
96
+
97
+ ### Chain
62
98
63
99
The idea behind this authentication method is that in some cases you might need to
64
100
authenticate the request with multiple methods.
@@ -76,17 +112,6 @@ $authenticationChain = [
76
112
];
77
113
78
114
$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
115
```
91
116
92
117
@@ -102,60 +127,31 @@ For example a common use case is to authenticate requests sent to certain paths.
102
127
103
128
104
129
``` php
105
- use Http\Message\Authentication\Mathing;
130
+ use Http\Message\Authentication\Matching;
131
+ use Psr\Http\Message\RequestInterface;
106
132
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();
108
137
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
+ );
112
141
```
113
142
114
143
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 `
116
145
The first argument is an authentication method, the second is a regexp to match against the URL.
117
146
118
147
119
148
``` php
120
- use Http\Message\Authentication\Mathing ;
149
+ use Http\Message\Authentication\Matching ;
121
150
122
151
$authentication = Matching::createUrlMatcher(new AuthenticationMethod(), '\/api');
123
152
```
124
153
125
154
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
-
159
155
## Implement your own
160
156
161
157
Implementing an authentication method is easy: only one method needs to be implemented.
@@ -170,16 +166,8 @@ class MyAuth implements Authentication
170
166
{
171
167
// do something with the request
172
168
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.
174
170
return $request;
175
171
}
176
172
}
177
173
```
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