예제 #1
0
def token_obtain_sliding_login_handler(request, username: '******',
                                       password: '******') -> dict:
    """
    Takes a set of user credentials and returns a sliding JSON web token to
    prove the authentication of those credentials.
    
    通过用户信息获取所属角色的界面权限并返回/前端根据返回权限进行渲染

    """
    ser = TokenObtainSlidingSerializer(data={
        get_username_field(): username,
        'password': password
    })
    try:
        ser.is_valid(raise_exception=True)
    except:
        raise InvalidUser('用户名/密码输入错误')
    update_last_login(None, ser.user)
    session_user_update(request, ser.user)
    pages_data = get_page_via_user(user_id=ser.user.id)
    pages_df = pd.DataFrame(pages_data)
    pages, _ = get_tree(pages_df, 'page_id', 'parent_id')
    print(pages)
    res = {'token': ser.validated_data.get('token'), 'pages': pages}
    return res
예제 #2
0
def token_obtain_sliding_login_handler(request, username: '******',
                                       password: '******') -> dict:
    """
    Takes a set of user credentials and returns a sliding JSON web token to
    prove the authentication of those credentials.
    """
    # if already login, flush session data.
    try:
        session_logout(request)
    except:
        pass
    user_name_field = get_username_field()
    ser = TokenObtainSlidingSerializer(data={
        user_name_field: username,
        'password': password
    })
    try:
        ser.is_valid(raise_exception=True)
        user = ser.user
    except:
        raise InvalidUser('用户名/密码输入错误')
    update_last_login(None, user)
    session_user_update(request, user)
    res = {'token': ser.validated_data.get('token')}
    return res
예제 #3
0
    def test_it_should_produce_a_json_web_token_when_valid(self):
        s = TokenObtainSlidingSerializer(context=MagicMock(), data={
            TokenObtainSlidingSerializer.username_field: self.username,
            'password': self.password,
        })

        self.assertTrue(s.is_valid())
        self.assertIn('token', s.validated_data)

        # Expecting token type claim to be correct for sliding token.  If this
        # is the case, instantiating a `SlidingToken` instance with encoded
        # token should not raise an exception.
        SlidingToken(s.validated_data['token'])
예제 #4
0
def token_obtain_sliding_login_handler(request, username, password):
    """
    Takes a set of user credentials and returns a sliding JSON web token to
    prove the authentication of those credentials.
    """
    ser = TokenObtainSlidingSerializer(data={
        get_username_field(): username,
        'password': password
    })
    try:
        ser.is_valid(raise_exception=True)
    except:
        raise InvalidUser('用户名/密码输入错误')
    update_last_login(None, ser.user)
    session_user_update(request, ser.user)
    res = dict(token=ser.validated_data.get('token'),
               user=UserSerializer(ser.user).data)
    return res
예제 #5
0
 def validate(self, data):
     username = data.get("username", None)
     password = data.get("password", None)
     user = authenticate(username=username, password=password)
     if user is None:
         raise serializers.ValidationError(
             'Wrong username or password, login failed')
     try:
         jwt_token = TokenObtainSlidingSerializer.get_token(user)
         update_last_login(None, user)
     except NetworkUser.DoesNotExist:
         raise serializers.ValidationError(
             'User with the given credentials does not exist')
     return {'username': user.username, 'token': jwt_token}
예제 #6
0
def jwt_encode(user):
    return TokenObtainSlidingSerializer.get_token(user)