namespaces API keys, finds collection based on JWT collection payload
This commit is contained in:
@@ -51,7 +51,7 @@ describe('Users REST API', () => {
|
||||
it('should return a logged in user from /me', async () => {
|
||||
const response = await fetch(`${url}/api/admins/me`, {
|
||||
headers: {
|
||||
Authorization: `JWT ${token}`,
|
||||
Authorization: `Admin JWT ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -65,7 +65,7 @@ describe('Users REST API', () => {
|
||||
const response = await fetch(`${url}/api/admins/refresh-token`, {
|
||||
method: 'post',
|
||||
headers: {
|
||||
Authorization: `JWT ${token}`,
|
||||
Authorization: `Admin JWT ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -104,7 +104,7 @@ describe('Users REST API', () => {
|
||||
roles: ['editor'],
|
||||
}),
|
||||
headers: {
|
||||
Authorization: `JWT ${token}`,
|
||||
Authorization: `Admin JWT ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'post',
|
||||
|
||||
@@ -24,7 +24,7 @@ const refresh = async (args) => {
|
||||
const opts = {};
|
||||
opts.expiresIn = options.collection.config.auth.tokenExpiration;
|
||||
|
||||
const token = options.authorization.replace('JWT ', '');
|
||||
const token = options.authorization.replace(`${options.collection.config.labels.singular} JWT `, '');
|
||||
const payload = jwt.verify(token, secret, {});
|
||||
delete payload.iat;
|
||||
delete payload.exp;
|
||||
|
||||
@@ -3,7 +3,7 @@ const PassportAPIKey = require('passport-headerapikey').HeaderAPIKeyStrategy;
|
||||
module.exports = ({ Model, config }) => {
|
||||
const opts = {
|
||||
header: 'Authorization',
|
||||
prefix: 'API-Key ',
|
||||
prefix: `${config.labels.singular} API-Key `,
|
||||
};
|
||||
|
||||
return new PassportAPIKey(opts, false, (apiKey, done) => {
|
||||
|
||||
@@ -3,13 +3,15 @@ const passportJwt = require('passport-jwt');
|
||||
const JwtStrategy = passportJwt.Strategy;
|
||||
const { ExtractJwt } = passportJwt;
|
||||
|
||||
module.exports = (config, collection) => {
|
||||
module.exports = (config, collections) => {
|
||||
const opts = {};
|
||||
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('JWT');
|
||||
opts.secretOrKey = config.secret;
|
||||
|
||||
return new JwtStrategy(opts, async (token, done) => {
|
||||
try {
|
||||
const collection = collections[token.collection];
|
||||
|
||||
const user = await collection.Model.findByUsername(token.email);
|
||||
|
||||
const json = user.toJSON({ virtuals: true });
|
||||
|
||||
Reference in New Issue
Block a user