Custom Header support for iOS app
Is your feature request related to a problem? Please describe. My evcc is secured behind a cloudflared tunnel and then exposed to the internet. I can't access it from the iOS app because it doesn't support the authentication via safari.
Describe the solution you'd like The easiest way would be to add an option in the app where you can configure custom headers that are sent with every request made. This way you could add Cloudflare service auth tokens.
Describe alternatives you've considered I've considerd trying to use the cookies one gets from authenticating in the browser from cloudflare but that doesn't work or i don't have the technical know how.
See also https://github.com/evcc-io/app/issues/35
Just came across the immich app. They're doing exactly this to support cloud flare tunneling. https://github.com/immich-app/immich/pull/10588
Guess this would be a pretty straightforward option which also has the benefit of working stateless (no intermediate login flow and cookies we have to take care of). Would also work for widget use-cases.
Screenshot from the immich iOS app
Ok, looks like my enthusiasm was premature. react-native-webview does not support adding custom headers reliably (only on first request).
I did some quick research and found a solution/workaround for this
Working with custom headers, sessions, and cookies
Setting Custom Headers
In React Native WebView, you can set a custom header like this:
<WebView
source={{
uri: 'http://example.com',
headers: {
'my-custom-header-key': 'my-custom-header-value',
},
}}
/>
This will set the header on the first load, but not on subsequent page navigations.
In order to work around this, you can track the current URL, intercept new page loads, and navigate to them yourself (original credit for this technique to Chirag Shah from Big Binary):
const CustomHeaderWebView = (props) => {
const { uri, onLoadStart, ...restProps } = props;
const [currentURI, setURI] = useState(props.source.uri);
const newSource = { ...props.source, uri: currentURI };
return (
<WebView
{...restProps}
source={newSource}
onShouldStartLoadWithRequest={(request) => {
// If we're loading the current URI, allow it to load
if (request.url === currentURI) return true;
// We're loading a new URL -- change state first
setURI(request.url);
return false;
}}
/>
);
};
<CustomHeaderWebView
source={{
uri: 'http://example.com',
headers: {
'my-custom-header-key': 'my-custom-header-value',
},
}}
/>;
@FinleyJDev yes, I found that in the docs. But artificial reloads don't seem like sustainable approach. I guess we also have issues with in-page requests (api calls, web socket). @Maschga
I also found the workaround, but I deliberately don't want to use it. It solves a problem that is a bug in the web view. The author of the blog, which dates from 2016 (sic!), also writes about this. This behaviour must be integrated internally into the web view; it is not something that can or should be built around the web view component. In addition, the workaround means that the component has to be re-rendered for every request, and evcc has many dependencies in the user interface. And it also makes the code of the evcc app more complicated.
If this is specifically for using EVCC behind a Cloudflare-protected app, it may be easier to add support for mTLS and use a client certificate instead of a custom header. I've seen this strategy in other self-hosted apps.
+1, same setup here (evcc behind Cloudflare Tunnel + Access).
Regarding the "headers only on first request" limitation: when Cloudflare Access gets a valid service token (CF-Access-Client-Id / CF-Access-Client-Secret), it sets a CF_Authorization cookie in the response. Subsequent requests from the webview (API, WebSocket) should then be authorized via that cookie. So sending the headers only on the initial load (and again on reload when the cookie expires) might already be enough for the Cloudflare case, without intercepting every navigation. Has anyone tested this?
For widgets and other native requests, the stateless approach used by Immich would still make sense.