- Real world authorization policies and the associated metadata required to arrive at an authorization decision can be complex and extremely varied (snowflake) from org to org.
Auth0can help with API authorization via a combination ofRules,Roles&Permissions. This can be called asCoarse Grained Authorization (CGA).- However, when policies are/can become fine grained, it is advisable to externalize
Fine Grained Authorization (FGA)to an org-wide centralized service for cleaner design, flexibility, scalability, CI/CD, and overall better management. OPAcan be the building block for such a service (FGAaaS) by serving as a centralized general purposePolicy Decision Point.- This repository covers a sample application to demonstrate this pattern for a
Multi-tenant API.
-
Client app acquires an
Auth0issuedaccess_tokenon behalf of the authenticateduser- The standard Authorization Code Flow can be used here.
- This flow is not relevant to the authorization logic or decision and hence isn't covered here.
- The
access_tokencan be decorated with custom claims usingAuth0 Rules.- The only custom claim in use in this solution is
http://example.com/authn_loawhich indicates theLevel of Assuranceof the Authentication mechanism used while authenticating the user.
- The only custom claim in use in this solution is
- The standard Authorization Code Flow can be used here.
-
Client app makes an API call with the
access_token.HTTP-METHOD/api/v1/:tenant/:resource- e.g.
GET /api/v1/cocacola/content_route - Authorization: Bearer
access_token
-
API code then requests OPA for an Authorization Decision
-
OPA computes the authorization decision
- OPA decodes the
access_tokenand extractsuser_idandauthn_loafrom thesubandhttp://example.com/authn_loaclaims respectively. - Using all the info OPA then computes the authorization decision and responds to the API.
- OPA decodes the
-
API honours the decision and allows/denies the request.
-
Clone this repo
git clone https://github.com/jatinvaidya/api-fga-auth0-opa.git -
Change to repo folder
cd api-fga-auth0-opa -
Create
app/.envandapi/.envfiles referring to the respective.env.samplefiles. -
Start
apiandopacontainersdocker-compose up -
Feed
policyanddatato OPAcd opacurl --request PUT --url http://localhost:8181/v1/policies/example --data-binary @policy.regocurl --request PUT --url http://localhost:8181/v1/data/example --data-binary @data.json -
Run the test app with some arguments and check results. See examples below.
docker run --net fga-network fga-demo/app-image:latest --username admin01@example.com --password Mju76yhn --tenant cocacola --resource vpc --action GETdocker run --net fga-network fga-demo/app-image:latest --username user01@example.com --password Mju76yhn --tenant cocacola --resource vpc --action DELETE