Commit 616917393681084195d37514db5e568d30ff9e16
1 parent
44a25fe29a
add adm auth
Showing 17 changed files with 204 additions and 5 deletions Side-by-side Diff
- __pycache__/main.cpython-310.pyc
- __pycache__/routes.cpython-310.pyc
- components/__pycache__/__init__.cpython-310.pyc
- components/k2auth/.gitignore
- components/k2auth/k2auth/models.py
- components/k2auth/k2auth/views.py
- components/k2auth/requirements.txt
- components/k2test/__pycache__/__init__.cpython-310.pyc
- components/k2test/k2test/__pycache__/__init__.cpython-310.pyc
- components/k2test/k2test/__pycache__/views.cpython-310.pyc
- k2/__pycache__/__init__.cpython-310.pyc
- k2/__pycache__/k2cfg.cpython-310.pyc
- k2/__pycache__/k2comp.cpython-310.pyc
- k2/__pycache__/k2obj.cpython-310.pyc
- k2/__pycache__/k2rout.cpython-310.pyc
- main.py
- routes.py
__pycache__/main.cpython-310.pyc
No preview for this file type
__pycache__/routes.cpython-310.pyc
No preview for this file type
components/__pycache__/__init__.cpython-310.pyc
No preview for this file type
components/k2auth/.gitignore
components/k2auth/k2auth/models.py
| 1 | +import uuid | |
| 2 | +from sqlalchemy import Column, Integer, String | |
| 3 | +from sqlalchemy.ext.declarative import declarative_base | |
| 4 | +from flask_jwt_extended import create_access_token | |
| 5 | +from datetime import timedelta | |
| 6 | +import hashlib | |
| 7 | +from flask import current_app as k2 | |
| 8 | + | |
| 9 | +Base = declarative_base() | |
| 10 | + | |
| 11 | +class k2users(Base): | |
| 12 | + __tablename__ = 'k2users' | |
| 13 | + user_id = Column(Integer, primary_key=True) | |
| 14 | + login = Column(String(50)) | |
| 15 | + name = Column(String(50)) | |
| 16 | + email = Column(String(150)) | |
| 17 | + password = Column(String(50)) | |
| 18 | + phone = Column(String(20)) | |
| 19 | + roleid = Column(String(50)) | |
| 20 | + | |
| 21 | + def __init__(self, **kwargs): | |
| 22 | + self.user_id = hashlib.md5(str(uuid.uuid4()).encode()).hexdigest() | |
| 23 | + self.login = kwargs.get('login') | |
| 24 | + self.password = hashlib.md5(kwargs.get('password').encode()).hexdigest() | |
| 25 | + self.name = kwargs.get('name') | |
| 26 | + self.email = kwargs.get('email') | |
| 27 | + self.phone = kwargs.get('phone') | |
| 28 | + self.roleid = kwargs.get('roleid') | |
| 29 | + | |
| 30 | + def get_token(self, expire_time=24): | |
| 31 | + expires_delta = timedelta(expire_time) | |
| 32 | + token = create_access_token(identity=self.user_id, expires_delta=expires_delta) | |
| 33 | + if self.roleid == '1': | |
| 34 | + role = 'admin' | |
| 35 | + else: | |
| 36 | + role = "client" | |
| 37 | + | |
| 38 | + userData = { | |
| 39 | + "avatar": "/src/assets/images/avatars/avatar-4.png", | |
| 40 | + "email": self.email, | |
| 41 | + "fullName": self.name, | |
| 42 | + "id": self.user_id, | |
| 43 | + "role": role, | |
| 44 | + "username": self.login | |
| 45 | + | |
| 46 | + } | |
| 47 | + if self.roleid != '1': | |
| 48 | + userAbilities = [ | |
| 49 | + { | |
| 50 | + "action": "read", | |
| 51 | + "subject": "Auth"}, | |
| 52 | + | |
| 53 | + { | |
| 54 | + "action": "read", | |
| 55 | + "subject": "AclDemo"} | |
| 56 | + ] | |
| 57 | + else: | |
| 58 | + userAbilities = [ | |
| 59 | + { | |
| 60 | + "action": "manage", | |
| 61 | + "subject": "all" | |
| 62 | + } | |
| 63 | + ] | |
| 64 | + return (token, userData, userAbilities) | |
| 65 | + | |
| 66 | + | |
| 67 | + @classmethod | |
| 68 | + def authenticate(cls, login, password): | |
| 69 | + session = k2.extensions['sqlalchemy'].db.session | |
| 70 | + user = '' | |
| 71 | + try: | |
| 72 | + user = session.query(cls).filter(cls.login == login).filter(cls.password == hashlib.md5(password.encode()).hexdigest()).one() | |
| 73 | + except: | |
| 74 | + user = None | |
| 75 | + return user | |
| 76 | + | |
| 77 | + @classmethod | |
| 78 | + def user_by_login(cls, login): | |
| 79 | + session = k2.extensions['sqlalchemy'].db.session | |
| 80 | + user = session.query(cls).filter(cls.login == login).first() | |
| 81 | + return user |
components/k2auth/k2auth/views.py
| 1 | +# import hashlib | |
| 2 | +# import uuid | |
| 3 | +from flask import Blueprint, request, jsonify | |
| 4 | +from flask import current_app as k2 | |
| 5 | +from flask_jwt_extended import jwt_required | |
| 6 | + | |
| 7 | +from .models import k2users | |
| 8 | + | |
| 9 | +k2auth = Blueprint('k2auth', __name__, template_folder='templates', static_folder='static', static_url_path='/adm') | |
| 10 | + | |
| 11 | + | |
| 12 | +class Auth(): | |
| 13 | + def __init__(self): | |
| 14 | + pass | |
| 15 | + | |
| 16 | + def register(self): | |
| 17 | + params = request.json | |
| 18 | + session = k2.extensions['sqlalchemy'].db.session | |
| 19 | + try: | |
| 20 | + user = k2users(**params) | |
| 21 | + session.add(user) | |
| 22 | + session.commit() | |
| 23 | + token = user.get_token() | |
| 24 | + | |
| 25 | + return {'access_token': token} | |
| 26 | + except: | |
| 27 | + return jsonify({'error': 'Unauthorized'}), 401 | |
| 28 | + | |
| 29 | + | |
| 30 | + def login(self): | |
| 31 | + params = request.json | |
| 32 | + user = k2users.authenticate(**params) | |
| 33 | + if user: | |
| 34 | + token, userData, userAbilities = user.get_token() | |
| 35 | + return {'accessToken': token, "userData": userData, "userAbilities": userAbilities} | |
| 36 | + else: | |
| 37 | + k2.logger.error(('%s failed to log in ', params.get('login'), params.get('password'))) | |
| 38 | + return jsonify({'error': 'Unauthorized'}), 401 | |
| 39 | + | |
| 40 | + | |
| 41 | + @jwt_required() | |
| 42 | + def logout(self): | |
| 43 | + response = { | |
| 44 | + 'message': 'Logged out successfully' | |
| 45 | + } | |
| 46 | + return jsonify(response) | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | +auth = Auth() | |
| 51 | +k2auth.add_url_rule('/login', methods=['POST'], view_func=auth.login) | |
| 52 | +k2auth.add_url_rule('/logout', methods=['GET'], view_func=auth.logout) | |
| 53 | +k2auth.add_url_rule('/register', methods=['POST'], view_func=auth.register) |
components/k2auth/requirements.txt
| 1 | +alembic==1.11.1 | |
| 2 | +Babel==2.12.1 | |
| 3 | +bcrypt==4.0.1 | |
| 4 | +bidict==0.22.1 | |
| 5 | +blinker==1.6.2 | |
| 6 | +certifi==2023.5.7 | |
| 7 | +charset-normalizer==3.1.0 | |
| 8 | +click==8.1.3 | |
| 9 | +colorama==0.4.6 | |
| 10 | +dnspython==2.3.0 | |
| 11 | +email-validator==2.0.0.post2 | |
| 12 | +Flask==2.3.2 | |
| 13 | +flask-babel==3.1.0 | |
| 14 | +Flask-Bcrypt==1.0.1 | |
| 15 | +Flask-Cors==3.0.10 | |
| 16 | +Flask-JWT-Extended==4.5.2 | |
| 17 | +Flask-Login==0.6.2 | |
| 18 | +Flask-Migrate==4.0.4 | |
| 19 | +Flask-MySQLdb==1.0.1 | |
| 20 | +Flask-SQLAlchemy==3.0.3 | |
| 21 | +flask-staticdirs==1.0.1 | |
| 22 | +Flask-WTF==1.1.1 | |
| 23 | +greenlet==2.0.2 | |
| 24 | +idna==3.4 | |
| 25 | +itsdangerous==2.1.2 | |
| 26 | +Jinja2==3.1.2 | |
| 27 | +jsonify==0.5 | |
| 28 | +Mako==1.2.4 | |
| 29 | +MarkupSafe==2.1.2 | |
| 30 | +mysql==0.0.3 | |
| 31 | +mysql-connector==2.2.9 | |
| 32 | +mysql-connector-python==8.0.33 | |
| 33 | +mysqlclient==2.1.1 | |
| 34 | +peppercorn==0.6 | |
| 35 | +protobuf==3.20.3 | |
| 36 | +PyJWT==2.7.0 | |
| 37 | +python-engineio==4.4.1 | |
| 38 | +python-socketio==5.8.0 | |
| 39 | +pytz==2023.3 | |
| 40 | +PyYAML==6.0 | |
| 41 | +requests==2.31.0 | |
| 42 | +six==1.16.0 | |
| 43 | +SQLAlchemy==2.0.15 | |
| 44 | +typing_extensions==4.6.2 | |
| 45 | +urllib3==2.0.2 | |
| 46 | +Werkzeug==2.3.4 | |
| 47 | +WTForms==3.0.1 |
components/k2test/__pycache__/__init__.cpython-310.pyc
No preview for this file type
components/k2test/k2test/__pycache__/__init__.cpython-310.pyc
No preview for this file type
components/k2test/k2test/__pycache__/views.cpython-310.pyc
No preview for this file type
k2/__pycache__/__init__.cpython-310.pyc
No preview for this file type
k2/__pycache__/k2cfg.cpython-310.pyc
No preview for this file type
k2/__pycache__/k2comp.cpython-310.pyc
No preview for this file type
k2/__pycache__/k2obj.cpython-310.pyc
No preview for this file type
k2/__pycache__/k2rout.cpython-310.pyc
No preview for this file type
main.py
routes.py
| ... | ... | @@ -4,8 +4,17 @@ |
| 4 | 4 | app.register_blueprint(components_bp) |
| 5 | 5 | from components.k2test.k2test.views import k2test |
| 6 | 6 | app.register_blueprint(k2test, url_prefix='/k2test') |
| 7 | -from components.grid.grid.views import grid | |
| 8 | -app.register_blueprint(grid, url_prefix='/grid') | |
| 9 | -from components.vue.vue.views import vue2 | |
| 10 | -app.register_blueprint(vue2, url_prefix='/') | |
| 7 | + | |
| 8 | +# from components.grid.grid.views import grid | |
| 9 | +# app.register_blueprint(grid, url_prefix='/grid') | |
| 10 | +# from components.vue.vue.views import vue2 | |
| 11 | +# app.register_blueprint(vue2, url_prefix='/') | |
| 12 | + | |
| 13 | + | |
| 14 | +from components.k2auth.k2auth.views import k2auth | |
| 15 | +app.register_blueprint(k2auth, url_prefix='/k2auth') | |
| 16 | + | |
| 17 | + | |
| 18 | +from components.adm.adm.views import adm | |
| 19 | +app.register_blueprint(adm, url_prefix='/adm') |