コード例 #1
0
    def login_user(data):
        try:
            # fetch the user data
            print('login user data:', data);
            user = User.query.filter_by(email=data.get('email')).first()
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.id)
                print('auth token:', auth_token);
                if auth_token:
                    response_object = {
                        'status': 'success',
                        'message': 'Successfully logged in.',
                        'Authorization': auth_token
                    }
                    return response_object, 200
            else:
                response_object = {
                    'status': 'fail',
                    'message': 'email or password does not match.'
                }
                return response_object, 401

        except Exception as e:
            print(e)
            response_object = {
                'status': 'fail',
                'message': 'Try again'
            }
            return response_object, 500
コード例 #2
0
 def renew_token(device_id, refresh_token):
     if device_id and refresh_token:
         LOG.info(device_id)
         device, status = get_device(device_id, refresh_token)
         LOG.info(device)
         # LOG.info(device)
         if device:
             user = User.query.filter_by(id=device.user_id).first()
             if user:
                 auth_token = User.encode_auth_token(
                     user.id, user.role, user.account_type)
                 response_object = {
                     # 'status': 'success',
                     # 'message': 'Successfully renew token',
                     'token': auth_token.decode()
                 }
                 return build_json_result(response_object, 200,
                                          'Successfully renew token')
             else:
                 return build_json_result(None, 500, 'Can not renew')
         else:
             return build_json_result(None, 500, 'Can not renew')
     else:
         return build_json_result(None, 403,
                                  'Provide a valid refresh token.')
コード例 #3
0
 def signup_user(data, is_admin):
     user = User.query.filter_by(email=data['email']).first()
     if not user:
         new_user = User(public_id=str(uuid.uuid4()),
                         email=data['email'],
                         username=data['username'],
                         password=data['password'],
                         phone=data['phone'],
                         admin=True if is_admin is True else False,
                         registered_on=datetime.datetime.utcnow())
         save_changes(new_user)
         try:
             # generate the auth token
             auth_token = User.encode_auth_token(new_user.id)
             response_object = {
                 'status': 'success',
                 'message': 'Successfully registered.',
                 'Authorization': auth_token.decode()
             }
             return response_object, 201
         except Exception as e:
             response_object = {
                 'status': 'fail',
                 'message': 'Some error occurred. Please try again.'
             }
             return response_object, 401
     else:
         response_object = {
             'status': 'fail',
             'message': 'User already exists. Please Log in.',
         }
         return response_object, 409
コード例 #4
0
    def login_user(data):
        try:
            # fetch the user data
            user = User.query.filter_by(email=data.get('email')).first()
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.id)
                if auth_token:
                    response_object = {
                        'status': 'success',
                        'message': 'Successfully logged in.',
                        'user': {
                            'id': user.public_id,
                            'email': user.email,
                            'admin_flag': user.admin,
                        },
                        'Authorization': auth_token.decode()
                    }
                    return response_object, 200
            else:
                response_object = {
                    'status': 'fail',
                    'message': 'Correo electronico y/o contraseña incorrectos'
                }
                return response_object, 401

        except Exception as e:
            print(e)
            response_object = {
                'status': 'fail',
                'message': 'Try again'
            }
            return response_object, 500
コード例 #5
0
    def login_user(data):
        try:
            user = User.query.filter_by(email=data.get('email')).first()
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.id)
                if auth_token:
                    response_object = {
                        'status': 'success',
                        'message': 'Successfully logged in.',
                        'authorization': auth_token.decode(),
                        'userId': user.public_id
                    }

                    return response_object, 200
            else:
                response_object = {
                    'status': 'fail',
                    'message': 'email or password does not match.'
                }
                return response_object, 401

        except Exception as e:
            print(e)
            response_object = {'status': 'fail', 'message': 'Try again'}
            return response_object, 500
コード例 #6
0
    def login_user(data):
        try:
            # fetch the user data
            user = User.query.filter_by(username=data.get('username')).first()
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.id)
                if auth_token:
                    response_object = {
                        'status': 'success',
                        'message': 'Successfully logged in.',
                        'Authorization': auth_token.decode()
                    }
                    return response_object, 200
            else:
                response_object = {
                    'status': 'fail',
                    'message': 'username or password does not match.'
                }
                return response_object, 401

        except Exception as e:
            response_object = {
                'status': 'fail',
                'message': 'Try again'
            }
            return response_object, 500
