Extract a Piece of Authorization Logic
Identify the logic to extract
First, select a piece of authorization logic to move to Oso Cloud. For your section of logic should be:- Well understood
- Low-impact
- Straightforward to extract
read
a repository. That permission affects two operations:
read
a single repository- list all the repositories that the user can
read
get
handlers:
- The same logic is implemented in multiple places
- The implementation slightly differs in each place
- It is not obvious it is critical authorization logic
- The logic is straightforward - a user can read a repository if:
- They have any role on the repository
- They have any role on the repository’s parent organization
- It only grants read access.
- The logic is already encapsulated.
Extract the logic into a dedicated function
With a piece of logic to refactor identified, extract it into a dedicated function. This decouples the authorization logic from the surrounding application logic. Create a function calledcanReadRepo()
in a new file called authz.ts
. That makes it obvious which permission it governs.
src/authz.ts
src/routes/accounts.ts
src/routes/accounts.ts
- A dedicated file for authorization logic (
src/authz.ts
). - The logic for the “read repository” permission is easy to find, understand, and reason about.
- The logic is defined only once.
- The application code is cleaner.