I started to play around with the IGDB API for an iOS App. Some days ago IGDB launched V4 which now requires authorizing with Twitch via oAuth2 in order to receive an app access token.
With my poor backend knowledge (I literally started yesterday to learn about NGINX) I managed to set up an NGINX Webserver which proxies requests to the IGDB API and injects the app access token into the HTTP Header. This is working fine for now.
My proxy.conf which is included in the nginx.conf looks like this:
server {
listen 443 ssl;
server_name myhost.com;
#SSL Config cut out
...
location / {
proxy_pass https://api.igdb.com/v4/games;
proxy_ssl_server_name on;
proxy_set_header Client-ID "MY TWITCH APP CLIENT ID";
proxy_set_header Authorization "Bearer THE_APP_ACCESS_TOKEN";
}
}
However THE_APP_ACCESS_TOKEN
was requested manually by me. For testing purposes this is fine, however it will expire after about 60 days (according to the Twitch Dev Docs). I am wondering now how I would dynamically request an access token (and store it somehow?), refresh it when it expires and inject it into the proxy.conf.
While researching I stumbled upon the HTTP Auth Request module in combination with the NGINX JavaScript module (https://www.nginx.com/blog/validating-oauth-2-0-access-tokens-nginx/).
Now I wonder if it is a reasonable approach to trigger a token request via the Auth Request Module before proxying the request, parse the JSON response with the JavaScript Module and inject the app access token contained in the response as a variable into the HTTP header of the proxy. While this sounds good to me in theory I barely have an idea how to implement this. Moreover, this approach does not yet include storing and updating the token as soon as it expires.
Do you have some hints for me how to tackle this or is there even another solution?