コード例 #7
0
def generate_token(user):
    response_object = {
        'status': 'success',
        'message': 'Successfully registered.',
        'Authorization': User.encode_auth_token(user.id).decode()
    }
    return response_object, 201
コード例 #8
0
ファイル: test_user_model.py プロジェクト: codex-tk/lab
 def test_encode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 registered_on=datetime.datetime.utcnow())
     db.session.add(user)
     db.session.commit()
     ret, auth_token = user.encode_auth_token(user.id, 'test_key')
     self.assertTrue(ret)
コード例 #9
0
 def test_encode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 registered_on=datetime.datetime.utcnow())
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
コード例 #10
0
 def test_decode_auth_token(self):
     user = User(password='******', registered_on=datetime.datetime.utcnow())
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
     self.assertTrue(
         User.decode_auth_token(auth_token.decode("utf-8")) == 1)
コード例 #11
0
def get_conversation_list(self):
    seed_conversation()
    admin_user_id = 1  # seeded in base.py
    admin = User.query.filter_by(id=admin_user_id).first()
    token = User.encode_auth_token(admin)
    return self.client.get(
        "/conversations/", headers={"Authorization": token.decode("utf-8")}
    )
コード例 #12
0
    def test_encode_auth_token(self):
        user = User(email='*****@*****.**',
                    password='******',
                    registration_date=datetime.utcnow())

        db.session.add(user)
        db.session.commit()
        auth_token = user.encode_auth_token(user.public_id)
        self.assertTrue(isinstance(auth_token, bytes))
コード例 #13
0
ファイル: user_service.py プロジェクト: Jieszs/restx-demo
def generate_token(user: User) -> Tuple[Dict[str, str], int]:
    try:
        # generate the auth token
        auth_token = User.encode_auth_token(user.id)
        return success_data('Successfully registered.',
                            {'Authorization': auth_token.decode()}), 201
    except Exception as e:
        current_app.logger.error(e)
        return internal_err_resp()
コード例 #14
0
 def save_user_to_db_and_get_token():
     user = User(email='*****@*****.**',
                 password='******',
                 created_at=datetime.datetime.utcnow(),
                 updated_at=datetime.datetime.utcnow())
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     return auth_token
コード例 #15
0
 def test_encode_auth_token(self):
     user = User(userId=Counter.getNextSequence('userId'),
                 userName='******',
                 email='*****@*****.**',
                 dateRegistered=datetime.datetime.utcnow())
     user.userPassword = '******'
     user.save()
     auth_token = user.encode_auth_token(user.userId)
     self.assertTrue(isinstance(auth_token, bytes))
コード例 #16
0
 def test_encode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 reg_datetime=datetime.datetime.utcnow(),
                 user_role='inspector',
                 level_cd='11')
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
コード例 #17
0
 def test_decode_auth_token(self):
     user = User(
         email="*****@*****.**",
         registered_on=datetime.datetime.utcnow(),
         password="******"
     )
     db.session.add(user)
     db.session.commit()
     auth_token = User.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
     self.assertTrue(User.decode_auth_token(auth_token) == 1)
コード例 #18
0
    def test_decode_auth_token(self):
        user = User(email='*****@*****.**',
                    password='******',
                    joined_at=datetime.datetime.utcnow())
        db.session.add(user)
        db.session.commit()
        auth_token = User.encode_auth_token(user.id)

        self.assertTrue(isinstance(auth_token, bytes))
        self.assertTrue(
            User.decode_auth_token(auth_token.decode("utf-8")) == 1)
コード例 #19
0
 def test_decode_auth_token(self):
     user_id = uuid.uuid4()
     user = User(email='*****@*****.**',
                 id=user_id,
                 username='******',
                 password='******',
                 registered_on=datetime.datetime.utcnow())
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertEqual(User.decode_auth_token(auth_token), user.id)
コード例 #20
0
 def test_decode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 reg_datetime=datetime.datetime.utcnow(),
                 user_role='worker',
                 level_cd='01')
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
     self.assertTrue(
         User.decode_auth_token(auth_token.decode("utf-8")) == 1)
