@1presence/bridge 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth.js +39 -33
- package/package.json +1 -1
package/dist/auth.js
CHANGED
|
@@ -70,41 +70,47 @@ function openBrowser(url) {
|
|
|
70
70
|
function runBrowserAuthFlow(gatewayUrl) {
|
|
71
71
|
return new Promise((resolve, reject) => {
|
|
72
72
|
const server = (0, http_1.createServer)((req, res) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
82
|
-
res.end(`<!DOCTYPE html>
|
|
83
|
-
<html>
|
|
84
|
-
<head><title>1Presence Bridge</title>
|
|
85
|
-
<style>
|
|
86
|
-
body { font-family: Georgia, serif; display:flex; align-items:center; justify-content:center;
|
|
87
|
-
min-height:100vh; margin:0; background:#faf8f4; color:#3a3028; }
|
|
88
|
-
.card { text-align:center; padding:40px; }
|
|
89
|
-
h1 { font-size:1.5rem; font-weight:400; margin:0 0 12px; }
|
|
90
|
-
p { color:#7a6a5a; font-size:0.9rem; }
|
|
91
|
-
</style></head>
|
|
92
|
-
<body><div class="card">
|
|
93
|
-
<h1>Bridge connected</h1>
|
|
94
|
-
<p>You can close this tab and return to your terminal.</p>
|
|
95
|
-
</div></body></html>`);
|
|
96
|
-
server.close();
|
|
97
|
-
const uid = uidFromToken(token);
|
|
98
|
-
const email = emailFromToken(token);
|
|
99
|
-
if (!uid) {
|
|
100
|
-
reject(new Error('Invalid token — missing uid'));
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
resolve({ token, uid, email });
|
|
73
|
+
// CORS headers so the PWA (https) can POST to http://localhost
|
|
74
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
75
|
+
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
|
|
76
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
77
|
+
if (req.method === 'OPTIONS') {
|
|
78
|
+
res.writeHead(204);
|
|
79
|
+
res.end();
|
|
80
|
+
return;
|
|
104
81
|
}
|
|
105
|
-
|
|
106
|
-
|
|
82
|
+
if (req.method !== 'POST') {
|
|
83
|
+
res.writeHead(405);
|
|
84
|
+
res.end();
|
|
85
|
+
return;
|
|
107
86
|
}
|
|
87
|
+
let body = '';
|
|
88
|
+
req.on('data', (chunk) => { body += chunk.toString(); });
|
|
89
|
+
req.on('end', () => {
|
|
90
|
+
try {
|
|
91
|
+
const { token } = JSON.parse(body);
|
|
92
|
+
if (!token) {
|
|
93
|
+
res.writeHead(400);
|
|
94
|
+
res.end();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
98
|
+
res.end(JSON.stringify({ ok: true }));
|
|
99
|
+
server.close();
|
|
100
|
+
const uid = uidFromToken(token);
|
|
101
|
+
const email = emailFromToken(token);
|
|
102
|
+
if (!uid) {
|
|
103
|
+
reject(new Error('Invalid token — missing uid'));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
resolve({ token, uid, email });
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
res.writeHead(400);
|
|
110
|
+
res.end();
|
|
111
|
+
reject(err);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
108
114
|
});
|
|
109
115
|
server.listen(0, '127.0.0.1', () => {
|
|
110
116
|
const { port } = server.address();
|