Exemple #1
0
 def post(self):
     data = UserRegistration.parser.parse_args()
     if UserModel.find_by_username(data['username']):
         return {'message': 'User {} already exists'.format(data['username'])}
     new_user = UserModel(
         username=data['username'],
         password=UserModel.generate_hash(data['password'])
     )
     try:
         new_user.save_to_db()
         access_token = create_access_token(identity=data['username'])
         refresher_token = create_refresh_token(identity=data['username'])
         return {
             'message': 'User {} was created'.format(data['username']),
             'access_token': access_token,
             'refresher_token': refresher_token
         }
     except:
         return {'message': 'Something went wrong'}, 500
    def post(self):
        data = parser.parse_args()
        current_user = UserModel.find_by_username(data['username'])

        if not current_user:
            return {
                'message': 'User {} doesn\'t exist'.format(data['username'])
            }

        if UserModel.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])
            return {
                'message': 'Logged in as {}'.format(current_user.username),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        else:
            return {'message': 'Wrong credentials'}
Exemple #3
0
    def post(self):

        data = parser.parse_args()

        username = data['username']

        # Checking that user is already exist or not
        if UserModel.find_by_username(username):

            return {'message': f'User {username} already exists'}

        # create new user
        new_user = UserModel(

            username=username,

            password=UserModel.generate_hash(data['password'])

        )

        try:
            
            # Saving user in DB and Generating Access and Refresh token
            new_user.save_to_db()
            
            access_token = create_access_token(identity=username)
            
            refresh_token = create_refresh_token(identity=username)

            return {
            
                'message': f'User {username} was created',
            
                'access_token': access_token,
            
                'refresh_token': refresh_token
            
            }
        
        except:
        
            return {'message': 'Something went wrong'}, 500
    def post(self):
        data = parser.parse_args()
        current_user = UserModel.find_by_username(data['username'])

        if not current_user:
            return {'message': f'User {data["username"]} doesn\'t exist'}

        if UserModel.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])
            resp = jsonify({
                'message': f'Logged in as {current_user.username}',
                'access_token': access_token,
                'refresh_token': refresh_token
            })
            set_access_cookies(resp, access_token)
            set_refresh_cookies(resp, refresh_token)
            return resp
        else:
            return {'message': 'Wrong credentials'}
Exemple #5
0
 def get(self):
     try:
         token  = request.args.get('token',default=0)
         if(token==0):
             return {'error': 'Enter token'}
         email = s.loads(token, salt='email-confirm', max_age=3600)
         a = UserModel.find_by_username(email)
         if(a.isverified is True):
             return {'success': 'Already verified'}
         UserModel.update_isverified(email)
         # a = UserModel.find_by_username(email)
         # print(a.isverified)
         return {'success': email}
     except SignatureExpired:
         return {'error': 'Expired token'}
     except BadTimeSignature:
         return {'error': 'Wrong token'}
     except Exception as e:
         print(e)
         return {'error': 'Unknown'}
Exemple #6
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('username', help = 'This field cannot be blank', required = True)
        parser.add_argument('password', help = 'This field cannot be blank', required = True)
        data = parser.parse_args()
        current_user = UserModel.find_by_username(data['username'])

        if not current_user:
            return {'message': 'Wrong credentials','error': True}, 500

        if UserModel.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity = data['username'])
            #refresh_token = create_refresh_token(identity = data['username'])
            return {
                'name': current_user.username,
                'access_token': access_token,
                #'refresh_token': refresh_token
                }
        else:
            return {'message': 'Wrong credentials', 'error': True}, 500
Exemple #7
0
    def post(self):

        data = user_login_parser.parse_args()
        current_user = UserModel.find_by_username(data['name'])
        if not current_user:
            return {'message': 'User {} doesn\'t exist'.format(data['name'])}, 404

        # if UserModel.verify_hash(data['password'], current_user.password):
        if data['password'] == current_user.password:
            access_token = create_access_token(identity=current_user.id)
            refresh_token = create_refresh_token(identity=current_user.id)
            resp = jsonify(
                {'message': 'Logged in as {}'.format(current_user.name), 'id': current_user.id})

            set_access_cookies(resp, access_token)
            set_refresh_cookies(resp, refresh_token)
            return resp

        else:
            return {'message': 'Wrong credentials'}, 500
