Skip to content

Commit

Permalink
feat: stripping out all jwt work as it was an unused experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Jul 9, 2020
1 parent 2e1bc33 commit f8b1969
Show file tree
Hide file tree
Showing 8 changed files with 1,611 additions and 6,225 deletions.
7,604 changes: 1,610 additions & 5,994 deletions package-lock.json

Large diffs are not rendered by default.

41 changes: 0 additions & 41 deletions packages/node/__tests__/create-jwt-link.test.js

This file was deleted.

39 changes: 0 additions & 39 deletions packages/node/__tests__/get-readme-data.test.js

This file was deleted.

69 changes: 0 additions & 69 deletions packages/node/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,73 +126,4 @@ describe('#metrics', () => {
mock.done();
}, 5000);
});

describe('#login', () => {
beforeEach(() => {
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
});

afterEach(() => nock.cleanAll());

it('should error if missing apiKey', () => {
expect(() => {
middleware.login();
}).toThrow('You must provide your ReadMe API key');
});

it('should error if missing user function', () => {
expect(() => {
middleware.login(apiKey);
}).toThrow('You must provide a function to get the user');
});

describe('redirect flows', () => {
beforeEach(() => {
nock(config.readmeUrl)
.get('/api/v1/')
.basicAuth({
user: apiKey,
pass: '',
})
.reply(200, { jwtSecret: 'jwt', baseUrl: 'http://readme.readme.io' });
});

it('should redirect if no user', () => {
const app = express();

app.get(
'/readme',
middleware.login(apiKey, () => false, {
loginUrl: '/login',
})
);

return request(app)
.get('/readme')
.expect(302)
.expect(res => {
expect(res.header.location.startsWith('/login?redirect=')).toBe(true);
});
});

it('should redirect to readme if user', () => {
const app = express();

app.get(
'/readme',
middleware.login(apiKey, () => ({ name: 'marc' }), {
loginUrl: '/login',
})
);

return request(app)
.get('/readme')
.expect(302)
.expect(res => {
expect(res.header.location.startsWith('http://readme.readme.io?auth_token=')).toBe(true);
});
});
});
});
});
32 changes: 0 additions & 32 deletions packages/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const request = require('r2');
const config = require('./config');

const constructPayload = require('./lib/construct-payload');
const createJWTLink = require('./lib/create-jwt-link');
const getReadmeData = require('./lib/get-readme-data');

// We're doing this to buffer up the response body
// so we can send it off to the metrics server
Expand Down Expand Up @@ -73,33 +71,3 @@ module.exports.metrics = (apiKey, group, options = {}) => {
return next();
};
};

module.exports.login = (apiKey, getUser, options = {}) => {
if (!apiKey) throw new Error('You must provide your ReadMe API key');
if (!getUser) throw new Error('You must provide a function to get the user');

// Make sure api key is valid
getReadmeData(apiKey);

return async (req, res, next) => {
let u;
try {
u = getUser(req);
} catch (e) {
// User isn't logged in
}

if (!u) {
const domain = req.headers['x-forwarded-host'] || req.get('host');
const fullUrl = `${req.protocol}://${domain}${req.originalUrl}`;
return res.redirect(`${options.loginUrl}?redirect=${encodeURIComponent(fullUrl)}`);
}

try {
const jwtUrl = await createJWTLink(apiKey, u, req.query.redirect);
return res.redirect(jwtUrl);
} catch (e) {
return next(e);
}
};
};
24 changes: 0 additions & 24 deletions packages/node/lib/create-jwt-link.js

This file was deleted.

23 changes: 0 additions & 23 deletions packages/node/lib/get-readme-data.js

This file was deleted.

4 changes: 1 addition & 3 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
},
"dependencies": {
"content-type": "^1.0.4",
"jsonwebtoken": "^8.3.0",
"lodash": "^4.17.15",
"r2": "^2.0.1",
"uuid": "^8.1.0"
"r2": "^2.0.1"
},
"scripts": {
"lint": "eslint .",
Expand Down

0 comments on commit f8b1969

Please sign in to comment.