-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle.php
More file actions
105 lines (94 loc) · 3.74 KB
/
google.php
File metadata and controls
105 lines (94 loc) · 3.74 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
100
101
102
103
104
105
<?php
session_start();
global $conn;
require __DIR__ . '/vendor/autoload.php';
include_once 'send_email.php';
include_once 'connect.php';
//google connection
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/secret/client_secret.json'); //login credentials for Google connection
$client->setRedirectUri('http://localhost/TT-login/google.php');
$client->addScope(['openid', 'profile', 'email']);
if (isset($_GET['code'])) {
$accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']); // get access token from Google
if (isset($accessToken['error'])) {
unset($_GET['code']);
header('Location: login.php');
exit();
}
$accessToken = $client->getAccessToken();
$googleOAuthService = new Google_Service_Oauth2($client);
$userInfo = $googleOAuthService->userinfo->get();
$email = $userInfo->getEmail();
$name = strtolower($userInfo->getGivenName());
function generateRandomPassword($length = 12) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = '';
// as long as $i is smaller than 12 continue for loop for random numbers
for ($i = 0; $i < $length; $i++) {
$password .= $chars[random_int(0, strlen($chars) - 1)];
}
return $password;
}
function generate_user_secret($length = 16) {
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; // Base32 secret
$secret = '';
for ($i = 0; $i < $length; $i++) {
$secret .= $characters[rand(0, strlen($characters) - 1)];
}
return $secret;
}
// create temp password for Google users
$randomPassword = generateRandomPassword();
$hashed_password = password_hash($randomPassword, PASSWORD_DEFAULT);
// create user_secret for authenticator qr
$user_secret = generate_user_secret();
// Check if the email already exists in the database
$checkUserQuery = "SELECT * FROM user WHERE email = ?";
$checkStmt = $conn->prepare($checkUserQuery);
$checkStmt->bind_param("s", $email);
$checkStmt->execute();
$checkResult = $checkStmt->get_result();
$userEmail = $email;
$password = $hashed_password;
$secret = $user_secret;
// checks if user already exists if so redirect with correct tokens
if ($checkResult->num_rows > 0) {
$_SESSION['access_token'] = $accessToken;
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $userEmail;
$_SESSION['password'] = $password;
header("Location: auth_redirect.php");
exit();
} else {
// user doesn't exist create new user
// Insert the user into the database
$insertSql = "INSERT INTO user (username, display_username, email, password, secret) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($insertSql);
if (!$stmt) {
die("Error preparing statement: " . $conn->error);
}
if (!$stmt->bind_param("sssss", $userEmail, $name, $userEmail, $password, $secret)) {
die("Error binding parameters: " . $stmt->error);
}
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error executing query: " . $stmt->error;
}
$stmt->close();
}
// close database connection
$conn->close();
$_SESSION['access_token'] = $accessToken;
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $userEmail;
$_SESSION['password'] = $password;
// send email to Google user with temp password (optional to use)
sendEmail($email, $randomPassword, $name);
header("Location: auth_redirect.php");
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
exit();
}