Example #1
0
def show_r(request):
    response = (RecordList.as_view()(request))
    # print(response.render().readable())
    print(obtain_jwt_token(request))
    # Response().render().set_cookie('token', )
    # return response
    return obtain_jwt_token(request)
Example #2
0
    def setUp(self):
        factory = APIRequestFactory()
        #set up super_token
        self.super_user = Account.objects.create_superuser(email="*****@*****.**", username="******", password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.super_token = response.data['token']

        #set up normal user and token
        self.normal_user = Account.objects.create_superuser(email="*****@*****.**", username="******", password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.normal_token = response.data['token']
Example #3
0
    def setUp(self):
        factory = APIRequestFactory()

        # set up normal user and token
        self.normal_user = Account.objects.create_user(email="*****@*****.**", username="******", password="******")
        request = factory.post("/api/v1/auth/login/", {"email": "*****@*****.**", "password": "******"})
        response = obtain_jwt_token(request)
        self.token = response.data["token"]

        # set up admin user
        self.cause_man = Account.objects.create_superuser(
            email="*****@*****.**", username="******", password="******"
        )
        request = factory.post("/api/v1/auth/login/", {"email": "*****@*****.**", "password": "******"})
        response = obtain_jwt_token(request)
        self.super_token = response.data["token"]
Example #4
0
def get_token(url, user=None):
    if not user:
        user = create_user()
    factory = APIRequestFactory()
    request = factory.post(url, user, format='json')
    response = obtain_jwt_token(request)
    return response.data['token']
Example #5
0
def login(*args, **kwargs):
    response = obtain_jwt_token(*args, **kwargs)
    if status.is_success(response.status_code):
        # according to rest_framework_jwt.utils.jwt_response_payload_handler
        # response.data = {'token': xxxx, ...}
        response = jwt_response_special_handling(response)
    return response
Example #6
0
def signup(request):
    email = request.data.get('email')
    if User.objects.filter(email=email).exists():
        return Response({'error' : '이미 가입된 이메일입니다.'}, status=status.HTTP_400_BAD_REQUEST)
    password = request.data.get('password')
    password_confirmation = request.data.get('password_confirmation')
    if password != password_confirmation:
        return Response({'error' : '비밀번호가 일치하지 않습니다.'}, status=status.HTTP_400_BAD_REQUEST)

    if len(password) < 8:
        return Response({'error' : '비밀번호는 8자리 이상 입력해주세요'}, status=status.HTTP_400_BAD_REQUEST)
    cnt = 0
    digit, upper, lower = False, False, False
    for p in password:
        if cnt < 3:
            if p.isdigit() and not digit:
                cnt += 1
                digit = True
            elif p.isupper() and not upper:
                cnt += 1
                upper = True
            elif p.islower() and not lower:
                cnt += 1
                lower = True
        else:
            break
    if cnt < 3:
        return Response({'error' : '비밀번호는 영어 대/소문자, 숫자로 구성되어있어야합니다.'}, status=status.HTTP_400_BAD_REQUEST)

    pin_code = request.data.get('pin_code')
    pin_code_confirmation = request.data.get('pin_code_confirmation')

    if pin_code != pin_code_confirmation:
        return Response({'error' : 'pincode 가 일치하지 않습니다.'}, status=status.HTTP_400_BAD_REQUEST)

    if len(pin_code) < 6 or not pin_code.isdigit():
        return Response({'error': 'pin_code는 6자리 숫자만 허용됩니다.'}, status=status.HTTP_400_BAD_REQUEST)

    pin_code = make_password(pin_code)
    context = {
        'username' : request.data.get('username'),
        'email' : request.data.get('email'),
        'password' : request.data.get('password'),
        'pin_code' : pin_code
    }

    serializer = UserSerializer(data=context)
    if serializer.is_valid(raise_exception=True):
        user = serializer.save()
        user.set_password(context['password'])
        user.save()
        token = obtain_jwt_token(request._request).data['token']
        response_context = {
            'username' : request.data.get('username'),
            'token' : token
        }
        return Response(response_context, status=status.HTTP_201_CREATED)
    else:
        return Response({'error' : '유효하지 않습니다.'}, status=status.HTTP_409_CONFLICT)
Example #7
0
    def setUp(self):
        factory = APIRequestFactory()

        #set up normal user and token
        self.normal_user = Account.objects.create_user(email="*****@*****.**", username="******", password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.token = response.data['token']

        #set up admin user
        self.cause_man = Account.objects.create_superuser(email="*****@*****.**", username="******",
                                                          password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.super_token = response.data['token']

        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='JWT ' + self.super_token)

        #create a company
        data = {'company_name': 'Test Company', 'about':"about", 'description':'desc',
                'founded': datetime.date(1900, 1, 22), 'country': 'Spain'}
        response = client.post('/api/v1/companies/', data)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['founded'], "1900-01-22")
        self.assertEqual(response.data['country'], "Spain")
        self.assertEqual(response.data['description'], "desc")
        self.company_pk = response.data['id']

        self.company = Company.objects.get(company_name="Test Company")
        self.test_slug = response.data['slug']
        #another company
        data = {'company_name': 'Mokia'}
        response = client.post('/api/v1/companies/', data)
        self.assertEqual(response.status_code, 201)
        self.mokia = Company.objects.get(company_name="Mokia")
        self.mokia_slug = response.data['slug']
        #third company
        data = {'company_name': 'Moke'}
        response = client.post('/api/v1/companies/', data)
        self.assertEqual(response.status_code, 201)
        self.moke = Company.objects.get(company_name="Moke")
        self.moke_slug = response.data['slug']

        #create a Cause
        self.cause = Cause.objects.create(creator=self.cause_man, name="Test cause", description="desc")
Example #8
0
    def test_login(self):
        #success case
        factory = APIRequestFactory()
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.assertTrue('token' in response.data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        #check that token is more or less in correct form
        #the token is set up in three parts, separeted by comma: 123.456.789
        token = response.data['token']
        self.assertEqual(3, len(token.split('.')))

        #fail case
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.assertTrue('non_field_errors' in response.data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
 def test_obtain_token(self):
     self.factory = RequestFactory()
     request = self.factory.post('/api-token-auth/', {
         'username': '******',
         'password': '******'
     })
     request.user = self.author
     response = obtain_jwt_token(request)
     self.assertEqual(response.status_code, 200)
Example #10
0
    def setUp(self):
        factory = APIRequestFactory()

        #set up normal user and token
        self.userOne = Account.objects.create_user(email="*****@*****.**", username="******", password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.tokenOne = response.data['token']

        #set up normal user and token
        self.userTwo = Account.objects.create_user(email="*****@*****.**", username="******", password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.tokenTwo = response.data['token']

        #set up couple of companies
        self.companyOne = Company.objects.create(account_owner=self.userOne, company_name="One")
        self.companyTwo = Company.objects.create(account_owner=self.userTwo, company_name="Two")
Example #11
0
 def get_token(self):
     'POST -d "username=jibin&password=jibin@123" http://localhost:8000/api-token-auth/'
     data = {
         "username": self.user.username,
         "password": "******",
     }
     request = self.factory.post('/api-token-auth/', data)
     request.user = self.user
     response = obtain_jwt_token(request)
     self.assertEqual(response.status_code, 200)
     self.token = response.data.get('token')
Example #12
0
    def setUp(self):
        factory = APIRequestFactory()
        #set up normal user and token
        self.normal_user = Account.objects.create_user(email="*****@*****.**", username="******", password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.normal_token = response.data['token']

        self.user2 = Account.objects.create_user(email="*****@*****.**", username="******", password="******")
        request = factory.post('/api/v1/auth/login/', {'email': '*****@*****.**', 'password': '******'})
        response = obtain_jwt_token(request)
        self.token2 = response.data['token']

        self.user3 = Account.objects.create_user(email="*****@*****.**", username="******", password="******")
        self.user4 = Account.objects.create_user(email="*****@*****.**", username="******", password="******")

        #create couple of companies
        self.companyOne = Company.objects.create(account_owner=self.normal_user, company_name="xyz")
        self.companyTwo = Company.objects.create(account_owner=self.user2, company_name="abc")

        #a product for companyOne
        self.productOne = Product.objects.create(owner=self.companyOne, name='chicken',
                                                 description="tastes good", price=3.4)
Example #13
0
def obtain_token(request, *args, **kwargs):
    data = json.loads(request.body)
    username = data.get('username')
    password = data.get('password')
    resp = obtain_jwt_token(request, *args, **kwargs)
    token = resp.data
    if token:
        user = authenticate(request, username=username, password=password)
        if user:
            user_object = User.objects.filter(username=username).first()
            # Check if the user already has a token
            # pylint:disable=no-member
            tkn = Token.objects.filter(
                user=user_object, valid=True)
            if tkn:
                return JsonResponse(data={'token': tkn['value']})
            expiration = timezone.now() + datetime.timedelta(hours=24)
            token_object = Token(user=user_object, value=token.get('token'), expiry=expiration,
                                 username=username, valid=True)
            token_object.save()
            return JsonResponse(data={
                'token': token,
            })
    return Response('Failed to generate token', status=status.HTTP_400_BAD_REQUEST)
    def test_if_student_was_create(self):
        # Registering a new user
        payload = {
            "name": "test",
            "user": {
                "username": "******",
                "password": "******",
                "email": "*****@*****.**"
            }
        }
        request = self.factory.post('/api/user', payload, format='json')
        response = views.StudentCreation.as_view()(request)
        self.assertEqual(response.status_code, 201)

        # Checking whether the actual user object was created or not
        user = User.objects.get(username='******')
        self.assertEqual(user.username, 'test')
        self.assertEqual(user.email, '*****@*****.**')

        # Checking if password is stored as a hash or plain
        user = User.objects.get(username='******')
        self.assertNotEqual(user.password, "password")

        # Trying a login protected route for student
        payload = {
            "username": "******",
            "password": "******"
        }
        request = self.factory.post('/api-token-auth/', payload, format='json')
        response = obtain_jwt_token(request)
        self.assertEqual(response.status_code, 200)
        response.render()
        token = json.loads(response.content)['token']
        self.client.credentials(HTTP_AUTHORIZATION='JWT {}'.format(token))
        response = self.client.get('/api/get_certificates/')
        self.assertEqual(response.status_code, 200)
Example #15
0
def my_obtain_jwt_token(request):
    response = obtain_jwt_token(request)
    return response
Example #16
0
def login(request):
    request = request._request
    response = obtain_jwt_token(request)
    if status.is_success(response.status_code):
        response = jwt_response_special_handling(response)
    return response
Example #17
0
def obtain_jwt_token(request):
    #if not code_is_correct(request): return HTTPResponse(code = 403)
    return JWT.obtain_jwt_token(request)
Example #18
0
def login(*args, **kwargs):
    response = obtain_jwt_token(*args, **kwargs)
    return response
Example #19
0
def get_auth(request):
    return obtain_jwt_token(request)
Example #20
0
def login(request):
	return obtain_jwt_token(request)