Skip to content

Commit c4b367e

Browse files
authored
Merge pull request #3 from GodwinShen/1-new-feature-create-internal-request-handler
Use path variable type in new flask_internal_proxy to reduce repeated…
2 parents c8c2ab1 + 7b2590a commit c4b367e

File tree

1 file changed

+15
-91
lines changed

1 file changed

+15
-91
lines changed

flaskProxyWithAuth.py

Lines changed: 15 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -78,102 +78,26 @@ def index():
7878
# </form>
7979
# ''')
8080

81-
@app.route("/<string:path0>/<string:path1>/<string:path2>/<string:filename>", methods=["GET", "POST", "PUT", "DELETE"])
82-
def _app(path0, path1, path2, filename):
81+
# Custom route to handle arbitrary path sequences
82+
@app.route("/<path:arbitrary_path>", methods=["GET", "POST", "PUT", "DELETE"])
83+
def flask_internal_proxy(arbitrary_path):
8384
user = session.get("user")
84-
print(f"path0: {path0}")
85-
print(f"path1: {path1}")
86-
print(f"path2: {path2}")
87-
print(f"filename: {filename}")
88-
print(f"request method: {request.method}")
8985
if user:
90-
#return jsonify({"message": "You are authenticated!", "user": user}), 200
91-
#if path1=='..':
92-
# path1 = 'immutable' # this is such a kludge, we'll see if it works
93-
target_url = f'http://localhost:{internal_app_port}/{path0}/{path1}/{path2}/{filename}'
94-
95-
return internal_request_handler(request, target_url)
96-
else:
97-
return jsonify({"message": "You are not logged in."}), 401
98-
99-
@app.route("/<string:path1>/<string:path2>", methods=["GET", "POST", "PUT", "DELETE"])
100-
def _app2(path1, path2):
101-
user = session.get("user")
102-
if user:
103-
target_url = f'http://localhost:{internal_app_port}/{path1}/{path2}'
104-
105-
return internal_request_handler(request, target_url)
106-
else:
107-
return jsonify({"message": "You are not logged in."}), 401
108-
109-
@app.route("/<string:data>", methods=["GET", "POST", "PUT", "DELETE"])
110-
def _app3(data):
111-
user = session.get("user")
112-
print(f"data: {data}")
113-
print(f"request method: {request.method}")
114-
allowed_paths = ['plans', 'models', 'scheduling', 'sequencing', 'constraints', 'tags', 'external_sources', 'dictionaries', 'expansion', 'parcels', 'documentation', 'gateway', 'about']
115-
if user:
116-
#if user and (('__data.json' in data) or ('favicon.svg' in data) or (data in allowed_paths) ):
117-
target_url = f'http://localhost:{internal_app_port}/{data}'
118-
119-
return internal_request_handler(request, target_url)
120-
else:
121-
return jsonify({"message": "You are not logged in."}), 401
122-
123-
@app.route("/login", methods=["GET", "POST", "PUT", "DELETE"])
124-
def _app4():
125-
user = session.get("user")
126-
if user:
127-
target_url = f'http://localhost:{internal_app_port}/login'
86+
# Convert the arbitrary path string to a list
87+
path_elements = arbitrary_path.split("/")
88+
# Build the target_url dynamically
89+
target_url = f"http://localhost:{internal_app_port}/" + "/".join(path_elements)
12890

12991
return internal_request_handler(request, target_url)
13092
else:
131-
return jsonify({"message": "You are not logged in."}), 401
132-
133-
@app.route("/_app/<string:filename>", methods=["GET", "POST", "PUT", "DELETE"])
134-
def _app5(filename):
135-
user = session.get("user")
136-
print(f"filename: {filename}")
137-
print(f"request method: {request.method}")
138-
if user:
139-
#if user and (('version.json' in filename) ):
140-
target_url = f'http://localhost:{internal_app_port}/_app/{filename}'
141-
142-
return internal_request_handler(request, target_url)
143-
else:
144-
return jsonify({"message": "You are not logged in."}), 401
145-
146-
@app.route("/<string:path0>/<string:path1>/<string:path2>", methods=["GET", "POST", "PUT", "DELETE"])
147-
def _app6(path0, path1, path2):
148-
user = session.get("user")
149-
if user:
150-
#return jsonify({"message": "You are authenticated!", "user": user}), 200
151-
#if path1=='..':
152-
# path1 = 'immutable' # this is such a kludge, we'll see if it works
153-
target_url = f'http://localhost:{internal_app_port}/{path0}/{path1}/{path2}'
154-
155-
return internal_request_handler(request, target_url)
156-
else:
157-
return jsonify({"message": "You are not logged in."}), 401
158-
159-
@app.route("/<string:path0>/<string:path1>/<string:path2>/<string:path3>/<string:filename>", methods=["GET", "POST", "PUT", "DELETE"])
160-
def _app7(path0, path1, path2, path3, filename):
161-
user = session.get("user")
162-
# print(f"path0: {path0}")
163-
# print(f"path1: {path1}")
164-
# print(f"path2: {path2}")
165-
# print(f"path3: {path3}")
166-
# print(f"filename: {filename}")
167-
# print(f"request method: {request.method}")
168-
if user:
169-
#return jsonify({"message": "You are authenticated!", "user": user}), 200
170-
#if path1=='..':
171-
# path1 = 'immutable' # this is such a kludge, we'll see if it works
172-
target_url = f'http://localhost:{internal_app_port}/{path0}/{path1}/{path2}/{path3}/{filename}'
173-
174-
return internal_request_handler(request, target_url)
175-
else:
176-
return jsonify({"message": "You are not logged in."}), 401
93+
redirect_uri = url_for("oauth2", _external=True)
94+
return oauth.keycloak.authorize_redirect(redirect_uri)
95+
# return render_template_string('''
96+
# <h1>Hello, you are not logged in.</h1>
97+
# <form action="{{ url_for('login_flask') }}" method="post">
98+
# <button type="submit">Login</button>
99+
# </form>
100+
# ''')
177101

178102
# Login page
179103
@app.route("/login_flask", methods=["POST"])

0 commit comments

Comments
 (0)