1

this is my first attempt to create a RESTful app using Flask framework. This is an API for cooking recipes. I already have basic functionality but I have encountered a problem with user accounts and content based on "logged in" user (username from authorization).

I run through many tutorials and documentations, but I have not found a solution I'm looking for. Only user/role based authentication to give or deny access to specified resource.

Simply: Everyone registered can see all users and their data, but only owner of the account can access full data and modify/delete it.

I've decided to use @auth.login_required decorator to check if user is registered and then in other methods (GET, POST etc.) use my own function to compare username from URL with username from auth header:

def is_owner(username):
    if username == request.authorization.username:
        return True
    else:
        return False

but I don't think this is a proper way of doing this. In POST, PUT, DELETE methods this is quite simple, because I either give or deny access, but in GET methods I want to return only part of information, so e.g. only username to guests, and full data containing email etc. for owner of the account. Same applies to other GET methods (return all recipes to owner or only those marked as public). This generates a problem with response because I would have to create new fields for each type of response in order to use marshal

The whole source code of this application is on my github.

This is mostly related question I've found, but I wonder if there are better ways.

There are for sure many bugs and strange ways in which I do some things as this is my first try. If you have some comments not regarding this particular question please let me know in comments or on my github page.

  • I recommend to insert the code from `GET` handler of one of the classes and imports related to it (`flask_httpauth` for example). It surely can be viewed in the github source but for the users of SO it will be easier to get the details here. – Sergey Shubin Oct 09 '17 at 06:46

0 Answers0