-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add permission support in lightspeed-backend
Signed-off-by: Karthik <[email protected]>
- Loading branch information
1 parent
1967890
commit 2dc7f6d
Showing
8 changed files
with
292 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
workspaces/lightspeed/plugins/lightspeed-backend/src/service/permission.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { mockCredentials } from '@backstage/backend-test-utils'; | ||
import { | ||
AuthorizeResult, | ||
createPermission, | ||
} from '@backstage/plugin-permission-common'; | ||
|
||
import { userPermissionAuthorization } from './permission'; | ||
|
||
describe('userPermissionAuthorization', () => { | ||
let mockPermissionsService: any; | ||
const mockPermission = createPermission({ | ||
name: 'test.permission', | ||
attributes: { action: 'read' }, | ||
}); | ||
|
||
beforeEach(() => { | ||
mockPermissionsService = { | ||
authorize: jest.fn(), | ||
}; | ||
}); | ||
|
||
it('should not throw NotAllowedError', async () => { | ||
mockPermissionsService.authorize.mockResolvedValueOnce([ | ||
{ result: AuthorizeResult.ALLOW }, | ||
]); | ||
|
||
const { authorizeUser } = userPermissionAuthorization( | ||
mockPermissionsService, | ||
); | ||
|
||
const result = await authorizeUser(mockPermission, mockCredentials.user()); | ||
expect(() => result).not.toThrow(); | ||
}); | ||
|
||
it('should throw NotAllowedError when authorization is denied', async () => { | ||
mockPermissionsService.authorize.mockResolvedValueOnce([ | ||
{ result: AuthorizeResult.DENY }, | ||
]); | ||
|
||
const { authorizeUser } = userPermissionAuthorization( | ||
mockPermissionsService, | ||
); | ||
|
||
await expect( | ||
authorizeUser(mockPermission, mockCredentials.user()), | ||
).rejects.toThrow('Unauthorized'); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
workspaces/lightspeed/plugins/lightspeed-backend/src/service/permission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
BackstageCredentials, | ||
PermissionsService, | ||
} from '@backstage/backend-plugin-api'; | ||
import { NotAllowedError } from '@backstage/errors'; | ||
import { | ||
AuthorizeResult, | ||
BasicPermission, | ||
} from '@backstage/plugin-permission-common'; | ||
|
||
export function userPermissionAuthorization( | ||
permissionsService: PermissionsService, | ||
) { | ||
const permissions = permissionsService; | ||
|
||
return { | ||
async authorizeUser( | ||
permission: BasicPermission, | ||
credentials: BackstageCredentials, | ||
): Promise<void> { | ||
const decision = ( | ||
await permissions.authorize( | ||
[ | ||
{ | ||
permission, | ||
}, | ||
], | ||
{ credentials }, | ||
) | ||
)[0]; | ||
|
||
if (decision.result !== AuthorizeResult.ALLOW) { | ||
throw new NotAllowedError('Unauthorized'); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.