Commit 616917393681084195d37514db5e568d30ff9e16
1 parent
44a25fe29a
add adm auth
Showing 17 changed files with 204 additions and 5 deletions Inline 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
| File was created | 1 | idea | |
| 2 | __pycache__/ | ||
| 3 | */__pycache__/ | ||
| 4 | *.py[cod] | ||
| 5 | .pyc | ||
| 6 | .idea/ | ||
| 7 | venv | ||
| 8 | |||
| 9 |
components/k2auth/k2auth/models.py
| File was created | 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 | ||
| 82 |
components/k2auth/k2auth/views.py
| File was created | 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) | ||
| 54 |
components/k2auth/requirements.txt
| File was created | 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 | ||
| 48 |
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
| 1 | from flask_cors import CORS | 1 | from flask_cors import CORS |
| 2 | from flask_babel import Babel | 2 | from flask_babel import Babel |
| 3 | from flask_jwt_extended import JWTManager | 3 | from flask_jwt_extended import JWTManager |
| 4 | #from k2.k2inst import * | 4 | #from k2.k2inst import * |
| 5 | from flask import Flask | 5 | from flask import Flask |
| 6 | from k2.k2cfg import k2 | 6 | from k2.k2cfg import k2 |
| 7 | 7 | ||
| 8 | # configure flask app | 8 | # configure flask app |
| 9 | app = Flask(__name__) | 9 | app = Flask(__name__) |
| 10 | app.config['SECRET_KEY'] = k2.secret_key | 10 | app.config['SECRET_KEY'] = k2.secret_key |
| 11 | app.json.sort_keys = k2.json_sort_keys | 11 | app.json.sort_keys = k2.json_sort_keys |
| 12 | #app.config.from_object(k2) | 12 | #app.config.from_object(k2) |
| 13 | 13 | ||
| 14 | # init db | 14 | # init db |
| 15 | k2().init_db(app) | 15 | k2().init_db(app) |
| 16 | CORS(app) | 16 | CORS(app) |
| 17 | jwt = JWTManager(app) | 17 | jwt = JWTManager(app) |
| 18 | 18 | ||
| 19 | #translate | 19 | #translate |
| 20 | k2.load_babel_translation_directories() | 20 | k2.load_babel_translation_directories() |
| 21 | app.config['BABEL_TRANSLATION_DIRECTORIES'] = k2.babel_translation_directories | 21 | app.config['BABEL_TRANSLATION_DIRECTORIES'] = k2.babel_translation_directories |
| 22 | babel = Babel(app) | 22 | babel = Babel(app) |
| 23 | babel.init_app(app, locale_selector=k2.get_locale) | 23 | babel.init_app(app, locale_selector=k2.get_locale) |
| 24 | 24 | ||
| 25 | # flask routing | 25 | # flask routing |
| 26 | from routes import * | 26 | from routes import * |
| 27 | 27 | ||
| 28 | |||
| 28 | if __name__ == '__main__': | 29 | if __name__ == '__main__': |
| 29 | app.run(debug=True, port=5000) | 30 | app.run(debug=True, port=5100) |
| 30 | 31 | ||
| 31 | 32 | ||
| 32 | 33 | ||
| 33 | 34 | ||
| 34 | 35 | ||
| 35 | 36 |
routes.py
| 1 | #app = Flask(__name__) | 1 | #app = Flask(__name__) |
| 2 | from main import app | 2 | from main import app |
| 3 | from k2.k2rout import components_bp | 3 | from k2.k2rout import components_bp |
| 4 | app.register_blueprint(components_bp) | 4 | app.register_blueprint(components_bp) |
| 5 | from components.k2test.k2test.views import k2test | 5 | from components.k2test.k2test.views import k2test |
| 6 | app.register_blueprint(k2test, url_prefix='/k2test') | 6 | app.register_blueprint(k2test, url_prefix='/k2test') |
| 7 | from components.grid.grid.views import grid | 7 | |
| 8 | app.register_blueprint(grid, url_prefix='/grid') | 8 | # from components.grid.grid.views import grid |
| 9 | from components.vue.vue.views import vue2 | 9 | # app.register_blueprint(grid, url_prefix='/grid') |
| 10 | app.register_blueprint(vue2, url_prefix='/') | 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') |