-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.html
More file actions
125 lines (112 loc) · 4.59 KB
/
oauth.html
File metadata and controls
125 lines (112 loc) · 4.59 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OAuth Redirect</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
text-align: center;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
max-width: 400px;
margin: 50px auto;
}
.success {
color: #28a745;
}
.error {
color: #dc3545;
}
.version {
font-size: 13px;
color: #666;
margin-top: 6px;
}
</style>
</head>
<body>
<div class="container">
<h2>OAuth Authorization</h2>
<div id="version" class="version">Version: v2</div>
<div id="status">Processing authorization...</div>
</div>
<script>
(function() {
const statusDiv = document.getElementById('status');
const versionDiv = document.getElementById('version');
// DRY constants
const VERSION = '4';
const CLOSE_DELAY_MS = 200; // how long the success message is shown before window.close()
// Centralized visible text so the body content is DRY and easy to change
const TEXTS = {
TITLE: 'OAuth Authorization',
PROCESSING: 'Processing authorization...',
SUCCESS: '✓ Authorization successful!<br>You can close this window.',
ERROR_PREFIX: '✗ Authorization failed:<br>',
NO_DATA: '✗ No authorization data received',
ERROR_PROCESS: '✗ Error processing authorization:<br>'
};
// Set version from the constant (no runtime fetch)
try { if (versionDiv) versionDiv.textContent = 'Version: v' + VERSION; } catch(_) {}
// Use TEXTS to populate the page content (DRY)
try {
const titleEl = document.querySelector('.container h2');
if (titleEl) titleEl.textContent = TEXTS.TITLE;
if (statusDiv) statusDiv.innerHTML = TEXTS.PROCESSING;
} catch(_) {}
try {
// Parse URL fragment for access_token
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
const accessToken = params.get('access_token');
const error = params.get('error');
if (accessToken) {
statusDiv.innerHTML = '<div class="success">' + TEXTS.SUCCESS + '</div>';
// Send token back to parent window
if (window.opener) {
window.opener.postMessage({
access_token: accessToken,
expires_in: params.get('expires_in'),
token_type: params.get('token_type')
}, '*');
}
// Close after a brief delay (configured above)
setTimeout(() => {
try { window.close(); } catch(_) {}
}, CLOSE_DELAY_MS);
} else if (error) {
statusDiv.innerHTML = '<div class="error">' + TEXTS.ERROR_PREFIX + error + '</div>';
if (window.opener) {
window.opener.postMessage({
error: error,
error_description: params.get('error_description')
}, '*');
}
} else {
statusDiv.innerHTML = '<div class="error">' + TEXTS.NO_DATA + '</div>';
if (window.opener) {
window.opener.postMessage({
error: 'no_token_received'
}, '*');
}
}
} catch (e) {
statusDiv.innerHTML = '<div class="error">' + TEXTS.ERROR_PROCESS + e.message + '</div>';
if (window.opener) {
window.opener.postMessage({
error: 'processing_error',
error_description: e.message
}, '*');
}
}
})();
</script>
</body>
</html>