-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
106 lines (91 loc) · 3.94 KB
/
Copy pathapp.py
File metadata and controls
106 lines (91 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import uuid
import glob
import hashlib
from flask import Flask, request, redirect, url_for, render_template, jsonify, send_from_directory
from flask_wtf import FlaskForm
from werkzeug.utils import secure_filename
from wtforms import FileField
from wtforms.validators import DataRequired, ValidationError
import oauth1.authenticationutils as authenticationutils
from oauth1.oauth import OAuth
app = Flask(__name__)
UPLOAD_FOLDER = '.'
app.config['SECRET_KEY'] = '596b365c-7d23-4a5a-ac6a-633cc2427738' # Replace with your actual secret key
app.config['UPLOAD_FOLDER'] = '.'
class FileExtensionValidator:
def __init__(self, ext):
self.ext = ext
def __call__(self, form, field):
if not field.data.filename.endswith(self.ext):
raise ValidationError(f"File must end with '{self.ext}'")
class UploadForm(FlaskForm):
file = FileField('Certificate', validators=[
DataRequired(),
FileExtensionValidator('.p12')
])
def allowed_file(filename):
ALLOWED_EXTENSIONS = {'p12'}
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
form = UploadForm()
if form.validate_on_submit():
file = form.file.data
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_files'))
return render_template('upload.html', form=form)
@app.route('/files', methods=['GET'])
def uploaded_files():
files = [f for f in os.listdir(app.config['UPLOAD_FOLDER']) if f.endswith('.p12')]
return render_template('files.html', files=files)
def generate_oauth_headers(consumer_key, signing_key, keystore_password, uri, http_verb, json_obj):
signing_key = authenticationutils.load_signing_key(signing_key, keystore_password)
authHeader = OAuth.get_authorization_header(uri, http_verb, json_obj, consumer_key, signing_key)
headerdict = {'Authorization': authHeader}
return headerdict
@app.route('/generate_oauth_headers', methods=['POST'])
def generate_headers():
data = request.get_json()
consumer_key = data.get('consumer_key')
signing_key = data.get('signing_key')
keystore_password = data.get('keystore_password')
uri = data.get('uri')
http_verb = data.get('http_verb')
json_obj = data.get('json_obj')
if all(param is not None for param in [consumer_key, signing_key, keystore_password, uri, http_verb, json_obj]):
headers = generate_oauth_headers(consumer_key, signing_key, keystore_password, uri, http_verb, json_obj)
return jsonify(headers)
else:
return jsonify({"error": "Missing required parameters"}), 400
@app.route('/api/upload_certificate', methods=['POST'])
def api_upload_certificate():
if 'file' not in request.files:
return jsonify({'error': 'No file part in the request'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected for uploading'}), 400
if not allowed_file(file.filename):
return jsonify({'error': 'File extension not allowed'}), 400
filename = secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER, filename))
return jsonify({'message': f'Successfully uploaded {filename}'}), 200
@app.route('/api/get_certificates', methods=['GET'])
def api_get_certificates():
files = glob.glob(os.path.join(app.config['UPLOAD_FOLDER'], '*.p12'))
file_objs = []
for file in files:
with open(file, 'rb') as f:
bytes = f.read()
readable_hash = hashlib.sha256(bytes).hexdigest()
file_objs.append({
'name': os.path.basename(file),
'remote_id': readable_hash
})
return jsonify(file_objs), 200
@app.route('/healthcheck', methods=['GET'])
def healthcheck():
return jsonify({"status": "OK"}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=False)