superset icon indicating copy to clipboard operation
superset copied to clipboard

How to authenticate with REST API

Open amancevice opened this issue 9 years ago • 27 comments

Make sure these boxes are checked before submitting your issue - thank you!

  • [x] I have checked the superset logs for python stacktraces and included it here as text if any
  • [x] I have reproduced the issue with at least the latest released version of superset
  • [x] I have checked the issue tracker for the same issue and I haven't found one similar

Superset version

0.17.1

Expected results

N/A

Actual results

N/A

Steps to reproduce

Sorry if this has been answered somewhere else, but is there any documentation on how to authenticate through the REST API?

Specifically, I'd like to be able to return JSON from the /superset/explore_json/ endpoint (copied from the UI slice view) using cURL (or something similar) but I get the following response when I do:

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

Thanks

amancevice avatar Mar 27 '17 16:03 amancevice

This is how we do it in the unit tests: https://github.com/airbnb/superset/blob/master/tests/base_tests.py#L127

But that may vary depending on the type of authentication you use.

mistercrunch avatar Mar 28 '17 15:03 mistercrunch

Hey @amancevice did you figure this out? I have this exact use case and was wondering if you solve it. Thanks!

slarrain avatar Nov 09 '17 16:11 slarrain

@slarrain I did not -- gave up & moved on to other things. Sorry!

amancevice avatar Nov 09 '17 16:11 amancevice

For those still looking for some kind of solution - the following example seems to work:

import requests
from bs4 import BeautifulSoup

# set up session for auth
s = requests.Session()
login_form = s.post("http://my_server/login")

# get Cross-Site Request Forgery protection token
soup = BeautifulSoup(login_form.text, 'html.parser')
csrf_token = soup.find('input',{'id':'csrf_token'})['value']

# login the given session
s.post('http://my_server/login/',data=dict(username='admin', password='my_passwd',csrf_token=csrf_token))

# run API call
print(s.get('http://my_server/users/api').text)

andrewsali avatar Jan 17 '18 20:01 andrewsali

Is there a documentation of the rest api ?

alexjacquot avatar May 17 '18 14:05 alexjacquot

The part we get for free on modelviews through FAB is documented here: https://github.com/dpgaspar/Flask-AppBuilder/blob/master/docs/quickhowto.rst#exposed-methods

mistercrunch avatar May 17 '18 16:05 mistercrunch

i need authentication by using json web tokens

kalimuthu123 avatar Aug 14 '19 13:08 kalimuthu123

For those still looking for some kind of solution - the following example seems to work:

import requests
from bs4 import BeautifulSoup

# set up session for auth
s = requests.Session()
login_form = s.post("http://my_server/login")

# get Cross-Site Request Forgery protection token
soup = BeautifulSoup(login_form.text, 'html.parser')
csrf_token = soup.find('input',{'id':'csrf_token'})['value']

# login the given session
s.post('http://my_server/login/',data=dict(username='admin', password='my_passwd',csrf_token=csrf_token))

# run API call
print(s.get('http://my_server/users/api').text)

I had to change login_form = s.post("http://my_server/login") to login_form = s.get("http://my_server/login") for this to work. thanks!

prgx-mrodri01 avatar Mar 27 '20 13:03 prgx-mrodri01

The solution to the problem is pretty simple. Just hit post api "api/v1/security/login". Provide the JSON body with following { "password": "complex-password", "provider": "db", "refresh": true, "username": "admin" } The jwt token will be generated in the response.

pritypriya25 avatar Jun 11 '21 14:06 pritypriya25

Hi pritypriya25 its working for me.it generated jwt token. how can i need to fetch dashboard in superset through /dashboard/ endpoint can you please help me. Thank you

imanju avatar Aug 30 '21 07:08 imanju

@imanju you can check superset api . It provides APIs for all kind of operations. https://superset.apache.org/docs/rest-api It also has a swagger ui where you can check for all APIs.

pritypriya25 avatar Aug 30 '21 07:08 pritypriya25

The solution to the problem is pretty simple. Just hit post api "api/v1/security/login". Provide the JSON body with following { "password": "complex-password", "provider": "db", "refresh": true, "username": "admin" } The jwt token will be generated in the response.

This can be used with default authentication. But I'm using a custom Security Manager to login and I don't have a password for the superset user (since it uses an external OAuth2 provider). How can I login to the APIs?

cyanoboy avatar Oct 14 '21 09:10 cyanoboy

I have similar problem like @cyanoboy. I am also using a custom Security Manager(external OAuth2 provider). Couldn't find a way to get the JWT token to query the APIs.

I do see that the request body has a provider field:

{
"password": "complex-password",
"provider": "db",
"refresh": true,
"username": "admin"
}

However, cannot find the right provider to use for the custom Security Manager. What to use when there is no username/password style db authentication.?

Any help is highly appreciated. Thanks in advance!

harshgadhia avatar Oct 18 '21 17:10 harshgadhia

I have the same issue and am wondering if it's just more straight forward to get the token from the oath provider directly and use it within superset. It should work. I"m going to give it a try and see if it works.

dshaqra avatar Jan 11 '22 23:01 dshaqra

I haven't really found a good way to authenticate non password based accounts against the rest api. So far only db and ldap work. What I have resorted to is creating a db based "service account" and using that for api actions

nytai avatar Feb 09 '22 01:02 nytai

@nytai can this db based service account work if use auth_type as AUTH_Oauth. I assume the bearer token will be from the issuer signed rs256 token how can this be used with api in superset

Synarcs avatar Feb 17 '22 20:02 Synarcs

Faced this problem trying to access explore_json with JWT token. This doesn't work cause this endpoint is missing @protected decorator. Want to make a PR to fix it (or receive feedback why I shouldn't do so).

xneg avatar Mar 30 '22 14:03 xneg

hello , i want to integrate superset with an external existing service , but the problem is that users have to login twice (one for the service and one for supsetset ) can i bypass it with the jwt token ?

did anyone face this before ?

Mehdi-YC avatar May 24 '22 19:05 Mehdi-YC

@SAVE-POlNT we are doing exact this thing now. But, of course, there are some pitfalls and also it depends on existing service. I don't think this topic matches this issue but you can contact me directly so we could discuss your problem.

xneg avatar May 25 '22 08:05 xneg

@xneg i would like to , can yu please leave your discord or your e-mail so i can contact you ?

Mehdi-YC avatar May 27 '22 23:05 Mehdi-YC