-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
82 lines (71 loc) · 3.35 KB
/
Copy pathauth.php
File metadata and controls
82 lines (71 loc) · 3.35 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
<?php
$server = 'mysql-volleycoachpro.alwaysdata.net';
$login = '403542';
$mdp = 'Iutinfo!';
$db = 'volleycoachpro_auth';
try {
// Connexion au serveur MySQL
$linkpdo = new PDO("mysql:host=$server;dbname=$db;charset=utf8mb4", $login, $mdp);
$linkpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
require_once 'jwt_utils.php'; // Inclusion du fichier jwt_utils.php
header("Content-Type: application/json"); // Réponse en JSON
$secret_key = "cyberquantiquebattlepassiciel"; // Remplace par une clé secrète sécurisée
// Récupération de la méthode HTTP
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'POST') {
// Récupération des données JSON envoyées
$input_data = json_decode(file_get_contents("php://input"), true);
if (isset($input_data['login']) && isset($input_data['password'])) {
$login = $input_data['login'];
$password = $input_data['password'];
$stmt = $linkpdo->prepare("SELECT * FROM Utilisateur WHERE Login = :login");
$stmt->execute([':login' => $login]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) { // Exemple fictif
$hashedInput = hash_hmac('sha256', $password, 'ripbozo');
if ($hashedInput === $user['Mdp']) {
// Génération du JWT
$headers = ['alg' => 'HS256', 'typ' => 'JWT'];
$payload = [
'login' => $login,
'exp' => time() + 3600 // Expire dans 1 heure
];
$jwt = generate_jwt($headers, $payload, $secret_key);
echo json_encode(['token' => $jwt,'user' => $user['Login']]);
} else {
http_response_code(401);
echo json_encode(['error' => 'Identifiants invalides']);
}
} else {
http_response_code(401);
echo json_encode(['error' => 'Identifiants invalides']);
}
} else {
http_response_code(400);
echo json_encode(['error' => 'Données manquantes']);
}
} elseif ($method === 'GET') {
// Vérification du token pour accéder aux ressources
$token = get_bearer_token();
if ($token && is_jwt_valid($token, $secret_key)) {
// Décoder le token pour obtenir le login
$parts = explode('.', $token);
$payload = json_decode(base64_decode($parts[1]), true);
$userLogin = $payload['login'];
echo json_encode([
'message' => 'Accès autorisé',
'login' => $userLogin,
'status' => 'success'
]);
} else {
http_response_code(403);
echo json_encode(['error' => 'Accès refusé']);
}
} else {
http_response_code(405);
echo json_encode(['error' => 'Méthode non autorisée']);
}
} catch (PDOException $e) {
die('Erreur : '.$e->getMessage()); // Afficher une erreur explicite
}
?>