-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJWT.php
115 lines (88 loc) · 3.19 KB
/
JWT.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
<?php
require_once "init.php";
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit();
}
$username = $_SESSION['username'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input = filter_input(INPUT_POST, "input", FILTER_SANITIZE_SPECIAL_CHARS);
if (!empty($input)) {
try {
$parts = explode('.', $input);
if (count($parts) !== 3) {
throw new Exception("Invalid JWT structure.");
}
$payload = $parts[1];
$payload = str_replace(['-', '_'], ['+', '/'], $payload);
$decoded = base64_decode($payload);
if ($decoded === false) {
throw new Exception("Base64 decoding failed.");
}
$opposite = json_decode($decoded, false);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("JSON decoding error: " . json_last_error_msg());
}
$opposite = json_encode($opposite);
} catch (Exception $e) {
$opposite = json_encode(["error" => "Decoding Error: " . $e->getMessage()]);
}
// Display result
$display = "<div style='color: #00008B'><h2>Opposite:</h2><br> <h2>" . nl2br(htmlspecialchars($opposite)) . "</h2></div>";
try {
// Insert into database
global $connect;
$sql = "INSERT INTO jwt (username, encoded, decoded, date)
VALUES (:username, :input, :opposite, CURRENT_TIMESTAMP)";
$stmt = $connect->prepare($sql);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':input', $input);
$stmt->bindParam(':opposite', $opposite);
$stmt->execute();
} catch (exception $e) {
$display = "Error: " . $e->getMessage();
}
} else {
$display = "<div style='color: #00008B'><h2>Fatal Error.. Please retry</h2></div>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Consolidev | JWT</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="CSS/styles.css">
<link rel="stylesheet" href="CSS/JWT.css">
<script src="https://kit.fontawesome.com/d0af7889fc.js" crossorigin="anonymous"></script>
</head>
<body>
<?php include('header.php'); ?>
<main class="main-container">
<div class="form-container">
<form action="JWT.php" method="post">
<i class="fa-solid fa-key title-icon"></i>
<h1 class-"page-title">Decode JWT tokens</h1>
<label for="input" class="input-label">Enter JWT token:</label>
<input type="text" id="input" name="input" placeholder="Enter JWT token"
required class="input-field"><br><br>
<input type="submit" value="Decode" class="submit-btn">
</form>
</div>
<div class="form-display">
<a href="jwtHistory.php">
<button class="btn history-btn">View History</button>
</a>
<?php
if (!empty($display)) {
echo $display;
}
?>
</div>
</main>
<footer>
<p>© <span id="2024"></span> consoliDev. All Rights Reserved.</p>
</footer>
</body>
</html>