You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I work for a local startup that was/is in need of an inventory/BOM solution for our products. We found Inventree, and so far its been fantastic.
We have Inventree securely hidden behind an SSO sign-in reverse proxy called Pomerium, which is essentially our solution to not having a conventional intranet (and for working remotely).
This means we have to log in twice, once through google SSO, and a second time with InvenTree.
Is there a way to 1) unify the accounts (inventree SSO login) and/or 2) or have some other sort of automated log in?
Hey @sihawken I'm so excited that you're finding Inventree useful :)
I do not have any experience with such systems. Do you have any way to work out how to connect these two systems - and potentially contribute these enhancements back to the project?
I do not either, I set up the SSO with Pomerium after a night of research. I would absolutely love to attempt this, but it's very daunting. I'm a mechanical engineer by trade with only a tad bit of python experience. Pardon my general lack of understanding if I get something wrong.
Regardless, here are a few details that could help with finding a way to accomplish this:
Pomerium allows setting request headers, which seems like it could be used to authenticate users.
And here are a few details regarding SSO. SSO services are often used to "stitch" together services with a single user account..
The most common protocol for SSO is OAuth2.0, which Twitter, Facebook, Microsoft, and Google support: https://oauth.net/2/. Office 365/Sharepoint and GSuite users would definitely see the most benefit from this. Second to that is SAML, which is generally only used in large enterprises.
There seems to be a great Django OAuth Toolkit (FreeBSD License). Perhaps that could make something like this much easier, I am unsure.
Theoretically, it would be possible to pass a header to the downstream application (InvenTree) to log someone in with an already authenticated SSO session. Signing them in automagically.
This is closely related to #634, it seems relatively straight forward to add providers to django-allauth. I also would like to use InvenTree with SSO so I'm willing to work on this at some point.
I have experience with building an auth management utility for django for my work this year as well as running django/gunicorn behind a forward auth mechanism the traefik uses.
Right now I have inventree running behind traefik and traefik-forward-auth proxy. This guarantees authentication before it hits django, giving you an X-Forwarded-User (or whatever header) which contains the email (or username). In my case, I'm using traefik-forward-auth and google auth, which sets a cookie on the browser and validates the cookie before forwarding it on to traefik. Users are whitelisted so even if you can login through google, the user will see an "Unauthorized" page. Removing the cookie just send you back to google auth to reauthenticate.
This works great for the web access but sucks for api access, since the clients have to somehow set a cookie to get past the auth mechanism.
The way around this is to generate an API key on a per user basis with an expiration date, or to get the oauth2 server to create a bearer token which can be put in the headers on an API request. Django would then look for either the oauth2 cookie on api header token in middleware upon every request, and put the user on the request.
The trick is that if the user needs to exist in django (or automatically get created) if you intend on using request.user.
The big lesson for me was that API and Web authentication are separate beasts and need to be handled as such. What works in the browser doesn't work for the api.
I was looking at middleware.py and and typically it's good practice to separate authentication (AUTHN) from authorization (AUTHZ). The result of the middleware for authentication should be to put the user on the request or django's unknown user. Authentication answers the question of "Do I know the user?".
Then the view layer is responsible for authorization answering the question "Does request.user have access to this view or specific data?"
If the oauth2 token or cookie is valid and maps to a specific user in the db that user should be put on the request in the middleware, or if the user doesn't exist should be forwarded to page for the user to enter their details, etc....
Also you'd want this to be pluggable by the config.yml file so authentication (AUTHN) middleware is configurable by the user.
Authorization (AUTHZ) could be data driven from what is stored in the database. "Does request.user have access to this view? Does request.user have access to this object in the database?" Those are typically handled from view to view. So certain views may be read only for all requests, certain people can only POST to specific API views.
@Zeigren@rcludwick there is a PR now that integrates OAuth and OpenID. Would you be interested in testing this in the next few weeks? It is not polished right now and the docs are just coming together but I would really appreciate input from real-life deployments.
I can not predict when this will be merged as there is a lot to test and this represents a big change in core functionality.
The PR does not cover the usage of a proxy to authenticate however.
Activity
SchrodingersGat commentedon Jan 30, 2020
Hey @sihawken I'm so excited that you're finding Inventree useful :)
I do not have any experience with such systems. Do you have any way to work out how to connect these two systems - and potentially contribute these enhancements back to the project?
sihawken commentedon Jan 30, 2020
I do not either, I set up the SSO with Pomerium after a night of research. I would absolutely love to attempt this, but it's very daunting. I'm a mechanical engineer by trade with only a tad bit of python experience. Pardon my general lack of understanding if I get something wrong.
Regardless, here are a few details that could help with finding a way to accomplish this:
Pomerium allows setting request headers, which seems like it could be used to authenticate users.
And here are a few details regarding SSO. SSO services are often used to "stitch" together services with a single user account..
The most common protocol for SSO is OAuth2.0, which Twitter, Facebook, Microsoft, and Google support: https://oauth.net/2/. Office 365/Sharepoint and GSuite users would definitely see the most benefit from this. Second to that is SAML, which is generally only used in large enterprises.
There seems to be a great Django OAuth Toolkit (FreeBSD License). Perhaps that could make something like this much easier, I am unsure.
Theoretically, it would be possible to pass a header to the downstream application (InvenTree) to log someone in with an already authenticated SSO session. Signing them in automagically.
Zeigren commentedon Feb 3, 2020
This is closely related to #634, it seems relatively straight forward to add providers to django-allauth. I also would like to use InvenTree with SSO so I'm willing to work on this at some point.
SchrodingersGat commentedon Aug 24, 2020
@sihawken - check out the change made in #930
eeintech commentedon Oct 20, 2020
@sihawken Did you check out the PR shared by @SchrodingersGat. Any comment/feedback?
rcludwick commentedon Jan 2, 2021
I have experience with building an auth management utility for django for my work this year as well as running django/gunicorn behind a forward auth mechanism the traefik uses.
Right now I have inventree running behind traefik and traefik-forward-auth proxy. This guarantees authentication before it hits django, giving you an X-Forwarded-User (or whatever header) which contains the email (or username). In my case, I'm using traefik-forward-auth and google auth, which sets a cookie on the browser and validates the cookie before forwarding it on to traefik. Users are whitelisted so even if you can login through google, the user will see an "Unauthorized" page. Removing the cookie just send you back to google auth to reauthenticate.
This works great for the web access but sucks for api access, since the clients have to somehow set a cookie to get past the auth mechanism.
The way around this is to generate an API key on a per user basis with an expiration date, or to get the oauth2 server to create a bearer token which can be put in the headers on an API request. Django would then look for either the oauth2 cookie on api header token in middleware upon every request, and put the user on the request.
The trick is that if the user needs to exist in django (or automatically get created) if you intend on using request.user.
The big lesson for me was that API and Web authentication are separate beasts and need to be handled as such. What works in the browser doesn't work for the api.
rcludwick commentedon Jan 2, 2021
I was looking at middleware.py and and typically it's good practice to separate authentication (AUTHN) from authorization (AUTHZ). The result of the middleware for authentication should be to put the user on the request or django's unknown user. Authentication answers the question of "Do I know the user?".
Then the view layer is responsible for authorization answering the question "Does request.user have access to this view or specific data?"
If the oauth2 token or cookie is valid and maps to a specific user in the db that user should be put on the request in the middleware, or if the user doesn't exist should be forwarded to page for the user to enter their details, etc....
Also you'd want this to be pluggable by the config.yml file so authentication (AUTHN) middleware is configurable by the user.
Authorization (AUTHZ) could be data driven from what is stored in the database. "Does request.user have access to this view? Does request.user have access to this object in the database?" Those are typically handled from view to view. So certain views may be read only for all requests, certain people can only POST to specific API views.
matmair commentedon Sep 1, 2021
@Zeigren @rcludwick there is a PR now that integrates OAuth and OpenID. Would you be interested in testing this in the next few weeks? It is not polished right now and the docs are just coming together but I would really appreciate input from real-life deployments.
I can not predict when this will be merged as there is a lot to test and this represents a big change in core functionality.
The PR does not cover the usage of a proxy to authenticate however.