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