2018-11-07 13:10:07 +03:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const should = require('should');
|
2020-05-26 21:10:29 +03:00
|
|
|
const {UnauthorizedError} = require('@tryghost/errors');
|
2021-10-06 13:12:21 +03:00
|
|
|
const members = require('../../../../../../core/server/services/auth/members');
|
2018-11-07 13:10:07 +03:00
|
|
|
|
2021-05-24 13:36:35 +03:00
|
|
|
describe('Auth Service - Members', function () {
|
2018-11-07 13:10:07 +03: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 14:40:43 +03:00
|
|
|
get() {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-07 13:10:07 +03: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 14:40:43 +03:00
|
|
|
get() {
|
|
|
|
return 'DodgyScheme credscredscreds';
|
|
|
|
}
|
2018-11-07 13:10:07 +03:00
|
|
|
}, {}, function next(err) {
|
|
|
|
const actual = err;
|
|
|
|
const expected = undefined;
|
|
|
|
|
|
|
|
should.equal(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
2021-05-24 13:36:35 +03:00
|
|
|
describe('attempts to verify the credentials as a JWT, not allowing the "NONE" algorithm', function () {
|
2018-11-07 13:10:07 +03:00
|
|
|
it('calls next with an UnauthorizedError if the verification fails', function () {
|
|
|
|
members.authenticateMembersToken({
|
2019-07-05 14:40:43 +03:00
|
|
|
get() {
|
|
|
|
return 'GhostMembers notafuckentoken';
|
|
|
|
}
|
2018-11-07 13:10:07 +03:00
|
|
|
}, {}, function next(err) {
|
|
|
|
const actual = err instanceof UnauthorizedError;
|
|
|
|
const expected = true;
|
|
|
|
|
|
|
|
should.equal(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
2021-05-24 13:36:35 +03:00
|
|
|
it('calls next with an error if the token is using the "none" algorithm', function () {
|
2018-11-07 13:10:07 +03:00
|
|
|
const claims = {
|
|
|
|
rumpel: 'stiltskin'
|
|
|
|
};
|
|
|
|
const token = jwt.sign(claims, null, {
|
|
|
|
algorithm: 'none'
|
|
|
|
});
|
|
|
|
const req = {
|
2019-07-05 14:40:43 +03:00
|
|
|
get() {
|
|
|
|
return `GhostMembers ${token}`;
|
|
|
|
}
|
2018-11-07 13:10:07 +03:00
|
|
|
};
|
|
|
|
members.authenticateMembersToken(req, {}, function next(err) {
|
2021-05-24 13:36:35 +03:00
|
|
|
const actual = err instanceof UnauthorizedError;
|
|
|
|
const expected = true;
|
2018-11-07 13:10:07 +03:00
|
|
|
|
2021-05-24 13:36:35 +03:00
|
|
|
should.equal(actual, expected);
|
2018-11-07 13:10:07 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|