Exemple #8
0
        def post(self, **kwargs):
            data = kwargs

            current_user = UserModel.find_by_username(data['username'], True)

            if current_user and UserModel.verify_hash(data['password'],
                                                      current_user.password):
                access_token = create_access_token(identity=data['username'])
                role = current_user.role
                current_user.close_connection()
                return {
                    'exito': True,
                    'message': 'Bienvenido {}'.format(data['username']),
                    'access_token': access_token,
                    'role': role,
                }
            else:
                if current_user:
                    current_user.close_connection()
                return {'exito': False, 'message': 'Credenciales incorrectas'}
Exemple #9
0
 def post(self):
     data = parser.parse_args()
     if UserModel.find_by_username(data['username']):
         return {
             'message': 'User {} already exist'.format(data['username'])
         }
     new_user = UserModel(username=data['username'],
                          password=UserModel.generate_hash(
                              data['password']))
     try:
         new_user.save_to_db()
         access_token = create_access_token(identity=data['username'])
         refresh_token = create_refresh_token(identity=data['username'])
         return {
             'message': 'User {} was created'.format(data['username']),
             'access_token': access_token,
             'refresh_token': refresh_token
         }
     except:
         return {'message': 'ada sesuatu yang salah'}, 500
    def get(self, user):
        #get identity & find the identity's info
        current_user = get_jwt_identity()
        result = UserModel.find_by_username(user)

        #pull our associated promoter's name from our info. Note that this will throw an error if the user has no associated promoter
        try:
            promoter_name = result.promoter_name
            promoter = PromoterModel.find_by_name(promoter_name)
        except:
            return {'message': 'You are not associated with a promoter.'}
        #if we're the right user, serialize the found promoter and return the results
        if (user == current_user):
            try:
                return promoter_schema.dump(promoter)
            except:
                return {'message': 'Something went wrong.'}, 500
        else:
            return {
                'message': 'You are not authorized to access this information.'
            }
    def post(self):

        data = parser.parse_args()

        username = data['username']
        current_user = UserModel.find_by_username(username)

        if not current_user:
            return {'message': f'User {username} doesn\'t exist'}
        if UserModel.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity=username)

            refresh_token = create_refresh_token(identity=username)
            return {
                'message': f'Logged in as {username}',
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        else:

            return {'message': "Wrong credentials"}
Exemple #12
0
    def put(self, user_id):
        current_user = UserModel.query.get(user_id)
        args = user_put_parser.parse_args()
        if args['username']:
            if UserModel.find_by_username(args['username']):
                return {'message': 'User {} already exists'. format(args['username'])}, 409
            current_user.username = args['username']

        if args['password']:
            current_user.password = UserModel.generate_hash(args['password'])

        if args['email']:
            current_user.email = args['email']

        if args['phone']:
            current_user.phone = args['phone']
        try:
            current_user.update_db()
            return {'message': 'User Details Updated'}, 200
        except:
            return {'message': 'Something went wrong'}, 500
    def post(self):
        data = parser.parse_args()
        current_user = UserModel.find_by_username(data['username'])
        if not current_user:
            return {
                'message': 'User {} doesn\'t exist'.format(data['username'])
            }, 401

        if UserModel.verify_hash(data['password'], current_user.password):
            expires = datetime.timedelta(hours=3)
            access_token = create_access_token(identity=data['username'],
                                               expires_delta=expires)
            refresh_token = create_refresh_token(identity=data['username'])
            return {
                'message': '{}'.format(current_user.username),
                'access_token': access_token,
                'refresh_token': refresh_token
            }, 200
        else:
            logger.error(f"There was an error: 401")
            return {'message': ''}, 401
Exemple #14
0
 def post(self):
     if request.is_json:
         try:
             username = request.get_json()['username']
             password = request.get_json()['password']
         except KeyError:
             return {"Message": "Some field is empty"}, 400
     else:
         return {"Message": "Request body is null/not JSON"}, 400
     current_user = UserModel.find_by_username(username)
     if not current_user:
         return {"Message": "User {} doesnt exist".format(username)}, 400
     if UserModel.verify_hash(password, current_user.password):
         access_token = create_access_token(identity=username)
         refresh_token = create_refresh_token(identity=username)
         return {
             'message': 'User {} Logged in Successfully'.format(username),
             'access_token': access_token,
             'refresh_token': refresh_token
         }, 200
     else:
         return {'message': 'Wrong credentials'}, 403
Exemple #15
0
    def post(self):

        data = request.get_json(force=True)

        username = data['username']

        current_user = UserModel.find_by_username(username)

        if not current_user:
            return {'message': 'User {} doesn\'t exist'.format(username)}

        user_role = UserModel.get_user_role(username)

        if UserModel.verify_hash(data['password'], current_user.password):

            genr_access_token = create_access_token(identity=username)
            genr_refresh_token = create_refresh_token(identity=username)

            UserModel.update_table(username, genr_access_token,
                                   genr_refresh_token)

            if user_role == 'passenger':
                userId = PassengerModel.get_passengerId(username)
                PassengerModel.update_token(userId, genr_access_token)
            if user_role == 'driver':
                userId = DriverModel.get_driverIdbyNIC(username)
                DriverModel.update_token(userId, genr_access_token)
            if user_role == 'owner':
                userId = OwnerModel.get_ownerId(username)
                OwnerModel.update_token(userId, genr_access_token)

            return {
                'success': 1,
                'access_token': genr_access_token,
                'refresh_token': genr_refresh_token,
                'user_id': userId
            }
        else:
            return {'message': 'wrong password'}
    def post(self):
        args = parser.parse_args()

        username = args['username']
        password = UserModel.generate_hash(args['password'])

        if UserModel.find_by_username(username):
            return {'message': 'User {} already exists'.format(username)}

        new_user = UserModel(username=username, password=password)

        try:
            new_user.save_to_db()
            access_token = create_access_token(identity=username)
            refresh_token = create_refresh_token(identity=username)
            return {
                'message': 'User {} was created'.format(username),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        except:
            return {'message': 'Something went wrong'}, 500
    def post(self):
        data = request.get_json()
        user = user_schema.load(data)
        current_user = UserModel.find_by_username(data['username'])
        if not current_user:
            return {
                'message': 'User {} doesn\'t exist.'.format(data['username'])
            }

        if UserModel.verify_hash(data['password'], current_user.password):
            try:
                access_token = create_access_token(identity=data['username'])
                refresh_token = create_refresh_token(identity=data['username'])
                return {
                    'message': 'Logged in as {}'.format(current_user.username),
                    'access_token': access_token,
                    'refresh_token': refresh_token
                }
            except:
                return {'message': 'Something went wrong.'}, 500
        else:
            return {'message': 'Wrong password.'}
Exemple #18
0
    def post(self, received_item):
        current_user = get_jwt_identity()
        logged_user = UserModel.find_by_username(current_user)

        try:
            new_order = OrderModel(
                user_id=logged_user.id,
                customized_message=received_item.get("message"),
                create_date=datetime.datetime.utcnow())

            for item in received_item.get("order"):
                found_menu = MenuModel.get_by_id(item.get("menu_id"))
                a = AssociationModel(counts=item.get("counts"))
                a.menu = found_menu
                new_order.menus.append(a)

            new_order.save_to_db()
            result = {"message": "successful"}
        except (Exception, ):
            logger.exception("create order failed")
            result = {"message": "failed"}
        return result
 def post(self):
     data = parser.parse_args()
     new_user = UserModel(username=data['username'],
                          password=UserModel.generate_hash(
                              data['password']))
     try:
         if UserModel.find_by_username(data['username']):
             return {
                 'message':
                 'User {} already exists'.format(data['username'])
             }, 401
         new_user.save_to_db()
         access_token = create_access_token(identity=data['username'])
         refresh_token = create_refresh_token(identity=data['username'])
         return {
             'message': 'User {} was created'.format(data['username']),
             'access_token': access_token,
             'refresh_token': refresh_token
         }, 200
     except Exception as e:
         logger.error(f"There was an error: {e}")
         return {'message': 'Something went wrong'}, 500
Exemple #20
0
    def post(self):
        print(request.headers)
        data = parser.parse_args()
        current_user = UserModel.find_by_username(data['username'])
        if not current_user:
            return {
                'message': 'User {} doesn\'t exist'.format(data['username'])
            }, 500

        if UserModel.verify_hash(data['password'], current_user.password):
            expires = timedelta(seconds=3600)
            access_token = create_access_token(identity=data['username'],
                                               expires_delta=expires)
            refresh_token = create_refresh_token(identity=data['username'])
            return {
                'message': 'Logged in as {}'.format(current_user.username),
                'access_token': access_token,
                'refresh_token': refresh_token,
                'user_id': data['username']
            }
        else:
            return {'message': 'Wrong credentials'}, 500
Exemple #21
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('username',
                            help='This field cannot be blank',
                            required=True)
        parser.add_argument('password',
                            help='This field cannot be blank',
                            required=True)
        parser.add_argument('mobile')
        parser.add_argument('email')
        parser.add_argument('name')

        data = parser.parse_args()

        if UserModel.find_by_username(data['username']):
            return {
                'message': 'User {} already exists'.format(data['username'])
            }, 201

        new_user = UserModel(username=data['username'],
                             mobile=data['mobile'],
                             email=data['email'],
                             name=data['name'],
                             password=UserModel.generate_hash(
                                 data['password']))

        new_user.save()
        claim = app.UserClaim(username=new_user.username,
                              userid=new_user.hex_id,
                              roles=new_user.roles)

        access_token = create_access_token(identity=claim)
        refresh_token = create_refresh_token(identity=claim)
        return {
            'message': 'User {} was created'.format(data['username']),
            'access_token': access_token,
            'refresh_token': refresh_token
        }
Exemple #22
0
def userLogin():
    data = request.get_json()
    current_user = UserModel.find_by_username(data['username'])
    if not current_user:
        return {'message': 'User {} doesn\'t exist'.format(data['username'])}

    if UserModel.verify_hash(data['password'], current_user.password):
        access_token = create_access_token(identity=data['username'])
        refresh_token = create_refresh_token(identity=data['username'])

        if not os.path.exists(app.config['UPLOAD_FOLDER'] + data['username']):
            os.makedirs(app.config['UPLOAD_FOLDER'] + data['username'])
        else:
            delete_files(app.config['UPLOAD_FOLDER'] + data['username'] + '/')

        if not os.path.exists(app.config['PARSE_FOLDER'] + data['username'] +
                              '/image'):
            os.makedirs(app.config['PARSE_FOLDER'] + data['username'] +
                        '/image')
        else:
            delete_files(app.config['UPLOAD_FOLDER'] + data['username'] +
                         '/image/')

        if not os.path.exists(app.config['PARSE_FOLDER'] + data['username'] +
                              '/json'):
            os.makedirs(app.config['PARSE_FOLDER'] + data['username'] +
                        '/json')
        else:
            delete_files(app.config['UPLOAD_FOLDER'] + data['username'] +
                         '/json/')

        return {
            'message': 'Logged in as {}'.format(current_user.username),
            'access_token': access_token,
            'refresh_token': refresh_token
        }
    else:
        return {'message': 'Wrong credentials'}, 404
Exemple #23
0
    def post(self):
        data = parser.parse_args()
        username = data['username']

        if UserModel.find_by_username(username):
            return {'message': f'User {username} already exists'}

        new_user = UserModel(username=username,
                             password=UserModel.generate_hash(
                                 data['password']))

        try:
            new_user.save_to_db()
            access_token = create_access_token(identity=username)
            refresh_token = create_refresh_token(identity=username)

            return {
                'message': f'User {username} was created',
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        except:
            return {'message': 'Something went wrong'}, 500
Exemple #24
0
    def post(self):
        data = parser.parse_args()

        if UserModel.find_by_username(data['username']):
            return {'message': 'User already exists', 'error': True}, 500
        elif len(data['password']) <= 7:
            return {'message': 'Password has to be at least 8 chars long', 'error': True}, 500
        new_user = UserModel(
            username = data['username'],
            password = UserModel.generate_hash(data['password'])
        )

        try:
            new_user.save_to_db()
            access_token = create_access_token(identity = data['username'])
            #refresh_token = create_refresh_token(identity = data['username'])
            return {
                'name': data['username'],
                'access_token': access_token,
                #'refresh_token': refresh_token
                }
        except:
            return {'message': 'Something went wrong', 'error': True}, 500
Exemple #25
0
    def post(self):
        data = user_parser.parse_args()
        if UserModel.find_by_username(data['name']):
            return {'message': 'User {} already exists'. format(data['name'])}, 409
        # new_user = UserModel(
        #     data['username'], UserModel.generate_hash(data['password']), data["email"], data['phone'])
        if data['invite_id']:
            invite = InviteModel.query.get(data['invite_id'])

            new_user = UserModel(
                data['name'], data['password'], invite.mail, data['phone'])
            try:
                team = TeamModel.query.get(invite.team_id)
                new_user.teams.append(team)
                new_user.save_to_db()
                resp = jsonify(
                    {'message': 'User {} was created'.format(data['name'])})
                resp.status_code = 200
                return resp
            except:
                return {'message': 'Something went wrong'}, 500
        else:
            return {'message': 'invite id Not Found'}, 500
    def post(self):
        args = parser.parse_args()

        username = args['username']
        password = args['password']

        current_user = UserModel.find_by_username(username)

        if not current_user:
            return {'message': 'User {} doesn\'t exist'.format(username)}

        if UserModel.verify_password(password, current_user.password):
            access_token = create_access_token(identity=username)
            refresh_token = create_refresh_token(identity=username)

            return {
                'message': 'Logged in as {}'.format(username),
                'access_token': access_token,
                'refresh_token': refresh_token
            }

        else:
            return {'message': 'Wrong credentials'}
Exemple #27
0
    def post(self):
        data = parser.parse_args()

        if UserModel.find_by_username(
                data['username']) or UserModel.find_by_email(data['email']):
            return {
                'message': "User with such username or email already exists"
            }, 400

        new_user = UserModel(username=data['username'],
                             email=data['email'],
                             password=UserModel.generate_hash(
                                 data['password']))

        # try:
        new_user.save_to_db()
        access_token = create_access_token(identity=new_user)
        refresh_token = create_refresh_token(identity=new_user)
        return {
            'message': 'User {} was created'.format(data['username']),
            'access_token': access_token,
            'refresh_token': refresh_token
        }
Exemple #28
0
    def post(self, args):
        current_user = get_jwt_identity()
        user = UserModel.find_by_username(current_user)

        if user:
            try:
                if args:
                    user.birthday = args.get('birthday')
                    user.phone = args.get('phone')
                    user.gender = args.get('gender')
                    user.save_to_db()
                    return {
                        'message':
                        'user {}: profile has been updated.'.format(
                            user.username)
                    }
                else:
                    return {'message': 'wrong parameters'}, 400
            except (Exception, ):
                return {'message': 'Something went wrong'}, 500

        else:
            return {'message': 'user not found'}, 404
Exemple #29
0
    def post(self):
        data = parser.parse_args()
        print(data)
        if data['imagefile']:  #바로 보낼 수 없으니까..
            data['imagefile'] = 'well received'
        
        if UserModel.find_by_username(data['username']):
            return {'message': 'User {} already exists'.format(data['username'])}

        new_user = UserModel(
            username=data['username'],
            password=UserModel.generate_hash(data['password'])
        )
        try:
            new_user.save_to_db()
            ## 여기서 만들자
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])
            return {'message': 'User {} was created'.format(data['username']),
                    'access_token': access_token,
                    'refresh_token': refresh_token}
        except:
            return {'message': 'something went wrong'}, 500
Exemple #30
0
    def post(self, args):
        if UserModel.find_by_username(args.get('username')):
            return {
                'message':
                'User {} already exists.'.format(args.get('username'))
            }, 400

        new_user = UserModel(
            username=args.get('username'),
            password=UserModel.generate_hash(args.get('password')),
            email=args.get('email'),
        )
        try:
            new_user.save_to_db()
            access_token = create_access_token(identity=args.get('username'))
            refresh_token = create_refresh_token(identity=args.get('username'))
            return {
                'message': 'User {} was created'.format(args.get('username')),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        except (Exception, ):
            return {'message': 'Something went wrong'}, 500