2018-11-07 17:10:07 +07:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const should = require('should');
|
2020-05-26 19:10:29 +01:00
|
|
|
const {UnauthorizedError} = require('@tryghost/errors');
|
2020-03-30 16:26:47 +01:00
|
|
|
const members = require('../../../../../core/server/services/auth/members');
|
2018-11-07 17:10:07 +07:00
|
|
|
|
2018-12-11 15:18:07 +07:00
|
|
|
describe.skip('Auth Service - Members', function () {
|
2018-11-07 17:10:07 +07:00
|
|
|
it('exports an authenticateMembersToken method', function () {
|
|
|
|
const actual = typeof members.authenticateMembersToken;
|
|
|
|
const expected = 'function';
|
|
|
|
should.equal(actual, expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('authenticateMembersToken', function () {
|
|
|
|
it('calls next without an error if there is no authorization header', function () {
|
|
|
|
members.authenticateMembersToken({
|
2019-07-05 13:40:43 +02:00
|
|
|
get() {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-07 17:10:07 +07:00
|
|
|
}, {}, function next(err) {
|
|
|
|
const actual = err;
|
|
|
|
const expected = undefined;
|
|
|
|
|
|
|
|
should.equal(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls next without an error if the authorization header does not match the GhostMembers scheme', function () {
|
|
|
|
members.authenticateMembersToken({
|
2019-07-05 13:40:43 +02:00
|
|
|
get() {
|
|
|
|
return 'DodgyScheme credscredscreds';
|
|
|
|
}
|
2018-11-07 17:10:07 +07:00
|
|
|
}, {}, function next(err) {
|
|
|
|
const actual = err;
|
|
|
|
const expected = undefined;
|
|
|
|
|
|
|
|
should.equal(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('attempts to verify the credentials as a JWT, allowing the "NONE" algorithm', function () {
|
|
|
|
it('calls next with an UnauthorizedError if the verification fails', function () {
|
|
|
|
members.authenticateMembersToken({
|
2019-07-05 13:40:43 +02:00
|
|
|
get() {
|
|
|
|
return 'GhostMembers notafuckentoken';
|
|
|
|
}
|
2018-11-07 17:10:07 +07:00
|
|
|
}, {}, function next(err) {
|
|
|
|
const actual = err instanceof UnauthorizedError;
|
|
|
|
const expected = true;
|
|
|
|
|
|
|
|
should.equal(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('calls next without an error after attaching the JWT claims to req.member if the verification suceeds', function () {
|
|
|
|
const claims = {
|
|
|
|
rumpel: 'stiltskin'
|
|
|
|
};
|
|
|
|
const token = jwt.sign(claims, null, {
|
|
|
|
algorithm: 'none'
|
|
|
|
});
|
|
|
|
const req = {
|
2019-07-05 13:40:43 +02:00
|
|
|
get() {
|
|
|
|
return `GhostMembers ${token}`;
|
|
|
|
}
|
2018-11-07 17:10:07 +07:00
|
|
|
};
|
|
|
|
members.authenticateMembersToken(req, {}, function next(err) {
|
|
|
|
should.equal(err, undefined);
|
|
|
|
|
|
|
|
const actual = req.member.rumpel;
|
|
|
|
const expected = claims.rumpel;
|
|
|
|
|
|
|
|
should.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|