Beispiel #1
0
    def post(self, request):
        json_str = request.body
        py_obj = json.loads(json_str)
        username = py_obj['username']
        password = py_obj['password']
        print(username, password)

        try:
            user = UserProfile.objects.get(username=username)
        except:
            result = {'code': 10200, 'error': '用户名或密码错误!'}
            return JsonResponse(result)

        md5 = hashlib.md5()
        md5.update(password.encode())
        password_h = md5.hexdigest()
        if password_h != user.password:
            result = {'code': 10201, 'error': '用户名或密码错误!'}
            return JsonResponse(result)
        token = make_token(username)

        return JsonResponse({
            'code': 200,
            'username': username,
            'data': {
                'token': token.decode()
            }
        })
 def post(self, request):
     # 1. 获取前段传递的json串
     json_str = request.body
     # 2. 将json串序列化为对象
     json_obj = json.loads(json_str)
     # 3. 获取用户名和密码
     username = json_obj['username']
     password = json_obj['password']
     # 4. 校验用户名和密码
     try:
         user = UserProfile.objects.get(username=username)
     except:
         result = {'code': 10200, 'error': '用户名或密码错误'}
         return JsonResponse(result)
     # 5. 计算密码的hash
     md5 = hashlib.md5()
     md5.update(password.encode())
     password_h = md5.hexdigest()
     # 与数据库中的密码的hash值对比
     if password_h != user.password:
         result = {'code': 10201, 'error': '用户名或密码错误'}
         return JsonResponse(result)
     # 6. 校验成功后签发token
     token = make_token(username)
     # token = token.decode()
     return JsonResponse({
         'code': 200,
         'username': username,
         'data': {
             'token': token
         }
     })
Beispiel #3
0
def tokens(request):
    """
    创建token->登录
    :param request:
    :return:
    """
    if not request.method == "POST":
        result = {"code": 201, "error": "Please use POST!"}
        return JsonResponse(result)
    json_str = request.body
    if not json_str:
        result = {"code": 202, "error": "Please give me json!"}
        return JsonResponse(result)
    json_obj = json.loads(json_str)
    username = json_obj.get("username")
    if not username:
        result = {"code": 203, "error": "Please give me username!"}
        return JsonResponse(result)
    password = json_obj.get("password")
    if not password:
        result = {"code": 205, "error": "Please give me password!"}
        return JsonResponse(result)
    users = Userprofile.objects.filter(username=username)
    if not users:
        result = {"code": 208, "error": "The username or password is wrong!"}
        return JsonResponse(result)

    p_m = hashlib.sha256()
    p_m.update(password.encode())
    hash_password = p_m.hexdigest()
    if users[0].password != hash_password:
        result = {"code": 209, "error": "The username or password is wrong!"}
        return JsonResponse(result)

    result = {
        "code": 200,
        "username": username,
        "data": {
            "token": make_token(username).decode()
        }
    }
    return JsonResponse(result)
Beispiel #4
0
    def post(self, request):
        json_str = request.body
        json_obj = json.loads(json_str)
        username = json_obj['username']
        password = json_obj['password']
        try:
            user = User.objects.get(username=username)
        except Exception as e:
            print('--log in error %s--' % e)
            result = {'code': 10200, 'error': '用户名或密码错误!'}
            return JsonResponse(result)

        md5 = hashlib.md5()
        md5.update(password.encode())
        if md5.hexdigest() != user.password:
            result = {'code': 10201, 'error': '用户名或密码错误!'}
            return JsonResponse(result)
        # 校验成功后,生成token
        token = make_token(username)

        result = {'code': 200, 'username': username, 'data': {'token': token}}
        print('csserv:', result)
        print("-NOT-GET-")
        return JsonResponse(result)
Beispiel #5
0
def tokens(request):
    # http://127.0.0.1:8000/v1/tokens
    if request.method == 'POST':
        # 请求示例: {‘username’: ‘xxx’, ‘password’: ‘yyy’}
        # 响应示例:{‘code’: 200,‘username’: ‘asc’, ‘data’: {‘token’: ‘zdsadasd’}}

        json_str = request.body
        json_obj = json.loads(json_str.decode())
        # print(json_obj)

        username = json_obj['username']
        if not username:
            html = {'code':201,'error':'NO username'}
            return JsonResponse(html)

        password = json_obj['password']
        if not password:
            html = {'code':202,'error':'NO password'}
            return JsonResponse(html)

        user = UserProfile.objects.filter(username=username)
        # print(user[0].username,user[0].email,user[0].password)
        if user.count() == 0:
            html = {'code':203,'error':'用户名或密码错误'}
            return JsonResponse(html)

        password = make_secret(password)
        if user[0].password != password or user[0].username != username:
            html = {'code':203,'error':'用户名或密码错误'}
            return JsonResponse(html)

        # print(username,password)
        # 制作响应token
        token = make_token(username)

        return JsonResponse({'code': 200, 'username': username, 'data': {'token': str(token)}})