Repository navigation
Expand file tree
/
Copy pathtoken.php
More file actions
99 lines (81 loc) · 3.37 KB
/
Copy pathtoken.php
File metadata and controls
99 lines (81 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
include_once 'config.php';
require_once __DIR__ . '/google-oauth.php';
require_once __DIR__ . '/google-identity.php';
require_once __DIR__ . '/google-health.php';
function googleOAuthCallbackFailure(string $logMessage): never
{
unset($_SESSION['google_oauth_state'], $_SESSION['google_oauth_code_verifier']);
error_log($logMessage);
http_response_code(400);
exit('Google Health authorisation failed. Please return to the home page and try again.');
}
$error = $_GET['error'] ?? null;
if (is_string($error) && $error !== '') {
googleOAuthCallbackFailure('Google OAuth returned error: ' . $error);
}
$state = $_GET['state'] ?? null;
$expectedState = $_SESSION['google_oauth_state'] ?? null;
if (!is_string($state) || !is_string($expectedState) || $state === '' || $expectedState === '' || !hash_equals($expectedState, $state)) {
googleOAuthCallbackFailure('Google OAuth state validation failed.');
}
$codeVerifier = $_SESSION['google_oauth_code_verifier'] ?? null;
if (!is_string($codeVerifier) || $codeVerifier === '') {
googleOAuthCallbackFailure('Google OAuth PKCE verifier is unavailable.');
}
$code = $_GET['code'] ?? null;
if (!is_string($code) || $code === '') {
googleOAuthCallbackFailure('Google OAuth did not return an authorisation code.');
}
unset($_SESSION['google_oauth_state'], $_SESSION['google_oauth_code_verifier']);
try {
$client = createGoogleOAuthClient($config_redirect_uri);
$token = $client->fetchAccessTokenWithAuthCode($code, $codeVerifier);
$tokenError = $token['error'] ?? null;
if (is_string($tokenError) && $tokenError !== '') {
$description = $token['error_description'] ?? $tokenError;
throw new RuntimeException('Google OAuth token exchange failed: ' . (string) $description);
}
$accessToken = $token['access_token'] ?? null;
if (!is_string($accessToken) || $accessToken === '') {
throw new UnexpectedValueException('Google did not return an access token.');
}
$refreshToken = $token['refresh_token'] ?? null;
if (!is_string($refreshToken) || $refreshToken === '') {
throw new UnexpectedValueException('Google did not return a refresh token.');
}
$identity = googleAuthenticatedIdentity($client, $token);
saveGoogleAuthenticatedUser($config_conn, $identity);
$client->setAccessToken($token);
$healthIdentity = googleHealthIdentity($client);
$healthUserId = $healthIdentity['healthUserId'];
$expectedHealthUserId = googleHealthUserIdForGoogleSub(
$config_conn,
$identity['googleSub']
);
if ($expectedHealthUserId === null) {
saveGoogleHealthUserId(
$config_conn,
$identity['googleSub'],
$healthUserId
);
} elseif (!hash_equals(
$expectedHealthUserId,
$healthUserId
)) {
throw new RuntimeException(
'Google Health identity does not match ' .
'the authenticated application user.'
);
}
saveGoogleOAuthToken($config_conn, $identity['googleSub'], $token);
session_regenerate_id(true);
setGoogleAuthenticatedSession($identity);
header('Location: data.php', true, 303);
exit;
} catch (Throwable $exception) {
error_log('Google OAuth callback failed: ' . $exception->getMessage());
http_response_code(500);
exit('Unable to complete Google Health authorisation.');
}