Skip to content
Snippets Groups Projects
Commit dde2b6c1 authored by Kai Lee's avatar Kai Lee
Browse files

Please enter the commit message for your changes. Lines starting

 with '' will be ignored, and an empty message aborts the commit.

 On branch master

 Initial commit

 Changes to be committed:
	new file:   .idea/encodings.xml
	new file:   .idea/hexang_web.iml
	new file:   .idea/misc.xml
	new file:   .idea/modules.xml
	new file:   .idea/vcs.xml
	new file:   .idea/workspace.xml
	new file:   __init__.py
	new file:   app/__init__.py
	new file:   app/__pycache__/__init__.cpython-37.pyc
	new file:   app/__pycache__/models.cpython-37.pyc
	new file:   app/__pycache__/views.cpython-37.pyc
	new file:   app/forms.py
	new file:   app/models.py
	new file:   app/static/mail_reset_passwd.html
	new file:   app/static/mail_set_passwd.html
	new file:   app/static/reg.html
	new file:   app/static/reset.html
	new file:   app/static/set_pwd.html
	new file:   app/views.py
	new file:   config.py
	new file:   instance/__init__.py
	new file:   instance/__pycache__/__init__.cpython-37.pyc
	new file:   instance/__pycache__/config.cpython-37.pyc
	new file:   instance/config.py
	new file:   run.py
	new file:   test.py
parents
No related branches found
No related tags found
No related merge requests found
Showing
with 305 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/hexang_web.iml" filepath="$PROJECT_DIR$/.idea/hexang_web.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
from flask import Flask
from instance.config import LDAP_CONFIG,flask_key
from .models import ldap_control
l = ldap_control(LDAP_CONFIG)
s = Flask(__name__)
s.secret_key = flask_key
\ No newline at end of file
File added
File added
File added
from ldap3 import Server,Connection,HASHED_MD5,MODIFY_REPLACE
from ldap3.utils.hashed import hashed
from re import match
class ldap_control:
def __init__(self,LDAP_CONFIG):
"""
LDAP_CONFIG ={ "port" : "389",
"host":"example.com",
"admin_dn" :"cn=admin,dc=example,dc=com",
"admin_passwd" : "tttttttt",
"user_dn" : "dc=example,dc=com"}
"""
self.user_dn,self.admin_dn,self.admin_passwd,self.host,self.port = LDAP_CONFIG["user_dn"],LDAP_CONFIG["admin_dn"],LDAP_CONFIG["admin_passwd"],LDAP_CONFIG["host"],LDAP_CONFIG["port"]
self.s = Server(host = self.host,port = self.port)
self.c = Connection(self.s,user = self.admin_dn,password = self.admin_passwd,auto_bind=True)
def result(self):
print(self.c.result)
def chech_email_or_name(self,str):
"""
判断 输入为用户名还是邮箱
:param str: 用户输入
:return: ldap 查询用数据
"""
if '@' in str :
result = f'mail={str}'
else:
result = f'cn={str}'
return result
def check_in(self,user_input):
input = self.chech_email_or_name(user_input)
self.c.search(self.user_dn,f'({input})')
print(self.c.entries)
if (self.c.entries ==[]):
return 0
else:
return 1
def add_user(self,username,email,description,passwd):
"""
:param username:
:param email:
:param description:
:return: 1,成功 0,失败
"""
att ={"cn":f'{username}',
"Mail":f"{email}",
"sn":f"{description}",
"userPassword":""
}
#try:
self.c.add(dn = f'cn={username},{self.user_dn}', object_class=['inetOrgPerson', 'top'], attributes=att)
self.reset_passwd(email,passwd)
print(self.c.result)
return 1
#except:
#return 0
def reset_passwd(self,user__input,newpasswd):
"""
通过用户名 (CN) 或邮箱(mail)重置密码
:param username:
:param newpasswd:
:return: 1 成功 0 失败
"""
input = self.chech_email_or_name(user__input)
user_dn = f'cn={user__input},{self.user_dn}'
hashed_password = hashed(HASHED_MD5, newpasswd)
changes = {
'userPassword': [(MODIFY_REPLACE, [hashed_password])]
}
#try:
self.c.modify(user_dn, changes=changes)
print(self.c.result)
return 1
#except:
#return 0
class User :
def __init__(self,user_info):
self.name = user_info['user_name']
self.mail = user_info['email']
#if self.check_user_input_mail(self.mail) == 0:
#return(0)
#if self.check_user_input_name(self.name) == 0:
#return(0)
self.passwd = user_info['password']
self.description = user_info['description']
def check_user_input_name(string):
if string.isalnum() :
return 1
else:
return 0
def check_user_input_mail(string):
if match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$", string):
return 1
else :
return 0
def creat_ldap_account(self,ldap_control):
ldap_control.add_user(self.name,self.mail,self.description,self.passwd)
"""
Hi {user},
This is hexang.
Please reset your account ({mail}) password through the following link:
{url}
---
Best regards,
Team hexang
"""
\ No newline at end of file
"""
Hi {name},
Welcome to hexang!
Please activate your account ({mail}) through the following link:
{url}
Have fun!
---
Best regards,
Team hexang
"""
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
from . import s
from flask import request,send_file,session
from . import l
from .models import User
from instance.config import ts,salt,MAIL
from flask_mail import Mail,Message
s.config['MAIL_SERVER'] = MAIL['smtp_server']
s.config['MAIL_PORT'] = MAIL['port']
s.config['MAIL_USE_SSL'] = True
s.config['MAIL_USERNAME'] = MAIL['from_addr']
s.config['MAIL_PASSWORD'] = MAIL['password']
m = Mail(s)
@s.route('/register/',methods = ["GET","POST"])
def reg_user():
if request.method == "GET":
return send_file('./static/reg.html')
elif request.method == "POST":
user_input = request.form
u = User(user_input)
u.creat_ldap_account(l)
return """
ok
"""
@s.route('/passwd-reset/',methods = ["GET","POST"])
def set_token():
if request.method == "GET":
return send_file('./static/reset.html')
elif request.method == "POST":
mail = request.form['mail']
if (User.check_user_input_mail(string=mail) and l.check_in(mail)):
token = ts.dumps(mail,salt=salt )
url = f"/rest/{token}"
reset_passwd = f"""
Hi user,
This is hexang.
Please reset your account ({mail}) password through the following link:
{url}
---
Best regards,
Team hexang
"""
print(url)
msg = Message(reset_passwd,recipients=[mail])
#m.send(msg)
return "ok"
else:
return "error"
@s.route('/rest/<path:token>/',methods=['GET'])
def check_token(token):
if request.method == 'GET':
#try :
session["mail"] = ts.loads(token,salt = salt,max_age = 3600)
return send_file('static/set_pwd.html')
#except:
#return "403"
@s.route('/rest/',methods=['POST'])
def rest_passwd():
if request.method == 'POST':
try:
mail = session.get("mail")
passwd = request.form['password']
l.reset_passwd(mail,passwd)
return "ok"
except:
return "error"
#重写各种 return
#邮箱
#重写 重置密码方法
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment