from flask import Flask, session, request, jsonify from urllib.parse import urlparse
@app.route("/cors/userinfo") defcors_userinfo(): headers = dict(request.headers) origin = headers.get("Origin", "") ifnot origin: return"Origin field is missing"
domain = urlparse(origin) if domain.hostname != "a.com"and \ (not domain.hostname.endswith(".a.com")): return"Origin is not allowed" # check protocol is not necessary but highly recommended if domain.scheme != "https": return"HTTPS is required"
resp_headers = { "Access-Control-Allow-Methods": "GET", # limit methods to only GET "Access-Control-Allow-Origin": origin, # for 1) "Access-Control-Allow-Credentials": "true", # for 2) "Vary": "Origin", # avoid browser cache } uid = session.get("uid", None) data = { "uid": uid } return jsonify(data), 200, resp_headers