|
| 1 | +""" rmon.views.decorators |
| 2 | +
|
| 3 | +该模块实现了视图控制器装饰器 |
| 4 | +""" |
| 5 | + |
| 6 | +from functools import wraps |
| 7 | +from flask import g, request |
| 8 | + |
| 9 | +from rmon.common.errors import RestError, AuthenticationError |
| 10 | +from rmon.models import User |
| 11 | + |
| 12 | + |
| 13 | +class ObjectMustBeExist: |
| 14 | + """该装饰器确保操作的对象必须存在 |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, object_class): |
| 18 | + """ |
| 19 | + Args: |
| 20 | + object_class (class): 数据库对象 |
| 21 | + """ |
| 22 | + |
| 23 | + self.object_class = object_class |
| 24 | + |
| 25 | + def __call__(self, func): |
| 26 | + """装饰器实现 |
| 27 | + """ |
| 28 | + @wraps(func) |
| 29 | + def wrapper(*args, **kwargs): |
| 30 | + """ |
| 31 | + Args: |
| 32 | + object_id (int): SQLAlchemy object id |
| 33 | + """ |
| 34 | + |
| 35 | + object_id = kwargs.get('object_id') |
| 36 | + if object_id is None: |
| 37 | + raise RestError(404, 'object not exist') |
| 38 | + |
| 39 | + obj = self.object_class.query.get(object_id) |
| 40 | + if obj is None: |
| 41 | + raise RestError(404, 'object not exist') |
| 42 | + |
| 43 | + g.instance = obj |
| 44 | + return func(*args, **kwargs) |
| 45 | + |
| 46 | + return wrapper |
| 47 | + |
| 48 | + |
| 49 | +class TokenAuthenticate: |
| 50 | + """通过 jwt 认证用户 |
| 51 | +
|
| 52 | + 验证 HTTP Authorization 头所包含的 token |
| 53 | + """ |
| 54 | + |
| 55 | + def __init__(self, admin=True, verify_exp=True): |
| 56 | + """ |
| 57 | + Args: |
| 58 | + admin(bool): 是否需要验证管理员权限 |
| 59 | + """ |
| 60 | + self.admin = admin |
| 61 | + self.verify_exp = verify_exp |
| 62 | + |
| 63 | + def __call__(self, func): |
| 64 | + """装饰器实现 |
| 65 | + """ |
| 66 | + @wraps(func) |
| 67 | + def wrapper(*args, **kwargs): |
| 68 | + |
| 69 | + pack = request.headers.get('Authorization', None) |
| 70 | + if pack is None: |
| 71 | + raise AuthenticationError(401, 'token not found') |
| 72 | + parts = pack.split() |
| 73 | + # Authorization 头部值必须为 'jwt <token_value>' 这种形式 |
| 74 | + if parts[0].lower() != 'jwt': |
| 75 | + raise AuthenticationError(401, 'invalid token header') |
| 76 | + elif len(parts) == 1: |
| 77 | + raise AuthenticationError(401, 'token missing') |
| 78 | + elif len(parts) > 2: |
| 79 | + raise AuthenticationError(401, 'invalid token') |
| 80 | + token = parts[1] |
| 81 | + user = User.verify_token(token, verify_exp=self.verify_exp) |
| 82 | + |
| 83 | + # 如果需要验证是否是管理员 |
| 84 | + if self.admin and not user.is_admin: |
| 85 | + raise AuthenticationError(403, 'no permission') |
| 86 | + |
| 87 | + # 将当前用户存入到 g 对象中 |
| 88 | + g.user = user |
| 89 | + return func(*args, **kwargs) |
| 90 | + return wrapper |
| 91 | + |
0 commit comments