-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_id_token.html
130 lines (114 loc) · 5.35 KB
/
index_id_token.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Callback URL Page</title>
<!-- Bootstrap CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
</head>
<body>
<div class="container mt-5">
<h2>JWT Token Check</h2>
<div id="tokenInfo"></div>
<button class="btn btn-primary" onclick="downloadPDF()">Print as PDF</button>
<div id="otherFields"></div>
</div>
<script>
$(document).ready(function() {
// Function to parse JWT token
function parseJwt(token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
};
// Function to parse hash parameters
function parseHashParams(hash) {
var params = {};
var regex = /([^&;=]+)=?([^&;]*)/g;
var matches;
while (matches = regex.exec(hash)) {
params[decodeURIComponent(matches[1])] = decodeURIComponent(matches[2]);
}
return params;
}
// Extract hash parameters
var hashParams = parseHashParams(window.location.hash.substring(1));
var token = hashParams['id_token'];
if (token) {
var payload = parseJwt(token);
var requiredAttributes = ['email', 'given_name', 'family_name'];
var optionalAttributes = ['custom:groups', 'custom:apps'];
var allRequiredPresent = true;
requiredAttributes.forEach(function(attr) {
var isPresent = payload[attr] ? true : false;
allRequiredPresent = allRequiredPresent && isPresent;
var statusClass = isPresent ? 'alert-success' : 'alert-danger';
var statusText = isPresent ? `Present [ ${payload[attr]} ]` : 'Missing or Empty';
$('#tokenInfo').append(
`<div class="alert ${statusClass}" role="alert">
<strong>${attr}:</strong> ${statusText}
</div>`
);
});
optionalAttributes.forEach(function(attr) {
var isPresent = payload[attr] ? true : false;
var statusClass = isPresent ? 'alert-warning' : 'alert-secondary';
var statusText = isPresent ? `Present [ ${payload[attr]} ]` : 'Not Provided';
$('#tokenInfo').append(
`<div class="alert ${statusClass}" role="alert">
<strong>${attr}:</strong> ${statusText}
</div>`
);
});
if (allRequiredPresent) {
$('#tokenInfo').append(
'<div class="alert alert-success" role="alert"><strong>Success:</strong> All required attributes are present.</div>'
);
}
else{
$('#tokenInfo').append(
'<div class="alert alert-danger" role="alert"><strong>Error:</strong> NOT all required attributes are present.</div>'
);
}
// Displaying other fields in alphabetical order
$('#otherFields').append('<h4>Other Fields:</h4>');
var otherKeys = Object.keys(payload).filter(function(key) {
return !requiredAttributes.includes(key) && !optionalAttributes.includes(key);
}).sort();
// Sorting the keys alphabetically
otherKeys.forEach(function(key) {
$('#otherFields').append(
`<div class="alert alert-info" role="alert">
<strong>${key}:</strong> ${payload[key]}
</div>`
);
});
} else {
$('#tokenInfo').append(
'<div class="alert alert-warning" role="alert">No ID Token found in URL fragment.</div>'
);
}
});
function downloadPDF() {
// const { jsPDF } = window.jspdf;
// const doc = new jsPDF();
//
// // Example: Capturing and adding the whole body's content
// const content = document.body.innerHTML;
// doc.text(content, 10, 10);
// doc.save('download.pdf');
window.print();
}
</script>
<!-- Bootstrap Bundle with Popper -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
</body>
</html>