Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Session;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Url;
|
Chris@0
|
6 use Drupal\basic_auth\Tests\BasicAuthTestTrait;
|
Chris@0
|
7 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests if sessions are correctly handled when a user authenticates.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group Session
|
Chris@0
|
13 */
|
Chris@0
|
14 class SessionAuthenticationTest extends WebTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 use BasicAuthTestTrait;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * A test user.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var \Drupal\user\Entity\User
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $user;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * {@inheritdoc}
|
Chris@0
|
27 */
|
Chris@0
|
28 public static $modules = ['basic_auth', 'session_test'];
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * {@inheritdoc}
|
Chris@0
|
32 */
|
Chris@0
|
33 protected function setUp() {
|
Chris@0
|
34 parent::setUp();
|
Chris@0
|
35
|
Chris@0
|
36 // Create a test administrator user.
|
Chris@0
|
37 $this->user = $this->drupalCreateUser(['administer site configuration']);
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Check that a basic authentication session does not leak.
|
Chris@0
|
42 *
|
Chris@0
|
43 * Regression test for a bug that caused a session initiated by basic
|
Chris@0
|
44 * authentication to persist over subsequent unauthorized requests.
|
Chris@0
|
45 */
|
Chris@0
|
46 public function testSessionFromBasicAuthenticationDoesNotLeak() {
|
Chris@0
|
47 // This route is authorized through basic_auth only, not cookie.
|
Chris@0
|
48 $protected_url = Url::fromRoute('session_test.get_session_basic_auth');
|
Chris@0
|
49
|
Chris@0
|
50 // This route is not protected.
|
Chris@0
|
51 $unprotected_url = Url::fromRoute('session_test.get_session_no_auth');
|
Chris@0
|
52
|
Chris@0
|
53 // Test that the route is not accessible as an anonymous user.
|
Chris@0
|
54 $this->drupalGet($protected_url);
|
Chris@0
|
55 $this->assertResponse(401, 'An anonymous user cannot access a route protected with basic authentication.');
|
Chris@0
|
56
|
Chris@0
|
57 // We should be able to access the route with basic authentication.
|
Chris@0
|
58 $this->basicAuthGet($protected_url, $this->user->getUsername(), $this->user->pass_raw);
|
Chris@0
|
59 $this->assertResponse(200, 'A route protected with basic authentication can be accessed by an authenticated user.');
|
Chris@0
|
60
|
Chris@0
|
61 // Check that the correct user is logged in.
|
Chris@0
|
62 $this->assertEqual($this->user->id(), json_decode($this->getRawContent())->user, 'The correct user is authenticated on a route with basic authentication.');
|
Chris@0
|
63
|
Chris@0
|
64 // If we now try to access a page without basic authentication then we
|
Chris@0
|
65 // should no longer be logged in.
|
Chris@0
|
66 $this->drupalGet($unprotected_url);
|
Chris@0
|
67 $this->assertResponse(200, 'An unprotected route can be accessed without basic authentication.');
|
Chris@0
|
68 $this->assertFalse(json_decode($this->getRawContent())->user, 'The user is no longer authenticated after visiting a page without basic authentication.');
|
Chris@0
|
69
|
Chris@0
|
70 // If we access the protected page again without basic authentication we
|
Chris@0
|
71 // should get 401 Unauthorized.
|
Chris@0
|
72 $this->drupalGet($protected_url);
|
Chris@0
|
73 $this->assertResponse(401, 'A subsequent request to the same route without basic authentication is not authorized.');
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Tests if a session can be initiated through basic authentication.
|
Chris@0
|
78 */
|
Chris@0
|
79 public function testBasicAuthSession() {
|
Chris@0
|
80 // Set a session value on a request through basic auth.
|
Chris@0
|
81 $test_value = 'alpaca';
|
Chris@0
|
82 $response = $this->basicAuthGet('session-test/set-session/' . $test_value, $this->user->getUsername(), $this->user->pass_raw);
|
Chris@0
|
83 $this->assertSessionData($response, $test_value);
|
Chris@0
|
84 $this->assertResponse(200, 'The request to set a session value was successful.');
|
Chris@0
|
85
|
Chris@0
|
86 // Test that on a subsequent request the session value is still present.
|
Chris@0
|
87 $response = $this->basicAuthGet('session-test/get-session', $this->user->getUsername(), $this->user->pass_raw);
|
Chris@0
|
88 $this->assertSessionData($response, $test_value);
|
Chris@0
|
89 $this->assertResponse(200, 'The request to get a session value was successful.');
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Checks the session data returned by the session test routes.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param string $response
|
Chris@0
|
96 * A response object containing the session values and the user ID.
|
Chris@0
|
97 * @param string $expected
|
Chris@0
|
98 * The expected session value.
|
Chris@0
|
99 */
|
Chris@0
|
100 protected function assertSessionData($response, $expected) {
|
Chris@0
|
101 $response = json_decode($response, TRUE);
|
Chris@0
|
102 $this->assertEqual(['test_value' => $expected], $response['session'], 'The session data matches the expected value.');
|
Chris@0
|
103
|
Chris@0
|
104 // Check that we are logged in as the correct user.
|
Chris@0
|
105 $this->assertEqual($this->user->id(), $response['user'], 'The correct user is logged in.');
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Tests that a session is not started automatically by basic authentication.
|
Chris@0
|
110 */
|
Chris@0
|
111 public function testBasicAuthNoSession() {
|
Chris@0
|
112 // A route that is authorized through basic_auth only, not cookie.
|
Chris@0
|
113 $no_cookie_url = Url::fromRoute('session_test.get_session_basic_auth');
|
Chris@0
|
114
|
Chris@0
|
115 // A route that is authorized with standard cookie authentication.
|
Chris@0
|
116 $cookie_url = '<front>';
|
Chris@0
|
117
|
Chris@0
|
118 // If we authenticate with a third party authentication system then no
|
Chris@0
|
119 // session cookie should be set, the third party system is responsible for
|
Chris@0
|
120 // sustaining the session.
|
Chris@0
|
121 $this->basicAuthGet($no_cookie_url, $this->user->getUsername(), $this->user->pass_raw);
|
Chris@0
|
122 $this->assertResponse(200, 'The user is successfully authenticated using basic authentication.');
|
Chris@0
|
123 $this->assertFalse($this->drupalGetHeader('set-cookie', TRUE), 'No cookie is set on a route protected with basic authentication.');
|
Chris@0
|
124
|
Chris@0
|
125 // On the other hand, authenticating using Cookie sets a cookie.
|
Chris@0
|
126 $edit = ['name' => $this->user->getUsername(), 'pass' => $this->user->pass_raw];
|
Chris@0
|
127 $this->drupalPostForm($cookie_url, $edit, t('Log in'));
|
Chris@0
|
128 $this->assertResponse(200, 'The user is successfully authenticated using cookie authentication.');
|
Chris@0
|
129 $this->assertTrue($this->drupalGetHeader('set-cookie', TRUE), 'A cookie is set on a route protected with cookie authentication.');
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 }
|