|
7 | 7 | SessionUtils::enforceLogin(); |
8 | 8 |
|
9 | 9 | if (Configuration::genGoogleOAuthFileExists()) { |
| 10 | + |
| 11 | + $code = idx(Utils::getGET(), 'code', false); |
| 12 | + $error = idx(Utils::getGET(), 'error', false); |
| 13 | + $state = idx(Utils::getGET(), 'state', false); |
| 14 | + |
10 | 15 | $google_oauth_file = Configuration::genGoogleOAuthFile(); |
11 | 16 | $client = new Google_Client(); |
12 | 17 | $client->setAuthConfig($google_oauth_file); |
|
16 | 21 | 'https://'.$_SERVER['HTTP_HOST'].'/data/google_oauth.php', |
17 | 22 | ); |
18 | 23 |
|
19 | | - if (isset($_GET['code'])) { |
20 | | - $client->authenticate($_GET['code']); |
| 24 | + $integration_csrf_token = base64_encode(random_bytes(100)); |
| 25 | + // Cookie is sent with headers, and therefore not set until after the PHP code executes - this allows us to reset the cookie on each request without clobbering the state |
| 26 | + setcookie( |
| 27 | + 'integration_csrf_token', |
| 28 | + strval($integration_csrf_token), |
| 29 | + 0, |
| 30 | + '/data/', |
| 31 | + must_have_string(Utils::getSERVER(), 'SERVER_NAME'), |
| 32 | + true, |
| 33 | + true, |
| 34 | + ); |
| 35 | + $client->setState(strval($integration_csrf_token)); |
| 36 | + |
| 37 | + if ($code !== false) { |
| 38 | + $integration_csrf_token = /* HH_IGNORE_ERROR[2050] */ |
| 39 | + idx($_COOKIE, 'integration_csrf_token', false); |
| 40 | + if (strval($integration_csrf_token) === '' || |
| 41 | + strval($state) === '' || |
| 42 | + strval($integration_csrf_token) != strval($state)) { |
| 43 | + $code = false; |
| 44 | + $error = false; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if ($code !== false) { |
| 49 | + $client->authenticate($code); |
21 | 50 | $access_token = $client->getAccessToken(); |
22 | 51 | $oauth_client = new Google_Service_Oauth2($client); |
23 | 52 | $profile = $oauth_client->userinfo->get(); |
|
49 | 78 | '"'; |
50 | 79 | } |
51 | 80 | $javascript_close = "window.open('', '_self', ''); window.close();"; |
52 | | - } else if (isset($_GET['error'])) { |
| 81 | + } else if ($error === true) { |
53 | 82 | $message = |
54 | 83 | tr( |
55 | 84 | 'There was an error connecting your account to Google, please try again later.', |
|
0 commit comments