Exemplo n.º 1
0
    def has_permission(self, view):
        # return True
        token = view.request.headers.get('AUTHORIZATION')
        permission = view.required_project_permission() if hasattr(
            view, 'required_project_permission') else None

        if not token:
            return False

        if token[:len(self.token_prefix)] == self.token_prefix:
            token = token[len(self.token_prefix):]

            result = project_auth(token, permission)

            if result.get('warning'):
                view.headers['X-Application-Warning'] = result['warning']

            return result['result']
        elif token[:len(self.project_token_prefix
                        )] == self.project_token_prefix:
            token = token[len(self.project_token_prefix):]

            result = project_auth(token, permission)

            if result.get('warning'):
                view.headers['X-Application-Warning'] = result['warning']

            return result['result']
        else:
            return False
Exemplo n.º 2
0
    def has_permission(self, view, request):
        # return True
        token = self.parse_token(request.headers.get('AUTHORIZATION'))
        view_permissions = view.required_project_permission(
            request) if hasattr(view, 'required_project_permission') else None

        if not token:
            return False

        bridge_settings = request.get_bridge_settings()

        if bridge_settings:
            project_token = bridge_settings.get('token')
            project = bridge_settings.get('project')
        else:
            project_token = settings.TOKEN
            project = settings.PROJECT

        if token['type'] == self.jwt_token_prefix:
            JWT_VERIFY_KEY = '\n'.join([
                line.lstrip() for line in settings.JWT_VERIFY_KEY.split('\\n')
            ])

            try:
                result = jwt.decode(token['value'],
                                    key=JWT_VERIFY_KEY,
                                    algorithms=['RS256'])
            except PyJWTError:
                return False

            user_permissions = result.get('projects', {}).get(project)

            if user_permissions is None:
                return False

            return self.has_view_permissions(view_permissions,
                                             user_permissions)
        elif token['type'] == self.user_token_prefix:
            result = project_auth(token['value'], project_token,
                                  view_permissions, token['params'])

            # if result.get('warning'):
            #     view.headers['X-Application-Warning'] = result['warning']

            return result['result']
        elif token['type'] == self.project_token_prefix:
            result = project_auth(token['value'], project_token,
                                  view_permissions, token['params'])

            # if result.get('warning'):
            #     view.headers['X-Application-Warning'] = result['warning']

            return result['result']
        else:
            return False
Exemplo n.º 3
0
    def has_permission(self, view):
        # return True
        token = view.request.headers.get('AUTHORIZATION')
        permission = view.required_project_permission() if hasattr(
            view, 'required_project_permission') else None

        if not token:
            return False

        bridge_settings_encoded = view.request.headers.get('X_BRIDGE_SETTINGS')

        if bridge_settings_encoded:
            from jet_bridge_base.utils.crypt import decrypt

            try:
                secret_key = settings.TOKEN.replace('-', '').lower()
                bridge_settings = json.loads(
                    decrypt(bridge_settings_encoded, secret_key))
            except Exception:
                bridge_settings = {}

            project_token = bridge_settings.get('token')
        else:
            project_token = settings.TOKEN

        if token[:len(self.token_prefix)] == self.token_prefix:
            token = token[len(self.token_prefix):]

            result = project_auth(token, project_token, permission)

            # if result.get('warning'):
            #     view.headers['X-Application-Warning'] = result['warning']

            return result['result']
        elif token[:len(self.project_token_prefix
                        )] == self.project_token_prefix:
            token = token[len(self.project_token_prefix):]

            result = project_auth(token, project_token, permission)

            # if result.get('warning'):
            #     view.headers['X-Application-Warning'] = result['warning']

            return result['result']
        else:
            return False