hass-emulated-hue/core

CORS support

Closed this issue · 0 comments

Is your feature request related to a problem? Please describe.

It's rather an inconvenience than a problem, though might be limiting use of Emulated Hue.

Describe the feature that you would like to be added

I would like to request CORS support in Emulated Hue. This would require Emulated Hue to support OPTIONS method on API endpoints, and output CORS headers in all responses (Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers).
I'd imagine it could make sense to add a feature toggle for this.

Describe alternatives you've considered

I setup NGINX Home Assistant SSL proxy and used the below config to force-inject CORS headers which solved my issue.
That being said, this workaround is not ideal as it requires an extra middleware, introduces an extra latency, adds complexity in setting up HTTPS endpoint, might not support some of advanced features in Emulated Hue.

server {
    listen 80;
    server_name 10.0.12.10; # HA IP

    location / {
        
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' '*';
            add_header 'Access-Control-Allow-Headers' '*';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        proxy_pass http://10.0.12.10:8080; # Emulated Hue
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' '*';
        add_header 'Access-Control-Allow-Headers' '*';
        add_header 'Access-Control-Max-Age' 1728000;
    }
}

Describe your use case for this feature
Some applications using Hue API, e.g. https://apps.apple.com/us/app/soundstorm-for-hue/id1230217709, require CORS headers. While many other apps may not require this, the fact that they work with the native bridge means that absence of CORS headers is a gap between emulated API and the real oe.

Additional context

N/A