import os
import requests
from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/headers")
def headers():
    return jsonify(dict(request.headers))

@app.route("/<provider>/.well-known/openid-configuration",)
def openid(provider):
    internal = os.environ.get('INTERNAL')
    external = os.environ.get('EXTERNAL')
    url = f'/application/o/{provider}/.well-known/openid-configuration'
    response = requests.get(f'{internal}/{url}')
    return jsonify({
        k: v.replace(internal, external)
            if isinstance(v, str) and (k != 'jwks_uri') else v
            for k, v in response.json().items()
        })

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
