Beispiel #1
0
 def post(self):
     json_object = request.get_json()
     user = UserModel.query.filter_by(
         username=json_object.get('username')).first()
     if user:
         response_obj = dict(status='Fail',
                             message=str(UserAlreadyExist()),
                             error=UserAlreadyExist.__name__)
         return make_response(jsonify(response_obj), 500)
     user = UserModel(username=json_object.get('username'),
                      password=json_object.get('password'))
     if json_object.get('admin'):
         user.admin = json_object.get('admin')
     if json_object.get('anonymous'):
         user.anonymous = json_object.get('anonymous')
     access_token, refresh_token, refresh_token_id = user.encode_auth_tokens(
         user.username)
     user.token_id = refresh_token_id
     db.session.add(user)
     db.session.commit()
     response_obj = dict(status='Success',
                         message='Successfully registered',
                         access_token=access_token.decode(),
                         refresh_token=refresh_token.decode())
     return make_response(jsonify(response_obj), 201)
Beispiel #2
0
 def test_encode_auth_tokens(self):
     user = UserModel(username='******', password='******')
     db.session.add(user)
     db.session.commit()
     access_token, refresh_token, refresh_token_id = user.encode_auth_tokens(
         user.username)
     self.assertTrue(isinstance(access_token, bytes))
     self.assertTrue(isinstance(refresh_token, bytes))
     self.assertTrue(isinstance(refresh_token_id, str))
Beispiel #3
0
    def test_decode_refresh_token(self):
        user = UserModel(username='******', password='******')
        db.session.add(user)
        db.session.commit()
        access_token, refresh_token, refresh_token_id = user.encode_auth_tokens(
            user.username)
        self.assertTrue(isinstance(access_token, bytes))
        self.assertTrue(isinstance(refresh_token, bytes))
        self.assertTrue(isinstance(refresh_token_id, str))

        response = UserModel.decode_refresh_token(refresh_token)
        self.assertTrue(isinstance(response, str))
	def test_validate_token(self):
		user = UserModel(username='******', password='******')
		db.session.add(user)
		db.session.commit()
		_, refresh_token, _ = user.encode_auth_tokens(user.username)
		with mock.patch('authApi.api.utils.tokenutils.request') as mocked_request:
			mocked_request.headers = dict(Authorization='Bearer ' + refresh_token.decode())
			@validate_token
			def a():
				return 'test_function'
			result = a()
			self.assertTrue(result == 'test_function')
Beispiel #5
0
    def test_decode_expired_token(self):
        user = UserModel(username='******', password='******')
        db.session.add(user)
        db.session.commit()
        _, refresh_token, refresh_token_id = user.encode_auth_tokens(
            user.username)
        self.assertTrue(isinstance(refresh_token, bytes))

        payload = jwt.decode(refresh_token, key)
        payload['exp'] = datetime.datetime.utcnow() - datetime.timedelta(
            minutes=32)
        token = jwt.encode(payload, key, algorithm='HS256')
        response = UserModel.decode_expired_token(token)
        resp = json.loads(response)
        self.assertTrue(resp.get('name') == payload.get('sub'))
        self.assertTrue(resp.get('token_id') == payload.get('jti'))