コード例 #21
0
 def login_user(api, data):
     try:
         user = User.query.filter_by(email=data.get('email')).first()
         if user and user.check_password(data.get('password')):
             auth_token = User.encode_auth_token(user.id)
             if auth_token:
                 return {'token': auth_token.decode()}
         else:
             return 'Email or Password does not match'
     except Exception as e:
         LOG.exception(e)
         api.abort(500, INTERNAL_SERVER_ERROR)
コード例 #22
0
 def test_decode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 registered_on=datetime.datetime.utcnow(),
                 public_id=str(uuid.uuid4()),
                 username='******')
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
     self.assertTrue(
         User.decode_auth_token(auth_token.decode('utf-8')) == 1)
コード例 #23
0
def generate_token(user):
    try:
        # generate the auth token
        auth_token = User.encode_auth_token(user._uuid)
        return dict(
            data={
                'Authorization': auth_token.decode()
            },
        ), HTTPStatus.CREATED
    except Exception as e:
        return dict(
            error='Encountered an unexpected error',
        ), HTTPStatus.UNAUTHORIZED
コード例 #24
0
 def test_decode_auth_token(self):
     user = User(
         email="*****@*****.**",
         password="******",
         registered_on=datetime.datetime.utcnow(),
     )
     db.session.add(user)
     db.session.commit()
     auth_token = User.encode_auth_token(user)
     self.assertTrue(isinstance(auth_token, bytes))
     sub, scope = User.decode_auth_token(auth_token.decode("utf-8"))
     self.assertTrue(sub == user.id)
     self.assertTrue(scope == False)
コード例 #25
0
 def test_encode_auth_token(self):
     user = User(
         first_name='Test',
         last_name='User',
         language='en',
         personal_phone='555-555-5555',
         email='*****@*****.**',
         password='******',
         registered_on=datetime.datetime.utcnow()
     )
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
コード例 #26
0
def generate_token(user):
    try:
        auth_token = User.encode_auth_token(user.id)
        return {
                   'status': 'success',
                   'message': 'Successfully registered.',
                   'Authorization': auth_token.decode()
               }, 201
    except Exception as e:
        response_object = {
            'status': 'fail',
            'message': 'Some error occurred, Try again',
        }
        return response_object, 401
コード例 #27
0
def generate_token(user):
    try:
        # generate the auth token
        auth_token = User.encode_auth_token(user.id, user.role)
        response_object = {
            'user_id': user.id,
            # 'status': 'success',
            # 'message': 'Successfully registered.',
            'token': auth_token.decode()
        }
        return build_json_result(response_object, 201,
                                 'Successfully registered.')
    except Exception as e:
        return build_json_result(None, 401,
                                 'Some error occurred. Please try again.')
コード例 #28
0
ファイル: auth_helper.py プロジェクト: Jieszs/restx-demo
    def login_user(data: Dict[str, str]) -> Tuple[Dict[str, str], int]:
        try:
            # fetch the user data
            user = User.query.filter_by(email=data.get('email')).first()
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.id)
                if auth_token:
                    return success_data('Successfully logged in.',
                                        {'Authorization': auth_token.decode()})
            else:
                return message(False, 'email or password does not match.'), 401

        except Exception as e:
            current_app.logger.error(e)
            return internal_err_resp()
コード例 #29
0
def generate_token(user):
    try:
        auth_token = User.encode_auth_token(str(user["id"]))
        response_obj = {
            "status": "success",
            "message": "Successfully registered.",
            "Authorization": auth_token.decode()
        }
        return response_obj, 201
    except Exception as e:
        response_obj = {
            "status": "fail",
            "message": "Some error occured. Please try again"
        }
        return response_obj, 401
コード例 #30
0
def generate_token(user):
    try:
        print(user)
        auth_token = User.encode_auth_token(user.id)
        return {
            'status': 'success',
            'msg': 'Successfully registered',
            'Authorization': auth_token.decode()
        }, 201
    except Exception as e:
        print(e)
        return {
            'status': 'fail',
            'msg': 'Some error occurred. Please try again'
        }, 401