-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_api1.py
144 lines (103 loc) · 4.5 KB
/
app_api1.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import subprocess
import uuid
import shutil
import json
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
def is_json(myjson):
try:
json.loads(myjson)
except ValueError as e:
return False
return True
def generatecsrfn(country, state, location, org, orgunit, commonname, keysize):
dirname=str(uuid.uuid4())
algo_size = "rsa:"+ str(keysize)
f_prm_2 = "/C="+country+"/ST="+state+"/L="+location+"/O="+org+"/CN=" + commonname
if (orgunit):
f_prm_2 = f_prm_2 + "/OU="+ str(orgunit)
subprocess.run(["mkdir",dirname])
res2 = os.getcwd()
strdir = os.path.join(res2, dirname)
os.chdir(strdir)
result_final = subprocess.run(["openssl","req","-new", "-newkey", algo_size,"-nodes","-out", "mycsr.csr", "-keyout" ,"mykey.key", "-subj",f_prm_2], capture_output=True, text=True)
test_csr_path = os.path.join(strdir, "mycsr.csr")
test_key_path = os.path.join(strdir, "mykey.key")
test_csr = open(test_csr_path,"r")
test_key = open(test_key_path,"r")
data_csr = test_csr.read()
data_key = test_key.read()
test_csr.close()
test_key.close()
f_result = { "csr" : data_csr, "key" : data_key }
path_parent = os.path.dirname(os.getcwd())
os.chdir(path_parent)
shutil.rmtree(strdir, ignore_errors=True)
return data_csr, data_key
@app.route('/', methods=['GET'])
@cross_origin()
def welcome():
return """<p>
<HTML>
<TITLE>CSR API Generator</TITLE>
<head>
<!-- Place this tag in your head or just before your close body tag. -->
<script async defer src="https://buttons.github.io/buttons.js"></script>
</head>
<BODY>
<p>
CSR Generator API is working...</p>
<br/>
<!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/tjhackzy" data-size="large" aria-label="Follow @tjhackzy on GitHub">Follow @tjhackzy</a>
<!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/tjhackzy/PyCSRAPI/fork" data-icon="octicon-repo-forked" data-size="large" aria-label="Fork tjhackzy/PyCSRAPI on GitHub">Fork</a>
<!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/tjhackzy/PyCSRAPI/archive/HEAD.zip" data-icon="octicon-download" data-size="large" aria-label="Download tjhackzy/PyCSRAPI on GitHub">Download</a>
</BODY></HTML>
"""
@app.route('/generate', methods=['POST'])
@cross_origin()
def generatecsr():
if (is_json(request.data) is False):
f_result = { "csr" : "", "key" : "" , "error":"Missing Json in Request." }
response_final = json.dumps(f_result)
return response_final,400
ret_obj = request.get_json()
commonname = ret_obj.get("commonname")
if(commonname is None):
f_result = { "csr" : "", "key" : "" , "error":"Common name is a required field." }
response_final = json.dumps(f_result)
return response_final,400
country = ret_obj.get("country")
if(country is None):
f_result = { "csr" : "", "key" : "" , "error":"Country is a required field." }
response_final = json.dumps(f_result)
return response_final,400
state = ret_obj.get("state")
if(state is None):
f_result = { "csr" : "", "key" : "" , "error":"State is a required field." }
response_final = json.dumps(f_result)
return response_final,400
locality = ret_obj.get("locality")
if(locality is None):
f_result = { "csr" : "", "key" : "" , "error":"Locality is a required field." }
response_final = json.dumps(f_result)
return response_final,400
organization = ret_obj.get("organization")
if(organization is None):
f_result = { "csr" : "", "key" : "" , "error":"Organization is a required field." }
response_final = json.dumps(f_result)
return response_final,400
organizationunit=ret_obj.get("organizationunit")
keysize = ret_obj.get("keysize")
final_result_json = generatecsrfn(country,state,locality,organization, organizationunit, commonname, keysize)
f_result = { "csr" : final_result_json[0], "key" : final_result_json[1] , "error":"" }
response_final = json.dumps(f_result)
return response_final
if __name__ == '__main__':
app.run(host='0.0.0.0', port=11055)