Implement the Logic in Oso Cloud
In this guide, you will implement the authorization logic in Oso Cloud. You will run the Oso Cloud logic alongside your existing logic to confirm that the Oso Cloud implementation is correct. After, you will replace the authorization logic with Oso Cloud.Convert the logic to Polar
In Oso Cloud, you express your authorization logic in Polar. Polar is a concise and flexible language that provides powerful abstractions for both authorization models and application entities. For now, you will implement the logic with simple permission checks in order to mirror the existing application logic. Once that works, you can improve the logic further with more abstractions available in Polar. Recall the authorization logic from the previous step:src/authz.ts
- they have a role on the repository or
- they have a role on the repository’s parent organization
policy.polar
- Entities in authorization logic are actors or resources.
- An actor is the entity that is requesting a permission (e.g.
User
) - A resource is the entity upon which the actor is requesting permission (e.g.
Organization
,Repository
)
- An actor is the entity that is requesting a permission (e.g.
- Two
has_permission
statements that mirror the logic from our application.
Use the Oso Cloud SDK alongside existing authorization code
Next, you will use the Oso Cloud SDK to evaluate authorization requests in your application. Do not replace your existing logic immediately. Instead, add the check API call for your language alongside your existing logic. In TypeScript, this isauthorize()
Import and instantiate the Oso Cloud client.
src/authz.ts
authorize()
call to the canReadRepo()
function, alongside the existing logic.
The call to
osoClient.authorize()
in the following sample will not return the
correct results until you send data to Oso Cloud in the next step.Do not use in
production without providing data as facts.src/auth.tz
authorize()
function accepts three arguments: an actor, an action, and a resource. It returns true
if the actor has permission to perform the action on the resource. If the actor does not have permission, authorize()
returns false
.
- The
actor
andresource
arguments are represented by atype
and anid
.- This is how Oso represents entities in your application.
- In the Node SDK, these entities are expressed as an object with
type
andid
properties.User
object:{type: "User", id: user.id.toString() }
Repository
object:{type: "Repository", id: repo.id.toString() }
- The
permission
argument is a string that is the name of the permission."read"
User
and Repository
objects are assigned to the osoUser
and osoRepository
variables. This simplifies the resulting osoClient.authorize()
call.
If you’re using a different client library, the form of the
Actor
and
Resource
arguments will be different. Check the documentation for your
language’s client for details.Compare the results from Oso Cloud to the original code
By logging the authorization results of the original code and the Oso Cloud SDK, you can compare them ensure the results are identicial. After that, you can switch to using Oso for live authorization with confidence. If you compare those results now, you’ll see something like this:false
, even when the original code returns true
. This results because we have not provided Oso the authorization data to determine what permissions the User
possesses for the Repository
.
We will provide these data initially as context facts.