3
3
4
4
use Authwave \Authenticator ;
5
5
use Authwave \Cipher ;
6
+ use Authwave \InsecureProtocolException ;
6
7
use Authwave \Token ;
8
+ use PHPUnit \Framework \MockObject \MockObject ;
7
9
use PHPUnit \Framework \TestCase ;
8
10
9
11
class AuthenticatorTest extends TestCase {
@@ -20,4 +22,48 @@ public function testGetAuthUriHostname() {
20
22
$ authUri
21
23
);
22
24
}
25
+
26
+ // All AuthUris MUST be served over HTTPS, with the one exception of localhost.
27
+ // But it should still default to HTTPS on localhost.
28
+ public function testGetAuthUriHostnameLocalhostHttpsByDefault () {
29
+ $ cipher = self ::createMock (Cipher::class);
30
+ $ token = self ::createMock (Token::class);
31
+ $ token ->method ("generateCipher " )
32
+ ->willReturn ($ cipher );
33
+
34
+ $ sut = new Authenticator ($ token , "localhost " );
35
+ $ authUri = $ sut ->getAuthUri ();
36
+ self ::assertStringStartsWith (
37
+ "https://localhost " ,
38
+ $ authUri
39
+ );
40
+ }
41
+
42
+ // We should be able to set the scheme to HTTP for localhost hostname only.
43
+ public function testGetAuthUriHostnameLocalhostHttpAllowed () {
44
+ $ cipher = self ::createMock (Cipher::class);
45
+ $ token = self ::createMock (Token::class);
46
+ $ token ->method ("generateCipher " )
47
+ ->willReturn ($ cipher );
48
+
49
+ $ sut = new Authenticator ($ token , "localhost " );
50
+ $ sut ->useLocalhostHttps (false );
51
+ $ authUri = $ sut ->getAuthUri ();
52
+ self ::assertStringStartsWith (
53
+ "http://localhost " ,
54
+ $ authUri
55
+ );
56
+ }
57
+
58
+ // We should NOT be able to set the scheme to HTTP for other hostnames.
59
+ public function testGetAuthUriHostnameNotLocalhostHttpNotAllowed () {
60
+ $ cipher = self ::createMock (Cipher::class);
61
+ $ token = self ::createMock (Token::class);
62
+ $ token ->method ("generateCipher " )
63
+ ->willReturn ($ cipher );
64
+
65
+ $ sut = new Authenticator ($ token , "localhost.com " );
66
+ self ::expectException (InsecureProtocolException::class);
67
+ $ sut ->useLocalhostHttps (false );
68
+ }
23
69
}
0 commit comments