Exemplo n.º 1
0
 def post(cls):
     # The signin_schema is made using vanilla marshmallow and not
     # flask marshmallow.
     parsed_data = signin_schema.load(request.get_json())
     incoming_email = parsed_data['email']
     incoming_password = parsed_data['password']
     # After parsing the data fetch an active user with received email
     discovered_active_user = ActiveModel.find_entry_by_email(
         incoming_email)
     if discovered_active_user is None:
         # Return an error if the user is not present in active table
         return {'message': USER_NOT_REGISTERED}, 400
     else:
         # Encode the discovered and incoming password
         discovered_password = discovered_active_user.password.encode()
         incoming_password = incoming_password.encode()
         # Compare the discovered and incoming password
         if bcrypt.checkpw(incoming_password, discovered_password):
             access_token = create_access_token(
                 identity=discovered_active_user.uid, fresh=True)
             refresh_token = create_refresh_token(
                 identity=discovered_active_user.uid)
             # If equal send access token and refresh token generated
             # using the uid.
             return {
                 'message': SIGNED_IN_SUCCESSFULLY,
                 'access_token': access_token,
                 'refresh_token': refresh_token,
                 'name': discovered_active_user.name,
                 'uid': discovered_active_user.uid
             }, 202
         # If password is not equal the return an error string
         else:
             return {'message': INCORRECT_PASSWORD}, 400
Exemplo n.º 2
0
    def post(cls):
        inactive_user_object = inactive_schema.load(request.get_json(),
                                                    db.session)

        if ActiveModel.find_entry_by_email(
                inactive_user_object.email) is not None:
            return {'message': ACTIVE_USER_FOUND}, 400

        if InactiveModel.find_entry_by_email(
                inactive_user_object.email) is None:
            email_delivery_response = InactiveModel.send_email(
                inactive_user_object)
        else:
            return {'message': INACTIVE_USER_FOUND}, 400

        # Checking if email was successfully sent or not
        if email_delivery_response is not None:
            # Checking password length and hashing it
            if len(inactive_user_object.password) > 72:
                return {'message': INVALID_PASSWORD_LENGTH}, 400

            encoded_password = inactive_user_object.password.encode()
            hashed_password = bcrypt.hashpw(encoded_password, bcrypt.gensalt())
            inactive_user_object.password = hashed_password.decode()

            # Adding a code field after generating a fresh code
            inactive_user_object.code = email_delivery_response['code']

            # Adding the time field using the inbuilt python module
            inactive_user_object.time = time.asctime(time.gmtime(time.time()))

            # Creating inactive user and checking if the operation was
            # successful or not.
            if inactive_user_object.create_inactive_user(
            ) == ERROR_WRITING_INACTIVE_TABLE:
                return {
                    'message': ERROR_REGISTERING_USER,
                    'details': ERROR_WRITING_INACTIVE_TABLE
                }, 500
        else:
            return {'message': ERROR_SENDING_EMAIL}, 400

        # This return value is temporarily equal to verification code
        # and needs to be replaced later.
        return {'message': 'Operation successful.'}
Exemplo n.º 3
0
    def post(cls):
        # Extract the email
        reset_data = reset_schema.load(request.get_json())
        # Now check if an active user exists
        discovered_active_user = ActiveModel.find_entry_by_email(
            reset_data['email'])
        if discovered_active_user is None:
            return {'message': 'No such account exists.'}, 400
        else:
            code = create_access_token(discovered_active_user.uid, False,
                                       datetime.timedelta(minutes=5))

            message = {
                'personalizations': [{
                    'to': [{
                        'email': discovered_active_user.email
                    }],
                    'dynamic_template_data': {
                        'data': {
                            'link':
                            f'https://www.themilkyway.tk/update/{code}',
                            'name': discovered_active_user.name.split()[0]
                        }
                    }
                }],
                'from': {
                    'email': '*****@*****.**',
                    'name': 'Password Reset'
                },
                'template_id':
                'Here goes the template id!'
            }

            sg = SendGridAPIClient(
                os.getenv('EMAIL_API_KEY', 'Here goes the default API Key!'))
            response = sg.send(message)

            if response.status_code == 202:
                return {'message': OP_SUCCESSFUL}