2019-04-05 10:06:23 +03:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const keypair = require('keypair');
|
|
|
|
const MembersSSR = require('./');
|
|
|
|
const keys = keypair();
|
|
|
|
|
|
|
|
const membersApiInstance = {
|
2019-09-16 08:09:39 +03:00
|
|
|
/**
|
|
|
|
* @param {string} token
|
|
|
|
*/
|
|
|
|
async getMemberDataFromMagicLinkToken(token) {
|
|
|
|
return jwt.decode(token);
|
2019-04-05 10:06:23 +03:00
|
|
|
},
|
2019-09-16 08:09:39 +03:00
|
|
|
async getMemberIdentityData() {
|
|
|
|
return {name: 'egg'};
|
2019-04-05 10:06:23 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-16 08:09:39 +03:00
|
|
|
const membersSSR = MembersSSR({
|
2019-04-05 10:06:23 +03:00
|
|
|
cookieSecure: false, // Secure cookie (default)
|
|
|
|
cookieKeys: ['some-coole-secret'], // Key to sign cookie with
|
2019-09-16 08:09:39 +03:00
|
|
|
getMembersApi: () => membersApiInstance // Used to fetch data and verify tokens
|
2019-04-05 10:06:23 +03:00
|
|
|
});
|
|
|
|
|
2019-09-16 08:09:39 +03:00
|
|
|
const server = require('http').createServer(async (req, res) => {
|
|
|
|
if (!req.method) {
|
|
|
|
res.writeHead(405);
|
|
|
|
return res.end('Method not allowed.');
|
|
|
|
}
|
2019-04-05 10:06:23 +03:00
|
|
|
if (req.method.toLowerCase() === 'post') {
|
2019-09-16 08:09:39 +03:00
|
|
|
try {
|
|
|
|
await membersSSR.exchangeTokenForSession(req, res);
|
2019-04-05 10:06:23 +03:00
|
|
|
res.writeHead(200);
|
|
|
|
res.end();
|
2019-09-16 08:09:39 +03:00
|
|
|
} catch (err) {
|
2019-04-11 17:26:51 +03:00
|
|
|
res.writeHead(err.statusCode);
|
2019-04-05 10:06:23 +03:00
|
|
|
res.end(err.message);
|
2019-09-16 08:09:39 +03:00
|
|
|
}
|
2019-04-05 10:06:23 +03:00
|
|
|
} else {
|
2019-09-16 08:09:39 +03:00
|
|
|
try {
|
|
|
|
const member = await membersSSR.getMemberDataFromSession(req, res);
|
2019-04-05 10:06:23 +03:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
});
|
|
|
|
res.end(JSON.stringify(member));
|
2019-09-16 08:09:39 +03:00
|
|
|
} catch (err) {
|
2019-04-11 17:26:51 +03:00
|
|
|
res.writeHead(err.statusCode);
|
2019-04-05 10:06:23 +03:00
|
|
|
res.end(err.message);
|
2019-09-16 08:09:39 +03:00
|
|
|
}
|
2019-04-05 10:06:23 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
2019-09-16 08:09:39 +03:00
|
|
|
const addressInfo = server.address();
|
|
|
|
if (addressInfo === null || typeof addressInfo === 'string') {
|
2022-02-15 15:27:22 +03:00
|
|
|
throw new TypeError(`Unexpected return value from server.address(): ${addressInfo}`); /* eslint-disable-line no-restricted-syntax */
|
2019-09-16 08:09:39 +03:00
|
|
|
}
|
|
|
|
const {address, port} = addressInfo;
|
2019-04-05 10:06:23 +03:00
|
|
|
const url = `http://${address}:${port}`;
|
2019-09-16 08:09:39 +03:00
|
|
|
|
|
|
|
const token = jwt.sign({
|
|
|
|
name: 'egg',
|
|
|
|
email: 'egg@mast.er'
|
|
|
|
}, keys.private, {
|
2019-04-05 10:06:23 +03:00
|
|
|
issuer: 'example.com',
|
2019-04-11 17:26:51 +03:00
|
|
|
audience: 'example.com',
|
2019-04-05 10:06:23 +03:00
|
|
|
algorithm: 'RS512'
|
|
|
|
});
|
|
|
|
|
2019-09-16 08:09:39 +03:00
|
|
|
require('http').request(`${url}?token=${token}`, {
|
|
|
|
method: 'post'
|
2021-07-14 14:03:37 +03:00
|
|
|
}, (loginResponse) => {
|
|
|
|
const cookies = loginResponse.headers['set-cookie'] || [];
|
2019-04-05 10:06:23 +03:00
|
|
|
require('http').request(url, {
|
|
|
|
headers: {
|
2019-09-16 08:09:39 +03:00
|
|
|
cookie: cookies.join('; ')
|
2019-04-05 10:06:23 +03:00
|
|
|
}
|
2021-07-14 14:03:37 +03:00
|
|
|
}, (authResponse) => {
|
|
|
|
authResponse.pipe(process.stdout);
|
|
|
|
authResponse.on('close', () => {
|
2019-04-05 10:06:23 +03:00
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
}).end();
|
2019-09-16 08:09:39 +03:00
|
|
|
}).end();
|
2019-04-05 10:06:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
server.on('close', () => {
|
|
|
|
process.exit(1);
|
|
|
|
});
|