forked from suryasr007/random-password-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (74 loc) · 2.56 KB
/
main.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
from flask import Flask, jsonify, abort, render_template, request
from flask_restful import Resource, Api, reqparse
from webargs import fields, validate
from webargs.flaskparser import use_args
from password_generator import PasswordGenerator
import re
app = Flask(__name__)
api = Api(app, prefix="/api")
parser = reqparse.RequestParser()
'''
Defaults
self.minlen = 6
self.maxlen = 16
self.minuchars = 1
self.minlchars = 1
self.minnumbers = 1
self.minschars = 1
self.excludeuchars = ""
self.excludelchars = ""
self.excludenumbers = ""
self.excludeschars = ""
'''
pwg = PasswordGenerator()
password_generator_args = {
'minlen': fields.Int(missing=6, validate=lambda val: val > 0),
'maxlen': fields.Int(missing=16, validate=lambda val: val > 0),
'minuchars': fields.Int(missing=1, validate=lambda val: val > 0),
'minuchars': fields.Int(missing=1, validate=lambda val: val > 0),
'minlchars': fields.Int(missing=1, validate=lambda val: val > 0),
'excludeuchars': fields.Str(missing=''),
'excludelchars': fields.Str(missing=''),
'excludenumbers': fields.Str(missing=''),
'excludeschars': fields.Str(missing='')
}
shuffle_password_args = {
'maxlen': fields.Int(required={'message': 'maximum length required', 'code': 400}),
'password': fields.Str(required={'message': 'Password required', 'code': 400})
}
non_duplicate_args = {
'maxlen': fields.Int(required={'message': 'maximum length required', 'code': 400})
}
class PasswordGenerator(Resource):
@use_args(password_generator_args)
def get(self, args):
try:
pwg.__dict__.update(args)
res = pwg.generate()
except Exception as e:
abort(404, str(e))
return jsonify({'password': res})
class ShufflePassword(Resource):
@use_args(shuffle_password_args)
def get(self, args):
try:
res = pwg.shuffle_password(password=args["password"], maxlen=args["maxlen"])
except Exception as e:
abort(404, str(e))
return jsonify({'password': res})
class NonDuplicatePassword(Resource):
@use_args(non_duplicate_args)
def get(self, args):
try:
res = pwg.non_duplicate_password(maxlen=args["maxlen"])
except Exception as e:
abort(404, str(e))
return jsonify({'password': res})
@app.route('/', methods=["GET"])
def home():
return render_template("home.html")
api.add_resource(PasswordGenerator, '/generate')
api.add_resource(ShufflePassword, '/shuffle')
api.add_resource(NonDuplicatePassword, '/nonduplicate')
if __name__ == '__main__':
app.run()