Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JWT getEntityQuery #2013

Merged
merged 8 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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