Skip to content

Commit

Permalink
fix(authentication): Add JWT getEntityQuery (#2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
burn2delete committed Jul 24, 2020
1 parent a9501ac commit e0e7fb5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/authentication-local/src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class LocalStrategy extends AuthenticationBaseStrategy {
}

async findEntity (username: string, params: Params) {
const { entityUsernameField, service, errorMessage } = this.configuration;
const { entityUsernameField, errorMessage } = this.configuration;
if (!username) { // don't query for users without any condition set.
throw new NotAuthenticated(errorMessage);
}
Expand All @@ -55,7 +55,7 @@ export class LocalStrategy extends AuthenticationBaseStrategy {
}, params);

const findParams = Object.assign({}, params, { query });
const entityService = this.app.service(service);
const entityService = this.entityService;

debug('Finding entity with query', params.query);

Expand All @@ -74,7 +74,7 @@ export class LocalStrategy extends AuthenticationBaseStrategy {
}

async getEntity (result: any, params: Params) {
const { entityService } = this;
const entityService = this.entityService;
const { entityId = entityService.id, entity } = this.configuration;

if (!entityId || result[entityId] === undefined) {
Expand Down
15 changes: 11 additions & 4 deletions packages/authentication/src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
const config = super.configuration;

return {
entity: authConfig.entity,
service: authConfig.service,
entity: authConfig.entity,
entityId: authConfig.entityId,
header: 'Authorization',
schemes: [ 'Bearer', 'JWT' ],
...config
Expand Down Expand Up @@ -66,7 +67,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
}

verifyConfiguration () {
const allowedKeys = [ 'entity', 'service', 'header', 'schemes' ];
const allowedKeys = [ 'entity', 'entityId', 'service', 'header', 'schemes' ];

for (const key of Object.keys(this.configuration)) {
if (!allowedKeys.includes(key)) {
Expand All @@ -79,22 +80,28 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
}
}

async getEntityQuery (_params: Params) {
return {};
}

/**
* Return the entity for a given id
* @param id The id to use
* @param params Service call parameters
*/
async getEntity (id: string, params: Params) {
const { entity } = this.configuration;
const entityService = this.entityService;
const { entity } = this.configuration;

debug('Getting entity', id);

if (entityService === null) {
throw new NotAuthenticated(`Could not find entity service`);
}

const result = await entityService.get(id, omit(params, 'provider', 'query'));
const query = await this.getEntityQuery(params);
const getParams = Object.assign({}, omit(params, 'provider'), { query });
const result = await entityService.get(id, getParams);

if (!params.provider) {
return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/test/jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('authentication/jwt', () => {
app.setup();
});

it('getEntity (and params.query)', async () => {
it('getEntity', async () => {
const [ strategy ] = app.service('authentication').getStrategies('jwt') as JWTStrategy[];

let entity = await strategy.getEntity(user.id, {
Expand Down

0 comments on commit e0e7fb5

Please sign in to comment.