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

feat: added authorizedCreate to block post beyond the role scope; closes #144 #203

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
44 changes: 34 additions & 10 deletions sci-log-db/src/__tests__/acceptance/task.controller.acceptance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Client, expect} from '@loopback/testlab';
import {Suite} from 'mocha';
import {SciLogDbApplication} from '../..';
import {clearDatabase, createUserToken, setupApplication} from './test-helper';
import { Client, expect } from '@loopback/testlab';
import { Suite } from 'mocha';
import { SciLogDbApplication } from '../..';
import { clearDatabase, createUserToken, setupApplication } from './test-helper';

describe('TaskRepositorySnippet', function (this: Suite) {
this.timeout(5000);
Expand All @@ -15,7 +15,7 @@ describe('TaskRepositorySnippet', function (this: Suite) {
readACL: ['taskAcceptance'],
updateACL: ['taskAcceptance'],
deleteACL: ['taskAcceptance'],
adminACL: ['admin'],
adminACL: ['taskAcceptance'],
shareACL: ['taskAcceptance'],
isPrivate: true,
defaultOrder: 0,
Expand All @@ -26,7 +26,7 @@ describe('TaskRepositorySnippet', function (this: Suite) {
};

before('setupApplication', async () => {
({app, client} = await setupApplication());
({ app, client } = await setupApplication());
await clearDatabase(app);
token = await createUserToken(app, client, ['taskAcceptance']);
});
Expand Down Expand Up @@ -59,6 +59,30 @@ describe('TaskRepositorySnippet', function (this: Suite) {
});
});

it('post a task with authentication should return 403 if taskSnippets permission are invalid', async () => {
const taskSnippetInvalid = {
ownerGroup: 'taskAcceptance',
createACL: ['taskAcceptance'],
readACL: ['taskAcceptance'],
updateACL: ['taskAcceptance'],
deleteACL: ['taskAcceptance'],
adminACL: ['admin'],
shareACL: ['taskAcceptance'],
isPrivate: true,
defaultOrder: 0,
expiresAt: '2055-10-10T14:04:19.522Z',
tags: ['aSearchableTag'],
dashboardName: 'string',
versionable: true,
};
await client
.post('/tasks')
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.send(taskSnippetInvalid)
.expect(403);
});

it('count snippet without token should return 401', async () => {
await client
.get('/tasks/count')
Expand Down Expand Up @@ -125,7 +149,7 @@ describe('TaskRepositorySnippet', function (this: Suite) {
await client
.patch('/tasks/')
.set('Content-Type', 'application/json')
.send({dashboardName: 'aNewName'})
.send({ dashboardName: 'aNewName' })
.expect(401);
});

Expand All @@ -134,7 +158,7 @@ describe('TaskRepositorySnippet', function (this: Suite) {
.patch('/tasks')
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.send({dashboardName: 'aNewName'})
.send({ dashboardName: 'aNewName' })
.expect(200)
.then(result => expect(result.body.count).to.be.eql(1))
.catch(err => {
Expand Down Expand Up @@ -162,7 +186,7 @@ describe('TaskRepositorySnippet', function (this: Suite) {
});

it('Search index with token should return 200 and matching body.tags', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
const includeTags = { fields: { tags: true }, include: ['subsnippets'] };
await client
.get(`/tasks/search=aSearchabletag?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
Expand Down Expand Up @@ -205,7 +229,7 @@ describe('TaskRepositorySnippet', function (this: Suite) {
});

it('Search index with token should return 200 and empty result', async () => {
const includeTags = {fields: {tags: true}};
const includeTags = { fields: { tags: true } };
await client
.get(`/tasks/search=aSearchabletag?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
Expand Down
48 changes: 24 additions & 24 deletions sci-log-db/src/controllers/basesnippet.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {authenticate} from '@loopback/authentication';
import {authorize} from '@loopback/authorization';
import {inject} from '@loopback/core';
import { authenticate } from '@loopback/authentication';
import { authorize } from '@loopback/authorization';
import { inject } from '@loopback/core';
import {
Count,
CountSchema,
Expand All @@ -19,12 +19,12 @@ import {
Response,
RestBindings,
} from '@loopback/rest';
import {SecurityBindings, UserProfile} from '@loopback/security';
import {Basesnippet} from '../models';
import {BasesnippetRepository} from '../repositories';
import {basicAuthorization} from '../services/basic.authorizor';
import {getModelSchemaRef} from '../utils/misc';
import {OPERATION_SECURITY_SPEC} from '../utils/security-spec';
import { SecurityBindings, UserProfile } from '@loopback/security';
import { Basesnippet } from '../models';
import { BasesnippetRepository } from '../repositories';
import { basicAuthorization } from '../services/basic.authorizor';
import { getModelSchemaRef } from '../utils/misc';
import { OPERATION_SECURITY_SPEC } from '../utils/security-spec';

@authenticate('jwt')
@authorize({
Expand All @@ -38,14 +38,14 @@ export class BasesnippetController {
@inject(SecurityBindings.USER) private user: UserProfile,
@repository(BasesnippetRepository)
public basesnippetRepository: BasesnippetRepository,
) {}
) { }

@post('/basesnippets', {
security: OPERATION_SECURITY_SPEC,
responses: {
'200': {
description: 'Basesnippet model instance',
content: {'application/json': {schema: getModelSchemaRef(Basesnippet)}},
content: { 'application/json': { schema: getModelSchemaRef(Basesnippet) } },
},
},
})
Expand All @@ -62,7 +62,7 @@ export class BasesnippetController {
})
basesnippet: Omit<Basesnippet, 'id'>,
): Promise<Basesnippet> {
return this.basesnippetRepository.create(basesnippet, {
return this.basesnippetRepository.authorizedCreate(basesnippet, {
currentUser: this.user,
});
}
Expand All @@ -72,14 +72,14 @@ export class BasesnippetController {
responses: {
'200': {
description: 'Basesnippet model count',
content: {'application/json': {schema: CountSchema}},
content: { 'application/json': { schema: CountSchema } },
},
},
})
async count(
@param.where(Basesnippet) where?: Where<Basesnippet>,
): Promise<Count> {
return this.basesnippetRepository.count(where, {currentUser: this.user});
return this.basesnippetRepository.count(where, { currentUser: this.user });
}

@get('/basesnippets', {
Expand All @@ -91,7 +91,7 @@ export class BasesnippetController {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Basesnippet, {includeRelations: true}),
items: getModelSchemaRef(Basesnippet, { includeRelations: true }),
},
},
},
Expand All @@ -101,7 +101,7 @@ export class BasesnippetController {
async find(
@param.filter(Basesnippet) filter?: Filter<Basesnippet>,
): Promise<Basesnippet[]> {
return this.basesnippetRepository.find(filter, {currentUser: this.user});
return this.basesnippetRepository.find(filter, { currentUser: this.user });
}

@get('/basesnippets/export={exportType}', {
Expand All @@ -113,7 +113,7 @@ export class BasesnippetController {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Basesnippet, {includeRelations: true}),
items: getModelSchemaRef(Basesnippet, { includeRelations: true }),
},
},
},
Expand Down Expand Up @@ -143,7 +143,7 @@ export class BasesnippetController {
'Find the index (i.e position) of a basesnippet within a query.',
content: {
'application/json': {
schema: {type: 'number'},
schema: { type: 'number' },
},
},
},
Expand All @@ -165,7 +165,7 @@ export class BasesnippetController {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Basesnippet, {includeRelations: true}),
items: getModelSchemaRef(Basesnippet, { includeRelations: true }),
},
},
},
Expand All @@ -189,15 +189,15 @@ export class BasesnippetController {
responses: {
'200': {
description: 'Basesnippet PATCH success count',
content: {'application/json': {schema: CountSchema}},
content: { 'application/json': { schema: CountSchema } },
},
},
})
async updateAll(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Basesnippet, {partial: true}),
schema: getModelSchemaRef(Basesnippet, { partial: true }),
},
},
})
Expand All @@ -216,15 +216,15 @@ export class BasesnippetController {
description: 'Basesnippet model instance',
content: {
'application/json': {
schema: getModelSchemaRef(Basesnippet, {includeRelations: true}),
schema: getModelSchemaRef(Basesnippet, { includeRelations: true }),
},
},
},
},
})
async findById(
@param.path.string('id') id: string,
@param.filter(Basesnippet, {exclude: 'where'})
@param.filter(Basesnippet, { exclude: 'where' })
filter?: FilterExcludingWhere<Basesnippet>,
): Promise<Basesnippet> {
return this.basesnippetRepository.findById(id, filter, {
Expand All @@ -245,7 +245,7 @@ export class BasesnippetController {
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Basesnippet, {partial: true}),
schema: getModelSchemaRef(Basesnippet, { partial: true }),
},
},
})
Expand Down
Loading