mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-27 10:42:45 +03:00
104f84f252
As discussed with the product team we want to enforce kebab-case file names for all files, with the exception of files which export a single class, in which case they should be PascalCase and reflect the class which they export. This will help find classes faster, and should push better naming for them too. Some files and packages have been excluded from this linting, specifically when a library or framework depends on the naming of a file for the functionality e.g. Ember, knex-migrator, adapter-manager
95 lines
2.7 KiB
JavaScript
95 lines
2.7 KiB
JavaScript
const jwt = require('jsonwebtoken');
|
|
const keypair = require('keypair');
|
|
const MembersSSR = require('./');
|
|
const keys = keypair();
|
|
|
|
const membersApiInstance = {
|
|
/**
|
|
* @param {string} token
|
|
*/
|
|
async getMemberDataFromMagicLinkToken(token) {
|
|
return jwt.decode(token);
|
|
},
|
|
async getMemberIdentityData() {
|
|
return {name: 'egg'};
|
|
}
|
|
};
|
|
|
|
const membersSSR = MembersSSR({
|
|
cookieSecure: false, // Secure cookie (default)
|
|
cookieKeys: ['some-coole-secret'], // Key to sign cookie with
|
|
getMembersApi: () => membersApiInstance // Used to fetch data and verify tokens
|
|
});
|
|
|
|
const server = require('http').createServer(async (req, res) => {
|
|
if (!req.method) {
|
|
res.writeHead(405);
|
|
return res.end('Method not allowed.');
|
|
}
|
|
if (req.method.toLowerCase() === 'post') {
|
|
try {
|
|
await membersSSR.exchangeTokenForSession(req, res);
|
|
res.writeHead(200);
|
|
res.end();
|
|
} catch (err) {
|
|
res.writeHead(err.statusCode, {
|
|
'Content-Type': 'text/plain;charset=UTF-8'
|
|
});
|
|
res.end(err.message);
|
|
}
|
|
} else {
|
|
try {
|
|
const member = await membersSSR.getMemberDataFromSession(req, res);
|
|
res.writeHead(200, {
|
|
'Content-Type': 'application/json'
|
|
});
|
|
res.end(JSON.stringify(member));
|
|
} catch (err) {
|
|
res.writeHead(err.statusCode, {
|
|
'Content-Type': 'text/plain;charset=UTF-8'
|
|
});
|
|
res.end(err.message);
|
|
}
|
|
}
|
|
});
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
const addressInfo = server.address();
|
|
if (addressInfo === null || typeof addressInfo === 'string') {
|
|
throw new TypeError({
|
|
message: `Unexpected return value from server.address(): ${addressInfo}`
|
|
});
|
|
}
|
|
const {address, port} = addressInfo;
|
|
const url = `http://${address}:${port}`;
|
|
|
|
const token = jwt.sign({
|
|
name: 'egg',
|
|
email: 'egg@mast.er'
|
|
}, keys.private, {
|
|
issuer: 'example.com',
|
|
audience: 'example.com',
|
|
algorithm: 'RS512'
|
|
});
|
|
|
|
require('http').request(`${url}?token=${token}`, {
|
|
method: 'post'
|
|
}, (loginResponse) => {
|
|
const cookies = loginResponse.headers['set-cookie'] || [];
|
|
require('http').request(url, {
|
|
headers: {
|
|
cookie: cookies.join('; ')
|
|
}
|
|
}, (authResponse) => {
|
|
authResponse.pipe(process.stdout);
|
|
authResponse.on('close', () => {
|
|
server.close();
|
|
});
|
|
}).end();
|
|
}).end();
|
|
});
|
|
|
|
server.on('close', () => {
|
|
process.exit(1);
|
|
});
|