-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_callback.php
158 lines (138 loc) · 6.25 KB
/
github_callback.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
require_once 'config.php';
require_once 'vendor/autoload.php';
use League\OAuth2\Client\Provider\Github;
use PHPGangsta_GoogleAuthenticator;
$provider = new Github([
'clientId' => GITHUB_CLIENT_ID,
'clientSecret' => GITHUB_SECRET,
'redirectUri' => GITHUB_CALLBACK,
]);
if (isset($_GET['code'])) {
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// Получение данных пользователя от GitHub
$user = $provider->getResourceOwner($token);
$email = $user->getEmail(); // Получаем из GitHub объекта пользователя
// Подключение к базе данных
$mysqli = new mysqli(JSTORE_DBA_HOST, JSTORE_DBA_USER, JSTORE_DBA_PASS, JSTORE_DBA_DBSE); // to be rewritten
// Генерация случайного пароля и TOTP-секрета
$authenticator = new PHPGangsta_GoogleAuthenticator();
$randomPassword = bin2hex(random_bytes(6)); // Для простоты будем использовать 12 символов
$totpSecret = $authenticator->createSecret();
$qrCodeUrl = $authenticator->getQRCodeGoogleUrl(JSTORE_TOTP_ORGANISATION_KEY, $email, $totpSecret);
// Проверка существования пользователя
$stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
// Пользователь не существует, сохраняем данные для подтверждения регистрации
$_SESSION['temp_data'] = [
'success' => true,
'email' => $user->getEmail(),
'username' => $user->getNickname(),
'password' => $randomPassword,
'totpSecret' => $totpSecret,
'qrCodeUrl' => $qrCodeUrl,
'message' => 'Success'
];
} else {
// Пользователь существует
$userData = $result->fetch_assoc();
$_SESSION["user_id"] = $userData["id"];
setcookie("username", $userData['username'], time() + 3600, "/");
header('Location: /');
exit();
}
}
// Обработка подтверждения регистрации
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm'])) {
// Регистрация и добавление в базу данных
if (isset($_SESSION['temp_data'])) {
$tempData = $_SESSION['temp_data'];
$hashedPassword = password_hash($tempData['password'], PASSWORD_DEFAULT);
$mysqli = new mysqli(JSTORE_DBA_HOST, JSTORE_DBA_USER, JSTORE_DBA_PASS, JSTORE_DBA_DBSE); // to be rewritten
$stmt = $mysqli->prepare("INSERT INTO users (email, username, password, user_totp_secret) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $tempData['email'], $tempData['username'], $hashedPassword, $tempData['totpSecret']);
$stmt->execute();
if ($stmt->affected_rows > 0) {
$_SESSION["user_id"] = $mysqli->insert_id;
setcookie("username", $userData['username'], time() + 3600, "/");
unset($_SESSION['temp_data']);
header('Location: /');
} else {
echo "Не удалось зарегистрировать пользователя.";
}
$stmt->close();
}
$mysqli->close();
exit();
}
if (isset($_SESSION['temp_data'])): ?>
<?php $tempData = $_SESSION['temp_data']; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Confirm Registration</title>
<!-- Подключение CSS для стиля Windows 98 -->
<link rel="stylesheet" href="https://unpkg.com/98.css">
<style>
.window {
max-width: 600px;
margin: 0 auto;
}
.window-body {
padding: 20px;
}
.form-row {
margin-bottom: 10px;
}
input[type="text"],
input[type="email"] {
width: 100%;
}
</style>
</head>
<body>
<div class="window">
<div class="title-bar">
<div class="title-bar-text">Confirm Registration</div>
</div>
<div class="window-body">
<p>Please confirm your registration details:</p>
<form action="github_callback.php" method="post">
<div class="form-row">
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<?= htmlspecialchars($tempData['email']); ?>"
readonly>
</div>
<div class="form-row">
<label for="username">Username:</label>
<input type="text" id="username" name="username"
value="<?= htmlspecialchars($tempData['username']); ?>" readonly>
</div>
<div class="form-row">
<label for="password">Password:</label>
<input type="text" id="password" name="password"
value="<?= htmlspecialchars($tempData['password']); ?>" readonly>
</div>
<div class="form-row">
<label for="totpSecret">TOTP Secret:</label>
<input type="text" id="totpSecret" name="totpSecret"
value="<?= htmlspecialchars($tempData['totpSecret']); ?>" readonly>
</div>
<div class="form-row">
<img src="<?= htmlspecialchars($tempData['qrCodeUrl']) ?>" alt="TOTP QR Code">
</div>
<div class="form-row">
<button type="submit" name="confirm">Confirm Registration</button>
</div>
</form>
</div>
</div>
</body>
</html>
<?php endif; ?>