Пример #1
0
def users(request):
  if (request.method == 'POST'):
    cipher    = AesService(AesService.secret_key)
    json_data = json.loads(request.body.decode(encoding='UTF-8'))
    username  = json_data.get('username', None)
    password  = cipher.encrypt(json_data.get('password', None))

    if (username is None or password is None):
      return HttpResponse('Unauthorized', status=401)
    else:
      if (not User.objects.filter(username=username, password=password).exists()):
        user  = User.objects.create(username=username, password=password)
        token = AuthService.getToken(user)

        return HttpResponse(json.dumps({'token': token}), content_type='application/json')
      else:
        return HttpResponse('Unauthorized', status=401)
Пример #2
0
def login(request):
  if (request.method == 'GET'):
    cipher   = AesService(AesService.secret_key)
    username = request.GET.get('username', None)
    password = request.GET.get('password', None)

    if (not username is None and not password is None):
      try:
        user  = User.objects.get(username=username)

        if (password == cipher.decrypt(user.password).decode(encoding='UTF-8')):
          token = AuthService.getToken(user)
          return HttpResponse(json.dumps({'token': token}), content_type='application/json')
        else:
          return HttpResponse('Unauthorized', status=401)
      except ObjectDoesNotExist:
        return HttpResponse('Unauthorized', status=401)
    else:
      return HttpResponse('Unauthorized', status=401)