Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API authentication is not available when LOGIN_REQUIRED is true #724

Closed
moepman opened this issue Dec 6, 2016 · 5 comments
Closed

API authentication is not available when LOGIN_REQUIRED is true #724

moepman opened this issue Dec 6, 2016 · 5 comments
Labels
type: bug A confirmed report of unexpected behavior in the application

Comments

@moepman
Copy link

moepman commented Dec 6, 2016

The REST API documentation is missing information on how to supply authentication credentials.

@dorkmatt
Copy link

dorkmatt commented Dec 7, 2016

Agreed this would be very helpful to document, the in-browser API works fine - but in a real world scenario CSRF tokens & cookies are unlikely to be used. Traditional HTTP auth doesn't appear to work for the API endpoints.

@jeremystretch
Copy link
Member

I just dug into this a bit. Although basic HTTP authentication is supported by the REST API, NetBox implements a middleware which redirects all unauthenticated requests to the login page if LOGIN_REQUIRED is true. The redirection occurs before the API can attempt to validate the user.

I believe I've worked around this by exempting all API views from the redirection middleware, and instead enforcing Django REST Framework's built-in authentication when LOGIN_REQUIRED is true. Will have a commit submitted shortly.

@jeremystretch jeremystretch added the type: bug A confirmed report of unexpected behavior in the application label Dec 7, 2016
@jeremystretch jeremystretch changed the title API documentation is missing on authentication API authentication is not available when LOGIN_REQUIRED is true Dec 7, 2016
jeremystretch added a commit that referenced this issue Dec 7, 2016
…TTP authentication when LOGIN_REQUIRED is true
@dorkmatt
Copy link

dorkmatt commented Dec 7, 2016

Not quite, the middleware seems removed but auth isn't working - example:

%  curl -H 'Accept: application/json' -u user:password https://netbox.domain.tld/api/ipam/vlans/ -v
*   Trying 192.168.2.2...
* Connected to netbox.domain.tld (192.168.2.2) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: netbox.domain.tld
* Server certificate: Let's Encrypt Authority X3
* Server certificate: DST Root CA X3
* Server auth using Basic with user 'c3noc'
> GET /api/ipam/vlans/ HTTP/1.1
> Host: netbox.domain.tld
> Authorization: Basic YzNub2M6cGFzc3dvcmQ=
> User-Agent: curl/7.43.0
> Accept: application/json
>
< HTTP/1.1 403 Forbidden
< Date: Wed, 07 Dec 2016 20:34:07 GMT
< Server: Apache
< Vary: Accept,Cookie
< X-Frame-Options: SAMEORIGIN
< Allow: GET, HEAD, OPTIONS
< Strict-Transport-Security: max-age=15552000; includeSubDomains
< Content-Security-Policy: default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data:; font-src 'self' data:;
< Transfer-Encoding: chunked
< Content-Type: application/json
<
* Connection #0 to host netbox.domain.tld left intact
{"detail":"Authentication credentials were not provided."}

The same error results for both incorrect and correct credentials. Possibly the DEFAULT_AUTHENTICATION_CLASSES need to be defined?

@jeremystretch
Copy link
Member

jeremystretch commented Dec 7, 2016

It's working for me:

$ curl "http://localhost:8000/api/dcim/sites/"
{"detail":"Authentication credentials were not provided."}

$ curl "http://localhost:8000/api/dcim/sites/" -u username:badpassword
{"detail":"Invalid username/password."}

$ curl "http://localhost:8000/api/dcim/sites/" -u username:goodpassword
[JSON objects]

BasicAuthentication is included in the default authentication classes so it shouldn't need to be defined explicitly.

@dorkmatt
Copy link

dorkmatt commented Dec 7, 2016

Works for me, the web server was restarted but not gunicorn - restarting both fixed the issue, thank you.

lampwins pushed a commit to lampwins/netbox that referenced this issue Oct 13, 2017
…o enable basic HTTP authentication when LOGIN_REQUIRED is true
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

3 participants