question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to secure newly added APIs

See original GitHub issue

I’ve created an API in superset in the config file. It shows up in swagger UI and I can call it successfully. My next step is to make sure that it can only be accessed if you have the correct permissions.So first I tried adding the @protect() decorator. This makes it so I get 401 when accessing the API.

{"message":"Access is Denied","severity":"danger"}

So far so good. Now I log in with an admin user using the /api/v1//security/login API and I get a token. Then I set the Authorization header on my REST call to my new API and I still get 401. So I thought maybe I need to add the permission to my role. So I add this decorator @permission_name(“AAAAAA”) And I go to find that permission in the roles edit section, but it cannot be found. I have tried several different ways to see if I can gain access to the API:

  1. Tried to change the permission name to some existing permission
  2. Instead of @protect() I tried @has_access_api
  3. I have tried setting the class_permission_name and a few other things and I have spent many hours on this so far searching the net and reading the code and documentation.Does anyone know what the correct way is to protect the API endpoint in the same way as the Dashboards and Charts APIs are protected? For reference here is the basic code I am using without permissions:
class AuthorizationAPI(BaseApi):
    csrf_exempt= True
    @expose('/role/<role>', methods=["POST"])
    @safe
    def role(self, role):
       #My code here
    
    from superset.app import SupersetAppInitializer
    class MySupsersetAppInitializer(SupersetAppInitializer):
    def init_views(self) -> None:
        appbuilder.add_api(AuthorizationAPI)
        super().init_views()

APP_INITIALIZER = MySupsersetAppInitializer

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
issue-label-bot[bot]commented, Sep 16, 2020

Issue-Label Bot is automatically applying the label #question to this issue, with a confidence of 0.79. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

0reactions
maudridcommented, Sep 25, 2020

After spending too much time debugging I finally found the problem. It all came down to the order of the decorators. This order is working for me now:

class AuthorizationAPI(BaseApi):
    csrf_exempt= True

    @expose('/role/<role>', methods=["POST"])
    @protect()
    @safe
    @permission_name("Add roles")
    def role(self, role):
Read more comments on GitHub >

github_iconTop Results From Across the Web

Securing APIs: 10 Best Practices for Keeping Your Data and ...
Best Practices for Securing APIs · Prioritize security. · Inventory and manage your APIs. · Use a strong authentication and authorization solution.
Read more >
8 API Security Best Practices to Protect Sensitive Data
Since REST APIs use HTTP, encryption can be achieved by using the Transport Layer Security (TLS) protocol or its previous iteration, the Secure...
Read more >
12 API security best practices to protect your business
Follow these guidelines to embed API security best practices into tasks ranging from API development, to deployment, to consumption.
Read more >
Securing REST APIs - Okta Developer
The first step in securing an API is to ensure that you only accept queries sent over a secure channel, like TLS (formerly...
Read more >
Best practices for REST API security: Authentication and ...
Best practices for REST API security: Authentication and authorization · Always use TLS · Use OAuth2 for single sign on (SSO) with OpenID...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found