simple-login / app

The SimpleLogin back-end and web app
https://simplelogin.io
GNU Affero General Public License v3.0
5.14k stars 435 forks source link

[Feature Request] Support setting APPLICATION_ROOT Flask config #1952

Open bonnerl opened 11 months ago

bonnerl commented 11 months ago

Problem

I want to use a single NGINX instance and domain to proxy to several backend services with SimpleLogin being one of the backends under a dedicated folder, for example, I'd like to have example.com/SimpleLogin/Dashboard serve Simple Login and example.com/HelloWorld/ to serve my hello world app. Currently SimpleLogin doesn't play nice with this because it will not use the /SimpleLogin/ prefix when generating URLs.

Proposed Solution

APPLICATION_ROOT Inform the application what path it is mounted under by the application / web server. This is used for generating URLs outside the context of a request

https://flask.palletsprojects.com/en/3.0.x/config/#APPLICATION_ROOT

Update Simple Login to support an optional APPLICATION_ROOT setting in simplelogin.env, and copy the value (when given) to Flask's APPLICATION_ROOT config. Flask will then use APPLICATION_ROOT to build URLs.

MuhammadNaeem42 commented 11 months ago

i will help you to setup this application contact me https://www.fiverr.com/s/9W3PNA simple

muxo771 commented 11 months ago

Is this the same for the 4.6.x beta? Seems to have no effect.

bonnerl commented 11 months ago

@muxo771 SimpleLogin doesn't currently support APPLICATION_ROOT for 3.x or 4.6.x.

leor-gh commented 6 months ago

I had the same use case requirement to host SImpleLogin not under web server ROOT but another path. I spent a few hours yesterday afternoon to fix the code and achieved it. And today I stumbled upon this issue.

Disclaimer I have not done any serious Python programming before, other than being aware of its syntax. I know even less about Flask (didn't hear about it before), and what I did may not be the "right" way.

APPLICATION_ROOT is a Flask thing, but it seems to be related to middle dispatcher, which SL does not use. Adding it may be quite a task.

What I did was simply adjusting the url_prefix (adding the prefix I want) when registering the blueprints in server.py (there are 10 blueprints) and prepending the prefix when adding the routes for the app (e.g. /favicon.ico). Then all the generated URLs will have the correct /path prefixes. I also changed the default "static" path to /path/static to take care of serving the static files.

The remaining issues are the HTML templates have mixed references to /static and /api Some places are hard-coded with /static while some use url_for('static'). The latter generates the correct path. Same goes for the /api path.

Then in nginx.conf, I pass the /path prefix from the URL to proxy_pass so SL receives the expected full path.

Once that is all done, SL works as expected. Only a few "routes" are not changed, but they are related to functionality I don't care/need, like OpenID, Paddle, Coinbase, etc.

Hope this helps.

leor-gh commented 6 months ago

There is a thread on stackoverflow discussing the same issue: (https://stackoverflow.com/questions/67737069/how-to-host-a-flask-app-on-a-subfolder-url-prefix-with-nginx/75123044)

One solution seems to be simply as adding a couple new imports and then APPLICATION_ROOT should work as intended.

Another one is adding a new argument to ProxyFix in server.js, which SL is already using. In nginx configuration, be sure to add the trailing / to proxy_pass so that it will remove the prefix when passing the URL to the server. SL can then process all the URLs correctly.

On generating URL, Flask will add the prefix as passed in via the X-Forwarded-Prefix to all generated URLs. This takes care of most URLs from the application.

However, as mentioned, quite a few files in the templates are hard coded with /static and /api and they need to be changed with using url_for as appropriate so that URLs are generated correctly.

I tested X-Forwarded-Prefix set up and the app is working fine for the functionality I use. My Android client also works with API access once I update the server URL.