Git Product home page Git Product logo

fastapi-oso-hello-world's Introduction

A brief tutorial for integrating oso logic into a FastAPI application.

Python Installation Requirements

sudo pip3 install oso FastAPI passlib[bcrypt] python-jose[cryptography]

Explore

Before getting started, read the following documentation:

Comments are left throughout main.py to guide you through the application's logic.

This application is written with the assumption that it is being passed query params and/or data through the response body.

Policies are defined in auth.polar

Authentication logic is modelled from the FastAPI Security Chapter (see link above) is abstracted to the '/auth' folder.

To test the various API calls are their results, open your terminal and perform the following commands:


# change directory
cd /path/to/this/directory

# spin up FastAPI
bash spin.sh

# In your browser, navigate to: 127.0.0.1:1337/docs
# Through the FastAPI swagger UI, you can test the various example API calls for their results.
# Next, read each route defined in main.py to explore oso + FastAPI functionality.
# When a route header label is clicked through the FastAPI swagger UI, the comments from main.py will be displayed for each corresponding route. 

fastapi-oso-hello-world's People

Contributors

harleylang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fastapi-oso-hello-world's Issues

Make use of `FastAPI().middlewear()`

  • Relevant conversation from slack:

We normally think about authorization decisions as being either (a) middleware over the route, or (b) resource-level in the method bodies. I think your allow_xyz decorator is maybe somewhere between the two? Maybe having something like a one off middleware would avoid needing to duplicate the logic a bit. And then using oso.is_allowed directly in the methods would mean you could do the type validation first using fastapi?

What I'm picturing is something like:

  1. A middleware to check each request is allowed. That can be used to check for route-level access.
  2. Fine-grained access control over data models like:

@app.post("/items/")
async def create_item(item: Item, current_user: User = Depends(get_current_user):
if oso.is_allowed(current_user, "create", item):
return item
else:
return ...

  • Bonus points -- generic decorator:

The thing that is (imo) really cool about FastAPI is the use of typing and validation. I wonder whether you could make (2) into a generic decorator:

@app.post("/items/")
@oso.authorize
async def create_item(item: Item):
return item

That would do all of:

  1. Adds the get_current_user dependency
  2. Calls oso.is_allowed(current_user, , )
  3. e.g. calls oso.is_allowed(current_user, "create_item", item)

Then the policy could just look like:

allow(user: User, "create_item", item: Item) if
   user.is_admin;allow(user: User, "create_item", item: Item) if
   user = item.created_by;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.