• BaiJiangJie's avatar
    Dev (#2838) · 9ca4a8c9
    BaiJiangJie authored
    * Dev ansible windows 2 (#2783)
    
    * [Update] 改密支持windows
    
    * [Update] 修改asset表结构
    
    * [Feature] Windows支持批量改密、测试可连接性等功能
    
    * [Update] 处理创建资产时labels的问题
    
    * [Update] 优化测试管理系统、系统用户可连接性任务执行逻辑
    
    * [Update] 优化ansible任务逻辑;添加自动推送rdp系统用户功能
    
    * [Update] 添加翻译
    
    * [Update] 优化ansible任务逻辑(测试系统用户可连接性, 通过协议过滤资产)
    
    * [Update] 更新翻译
    
    * [Update] 更新翻译
    
    * [Update] 推送windows系统用户,默认添加到Users、Remote Desktop Users组中
    
    * [Update] 优化小细节
    
    * [Update] 更新翻译,删除多余代码
    
    * [Update] 更新翻译信息
    
    * [Bugfix] 修复windows推送系统用户小bug (#2794)
    
    * [Update] 邮件设置添加配置项:发送账号 (#2796)
    
    * [Bugfix] 和资产相关的Serializer添加protocols字段; (#2800)
    
    * [Bugfix] 和资产相关的Serializer添加protocols字段;
    
    * [Bugfix] RemoteApp Form 修改过滤RDP协议资产
    
    * [Bugfix] 修改小问题
    
    * [Update] 用户授权相关API,如果需要切换到root org (#2803)
    
    * [Update] 用户授权相关API,如果需要切换到root org
    
    * [Update] 优化小问题
    
    * [Update] 增加审计员权限控制 (#2792)
    
    * [Update] 审计员
    
    * [Update] 增加审计员的权限控制
    
    * [Update] 增加审计员Api全校的控制
    
    * [Update] 优化auditor的api权限控制
    
    * [Update] 优化审计员权限控制
    
    * [Update]优化管理员权限的View
    
    * [Update] 优化超级管理权限的View
    
    * [Update] 添加审计员切换组织查询会话管理数据
    
    * [Update] 前端禁用审计员在线会话终断按钮
    
    * [Update]优化细节问题
    
    * [Update] Auth Info (#2806)
    
    * [Update] 修改支持auth info导出
    
    * [Update] 统一认证查看
    
    * [Update] 修改auth book manager
    
    * [Update] 修改auth info
    
    * [Update] 完成修改auth info
    
    * [Update] 优化api
    
    * [Update] 修改assets 的related
    
    * [Update] serializer mixin继承 (#2810)
    
    * [Update] serializer mixin继承
    
    * [Update] 修改system user更新serialzier
    
    * [Update] 修改success message
    
    * [Update] 添加一键禁用LDAP认证脚本 (#2813)
    
    * [Update] 修改资产创建格式
    
    * [Update] 兼容之前的protocols格式
    
    * [Update] Merge master_bugfix to dev_bugfix (#2817)
    
    * [Update] 邮件设置添加配置项:发送账号 (#2795)
    
    * [Bugfix] 修复普通用户被授权的RemoteApp列表加载为空的bug
    
    * [Bugfix] 修复普通用户加载被授权的RemoteApp为空的bug
    
    * [Update] 修改邮件测试的接受者为发送者
    
    * [Update] 修改小问题
    
    * [Update] 修改资产授权序列类返回资产protocols的协议格式/, 同时添加protocol和port字段
    
    * [Update] 修改文案 (#2823)
    
    * [Update] 修改文案
    
    * [Update] 修改文案2
    
    * [Bugfix] 修复资产没有管理用户时获取connectivity字段失败的bug
    
    * [Update] 优化测试可连接性时结果获取 (#2825)
    
    * [Update] 修改资产使用patch方法更新时页面不提示messages信息
    
    * [Update] 添加迁移文件,修改设置资产可连接性时管理用户为None的bug
    
    * [Update] 修改org.middleware自动切换组织的bug (#2829)
    
    * [Update] 修改org.middleware自动切换组织的bug
    
    * [Update] 将切换组织逻辑移动到PermsUtil中
    
    * [Update] 修改首页组织名称显示来源
    9ca4a8c9
api.py 6.19 KB
# -*- coding: utf-8 -*-
#

import uuid
import time

from django.core.cache import cache
from django.conf import settings
from django.utils.translation import ugettext as _
from django.utils.six import text_type
from django.contrib.auth import get_user_model
from rest_framework import HTTP_HEADER_ENCODING
from rest_framework import authentication, exceptions
from rest_framework.authentication import CSRFCheck

from common.utils import get_object_or_none, make_signature, http_to_unixtime
from ..models import AccessKey, PrivateToken


def get_request_date_header(request):
    date = request.META.get('HTTP_DATE', b'')
    if isinstance(date, text_type):
        # Work around django test client oddness
        date = date.encode(HTTP_HEADER_ENCODING)
    return date


class AccessKeyAuthentication(authentication.BaseAuthentication):
    """App使用Access key进行签名认证, 目前签名算法比较简单,
    app注册或者手动建立后,会生成 access_key_id 和 access_key_secret,
    然后使用 如下算法生成签名:
    Signature = md5(access_key_secret + '\n' + Date)
    example: Signature = md5('d32d2b8b-9a10-4b8d-85bb-1a66976f6fdc' + '\n' +
                    'Thu, 12 Jan 2017 08:19:41 GMT')
    请求时设置请求header
    header['Authorization'] = 'Sign access_key_id:Signature' 如:
    header['Authorization'] =
        'Sign d32d2b8b-9a10-4b8d-85bb-1a66976f6fdc:OKOlmdxgYPZ9+SddnUUDbQ=='

    验证时根据相同算法进行验证, 取到access_key_id对应的access_key_id, 从request
    headers取到Date, 然后进行md5, 判断得到的结果是否相同, 如果是认证通过, 否则 认证
    失败
    """
    keyword = 'Sign'

    def authenticate(self, request):
        auth = authentication.get_authorization_header(request).split()
        if not auth or auth[0].lower() != self.keyword.lower().encode():
            return None

        if len(auth) == 1:
            msg = _('Invalid signature header. No credentials provided.')
            raise exceptions.AuthenticationFailed(msg)
        elif len(auth) > 2:
            msg = _('Invalid signature header. Signature '
                    'string should not contain spaces.')
            raise exceptions.AuthenticationFailed(msg)

        try:
            sign = auth[1].decode().split(':')
            if len(sign) != 2:
                msg = _('Invalid signature header. '
                        'Format like AccessKeyId:Signature')
                raise exceptions.AuthenticationFailed(msg)
        except UnicodeError:
            msg = _('Invalid signature header. '
                    'Signature string should not contain invalid characters.')
            raise exceptions.AuthenticationFailed(msg)

        access_key_id = sign[0]
        try:
            uuid.UUID(access_key_id)
        except ValueError:
            raise exceptions.AuthenticationFailed('Access key id invalid')
        request_signature = sign[1]

        return self.authenticate_credentials(
            request, access_key_id, request_signature
        )

    @staticmethod
    def authenticate_credentials(request, access_key_id, request_signature):
        access_key = get_object_or_none(AccessKey, id=access_key_id)
        request_date = get_request_date_header(request)
        if access_key is None or not access_key.user:
            raise exceptions.AuthenticationFailed(_('Invalid signature.'))
        access_key_secret = access_key.secret

        try:
            request_unix_time = http_to_unixtime(request_date)
        except ValueError:
            raise exceptions.AuthenticationFailed(
                _('HTTP header: Date not provide '
                  'or not %a, %d %b %Y %H:%M:%S GMT'))

        if int(time.time()) - request_unix_time > 15 * 60:
            raise exceptions.AuthenticationFailed(
                _('Expired, more than 15 minutes'))

        signature = make_signature(access_key_secret, request_date)
        if not signature == request_signature:
            raise exceptions.AuthenticationFailed(_('Invalid signature.'))

        if not access_key.user.is_active:
            raise exceptions.AuthenticationFailed(_('User disabled.'))
        return access_key.user, None


class AccessTokenAuthentication(authentication.BaseAuthentication):
    keyword = 'Bearer'
    model = get_user_model()
    expiration = settings.TOKEN_EXPIRATION or 3600

    def authenticate(self, request):
        auth = authentication.get_authorization_header(request).split()
        if not auth or auth[0].lower() != self.keyword.lower().encode():
            return None

        if len(auth) == 1:
            msg = _('Invalid token header. No credentials provided.')
            raise exceptions.AuthenticationFailed(msg)
        elif len(auth) > 2:
            msg = _('Invalid token header. Sign string '
                    'should not contain spaces.')
            raise exceptions.AuthenticationFailed(msg)

        try:
            token = auth[1].decode()
        except UnicodeError:
            msg = _('Invalid token header. Sign string '
                    'should not contain invalid characters.')
            raise exceptions.AuthenticationFailed(msg)
        return self.authenticate_credentials(token)

    def authenticate_credentials(self, token):
        user_id = cache.get(token)
        user = get_object_or_none(self.model, id=user_id)

        if not user:
            msg = _('Invalid token or cache refreshed.')
            raise exceptions.AuthenticationFailed(msg)
        return user, None


class PrivateTokenAuthentication(authentication.TokenAuthentication):
    model = PrivateToken


class SessionAuthentication(authentication.SessionAuthentication):
    def authenticate(self, request):
        """
        Returns a `User` if the request session currently has a logged in user.
        Otherwise returns `None`.
        """

        # Get the session-based user from the underlying HttpRequest object
        user = getattr(request._request, 'user', None)

        # Unauthenticated, CSRF validation not required
        if not user or not user.is_active:
            return None

        try:
            self.enforce_csrf(request)
        except exceptions.AuthenticationFailed:
            return None

        # CSRF passed with authenticated user
        return user, None