@@ -56,49 +56,47 @@ public function testConstructor()
56
56
$ this ->assertInstanceOf (AuthenticationInterface::class, $ basicAccess );
57
57
}
58
58
59
- public function testIsAuthenticatedWithoutHeader ()
60
- {
61
- $ this ->request
62
- ->getHeader ('Authorization ' )
63
- ->willReturn ([]);
64
-
65
- $ basicAccess = new BasicAccess (
66
- $ this ->userRepository ->reveal (),
67
- 'test ' ,
68
- $ this ->responseFactory
69
- );
70
- $ this ->assertNull ($ basicAccess ->authenticate ($ this ->request ->reveal ()));
71
- }
72
59
73
- public function testIsAuthenticatedWithoutBasic ()
60
+ /**
61
+ * @param array $authHeader
62
+ * @dataProvider provideInvalidAuthenticationHeader
63
+ */
64
+ public function testIsAuthenticatedWithInvalidData (array $ authHeader )
74
65
{
75
66
$ this ->request
76
67
->getHeader ('Authorization ' )
77
- ->willReturn (['foo ' ]);
68
+ ->willReturn ($ authHeader );
69
+
70
+ $ this ->userRepository ->authenticate (Argument::any (), Argument::any ())->shouldNotBeCalled ();
78
71
79
72
$ basicAccess = new BasicAccess (
80
73
$ this ->userRepository ->reveal (),
81
74
'test ' ,
82
75
$ this ->responseFactory
83
76
);
84
-
85
77
$ this ->assertNull ($ basicAccess ->authenticate ($ this ->request ->reveal ()));
86
78
}
87
79
88
- public function testIsAuthenticatedWithValidCredential ()
80
+ /**
81
+ * @param string $username
82
+ * @param string $password
83
+ * @param array $authHeader
84
+ * @dataProvider provideValidAuthentication
85
+ */
86
+ public function testIsAuthenticatedWithValidCredential (string $ username , string $ password , array $ authHeader )
89
87
{
90
88
$ this ->request
91
89
->getHeader ('Authorization ' )
92
- ->willReturn ([ ' Basic QWxhZGRpbjpPcGVuU2VzYW1l ' ] );
90
+ ->willReturn ($ authHeader );
93
91
$ this ->request
94
92
->withAttribute (UserInterface::class, Argument::type (UserInterface::class))
95
93
->willReturn ($ this ->request ->reveal ());
96
94
97
95
$ this ->authenticatedUser
98
96
->getIdentity ()
99
- ->willReturn (' Aladdin ' );
97
+ ->willReturn ($ username );
100
98
$ this ->userRepository
101
- ->authenticate (' Aladdin ' , ' OpenSesame ' )
99
+ ->authenticate ($ username , $ password )
102
100
->willReturn ($ this ->authenticatedUser ->reveal ());
103
101
104
102
$ basicAccess = new BasicAccess (
@@ -109,7 +107,6 @@ public function testIsAuthenticatedWithValidCredential()
109
107
110
108
$ user = $ basicAccess ->authenticate ($ this ->request ->reveal ());
111
109
$ this ->assertInstanceOf (UserInterface::class, $ user );
112
- $ this ->assertEquals ('Aladdin ' , $ user ->getIdentity ());
113
110
}
114
111
115
112
public function testIsAuthenticatedWithNoCredential ()
@@ -151,7 +148,52 @@ public function testGetUnauthenticatedResponse()
151
148
152
149
$ response = $ basicAccess ->unauthorizedResponse ($ this ->request ->reveal ());
153
150
154
- $ this ->assertInstanceOf (ResponseInterface::class, $ response );
155
151
$ this ->assertEquals (['Basic realm="test" ' ], $ response ->getHeader ('WWW-Authenticate ' ));
156
152
}
153
+
154
+ public function provideInvalidAuthenticationHeader (): array
155
+ {
156
+ return [
157
+ 'empty-header ' => [[]],
158
+ 'missing-basic-prefix ' => [['foo ' ]],
159
+ 'only-username-without-colon ' => [['Basic ' . base64_encode ('Aladdin ' )]],
160
+ 'base64-encoded-pile-of-poo-emoji ' => [['Basic ' . base64_encode ('💩 ' )]],
161
+ 'pile-of-poo-emoji ' => [['Basic 💩 ' ]],
162
+ 'only-pile-of-poo-emoji ' => [['💩 ' ]],
163
+ 'basic-prefix-without-content ' => [['Basic ' ]],
164
+ 'only-basic ' => [['Basic ' ]],
165
+ 'multiple-auth-headers ' => [
166
+ [
167
+ ['Basic ' . base64_encode ('Aladdin:OpenSesame ' )],
168
+ ['Basic ' . base64_encode ('Aladdin:OpenSesame ' )],
169
+ ],
170
+ ],
171
+ ];
172
+ }
173
+
174
+ public function provideValidAuthentication (): array
175
+ {
176
+ return [
177
+ 'aladdin ' => ['Aladdin ' , 'OpenSesame ' , ['Basic ' . base64_encode ('Aladdin:OpenSesame ' )]],
178
+ 'aladdin-with-nonzero-array-index ' => [
179
+ 'Aladdin ' ,
180
+ 'OpenSesame ' ,
181
+ [-200 => 'Basic ' . base64_encode ('Aladdin:OpenSesame ' )]
182
+ ],
183
+ 'passwords-with-colon ' => ['Aladdin ' , 'Open:Sesame ' , ['Basic ' . base64_encode ('Aladdin:Open:Sesame ' )]],
184
+ 'username-without-password ' => ['Aladdin ' , '' , ['Basic ' . base64_encode ('Aladdin: ' )]],
185
+ 'password-without-username ' => ['' , 'OpenSesame ' , ['Basic ' . base64_encode (':OpenSesame ' )]],
186
+ 'passwords-with-multiple-colons ' => [
187
+ 'Aladdin ' ,
188
+ '::Open:::Sesame:: ' ,
189
+ ['Basic ' . base64_encode ('Aladdin:::Open:::Sesame:: ' )]
190
+ ],
191
+ 'no-username-or-password ' => ['' , '' , ['Basic ' . base64_encode (': ' )]],
192
+ 'no-username-password-only-colons ' => ['' , ':::::: ' , ['Basic ' . base64_encode ('::::::: ' )]],
193
+ 'unicode-username-and-password ' => [
194
+ 'thumbsup-emoji-👍 ' ,
195
+ 'thumbsdown-emoji-👎 ' ,
196
+ ['Basic ' . base64_encode ('thumbsup-emoji-👍:thumbsdown-emoji-👎 ' )]],
197
+ ];
198
+ }
157
199
}
0 commit comments