Skip to content

Commit 517d677

Browse files
committed
Implement a sample IDP for CSRF and PKCE tests
1 parent fd5f19e commit 517d677

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

main.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import urllib.parse
2+
3+
from flask import Flask, request, jsonify, url_for, redirect
4+
from oauthlib.oauth2 import Server
5+
6+
from validator import MyRequestValidator
7+
8+
app = Flask(__name__)
9+
10+
oauth2_server = Server(MyRequestValidator())
11+
12+
13+
@app.route('/auth', methods=['GET', 'POST'])
14+
def auth():
15+
if request.method == 'GET':
16+
try:
17+
# Validate the client request for authorization
18+
uri = request.url
19+
http_method = request.method
20+
headers = request.headers
21+
body = request.get_data()
22+
23+
scopes, credentials = oauth2_server.validate_authorization_request(uri, http_method, body, headers)
24+
del credentials['request']
25+
action = url_for('auth') + "?" + urllib.parse.urlencode({"scopes": ','.join(scopes), **credentials})
26+
27+
# Assuming the user is authenticated and named 'user1'
28+
# You can integrate real user authentication here
29+
return f"""
30+
Do you authorize the app to access your data?
31+
<form action="{action}" method="POST">
32+
<button type="submit">Yes</button>
33+
</form>
34+
"""
35+
except:
36+
return "Invalid authorization request", 400
37+
38+
elif request.method == 'POST':
39+
uri = request.url
40+
http_method = request.method
41+
headers = request.headers
42+
body = request.get_data()
43+
44+
headers, body, status = oauth2_server.create_authorization_response(uri, http_method, body, headers)
45+
46+
if status == 302:
47+
location = headers.get('Location', '')
48+
return redirect(location)
49+
50+
return jsonify(body), status
51+
52+
53+
@app.route('/token', methods=['POST'])
54+
def token():
55+
uri = request.url
56+
http_method = request.method
57+
headers = request.headers
58+
body = request.get_data()
59+
60+
headers, body, status = oauth2_server.create_token_response(uri, http_method, body, headers, {})
61+
62+
return body, status
63+
64+
65+
if __name__ == "__main__":
66+
app.run()

validator.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from oauthlib.oauth2 import Client
2+
from oauthlib.oauth2 import RequestValidator
3+
4+
5+
class MyRequestValidator(RequestValidator):
6+
7+
def validate_client_id(self, client_id, request, *args, **kwargs):
8+
return True
9+
10+
def validate_redirect_uri(self, client_id, redirect_uri, request, *args, **kwargs):
11+
return True
12+
13+
def get_default_redirect_uri(self, client_id, request, *args, **kwargs):
14+
return ""
15+
16+
def get_default_scopes(self, client_id, request, *args, **kwargs):
17+
return []
18+
19+
def authenticate_client(self, request, *args, **kwargs):
20+
request.client = Client(client_id="my_client", access_token="my_token")
21+
return True
22+
23+
def confirm_redirect_uri(self, client_id, code, redirect_uri, client, request, *args, **kwargs):
24+
return True
25+
26+
def validate_code(self, client_id, code, client, request, *args, **kwargs):
27+
return True
28+
29+
def validate_scopes(self, client_id, scopes, client, request, *args, **kwargs):
30+
return True
31+
32+
def save_authorization_code(self, client_id, code, request, *args, **kwargs):
33+
return True
34+
35+
def validate_response_type(self, client_id, response_type, client, request, *args, **kwargs):
36+
return True
37+
38+
def validate_grant_type(self, client_id, grant_type, client, request, *args, **kwargs):
39+
return True
40+
41+
def save_bearer_token(self, token, request, *args, **kwargs):
42+
return True
43+
44+
def invalidate_authorization_code(self, client_id, code, request, *args, **kwargs):
45+
return True

0 commit comments

Comments
 (0)