def post(self):
        clue = self.request.get('clue')
        mobileNumber = "+44" + self.request.get('mobilenumber')

        query = PasswordStore.query(
            ndb.AND(
                PasswordStore.clue == clue,
                PasswordStore.mobileNumber == mobileNumber,
                PasswordStore.confirmed == 1)).order(-PasswordStore.created)
        result = query.fetch(1)

        if result:
            encrypt = Encrypt()

            #logging.info('found: ' + clue + " - " + mobileNumber)
            sms = SendSMS()
            sms.sendPassword(
                mobileNumber,
                encrypt.decryptString(result[0].encryptedPassword,
                                      mobileNumber))
        #else:
        #logging.info('not found: ' + clue + " - " + mobileNumber)

        template_values = {'nav': 'retrieve'}

        template = JINJA_ENVIRONMENT.get_template('templates/success.html')
        self.response.write(template.render(template_values))
    def post(self):
        # Step 1 - retrieve and verify user input
        clue = self.request.get('clue').strip()
        mobileNumber = "+44" + self.request.get('mobilenumber').strip()

        # Step 2 - get the password record

        query = PasswordStore.query(
            ndb.AND(PasswordStore.clue == clue, PasswordStore.mobileNumber ==
                    mobileNumber)).order(-PasswordStore.created)
        passwordStorerecord = query.fetch(1)

        if passwordStorerecord:
            #logging.info('found: ' + clue + " - " + mobileNumber)

            passwordStorerecord = passwordStorerecord[0]
            passwordStoreId = passwordStorerecord.key.id(
            )  # the id of the record just created

            # Step 3 - store verification record
            verificationRecord = Verification()
            verificationRecord.action = 'delete'
            verificationRecord.confirmed = 0
            verificationRecord.passwordStoreId = passwordStoreId
            verificationRecord.put()

            verificationRecordId = verificationRecord.key.id(
            )  # the id of the record just created
            logging.info('storing verification id: ' +
                         str(verificationRecordId))

            # Step 4 - send SMS with encrypted verification

            encrypt = Encrypt()

            i = str(verificationRecordId)
            e = encrypt.encryptString(i)
            d = encrypt.decryptString(e)

            sms = SendSMS()
            sms.verifyDelete(mobileNumber, e)

            logging.info('sending delete verification: ' + " - " + i + " - " +
                         e + " - " + d)
        #else:
        #logging.info('not found: ' + clue + " - " + mobileNumber)

        # Step 5 - render reply
        template_values = {'nav': 'delete', 'id': e}

        template = JINJA_ENVIRONMENT.get_template(
            'templates/check_phone_success.html')
        self.response.write(template.render(template_values))
    def post(self):
        # Step 1 - retrieve and verify user input
        clue = self.request.get('clue').strip()
        mobileNumber = "+44" + self.request.get('mobilenumber').strip()
        password = self.request.get('pass').strip()

        # Step 2 - store the password
        encrypt = Encrypt()
        e = encrypt.encryptString(password, mobileNumber)

        passwordStorerecord = PasswordStore()
        passwordStorerecord.clue = clue
        passwordStorerecord.mobileNumber = mobileNumber
        passwordStorerecord.encryptedPassword = e
        passwordStorerecord.confirmed = 0
        passwordStorerecord.put()

        passwordStoreId = passwordStorerecord.key.id(
        )  # the id of the record just created
        logging.info('storing password id: ' + str(passwordStoreId))

        # Step 3 - store verification record
        verificationRecord = Verification()
        verificationRecord.action = 'add'
        verificationRecord.confirmed = 0
        verificationRecord.passwordStoreId = passwordStoreId
        verificationRecord.put()

        verificationRecordId = verificationRecord.key.id(
        )  # the id of the record just created
        logging.info('storing verification id: ' + str(verificationRecordId))

        # Step 4 - send SMS with encrypted verification
        i = str(verificationRecordId)
        e = encrypt.encryptString(i)
        d = encrypt.decryptString(e)

        sms = SendSMS()
        sms.verifyPasswordAdd(mobileNumber, e)

        logging.info('sending verification: ' + " - " + i + " - " + e + " - " +
                     d)

        # Step 5 - render reply

        template_values = {'nav': 'store', 'id': e}

        template = JINJA_ENVIRONMENT.get_template(
            'templates/check_phone_success.html')
        self.response.write(template.render(template_values))
    def get(self):

        template = 'error.html'
        template_values = {'nav': 'none'}

        if (self.request.get('id')):
            encrypt = Encrypt()
            verificationId = int(encrypt.decryptString(self.request.get('id')))

            verificationRecord = Verification.get_by_id(verificationId)
            logging.info(verificationRecord)

            if verificationRecord:
                passwordStorerecord = PasswordStore.get_by_id(
                    verificationRecord.passwordStoreId)

                if passwordStorerecord:
                    # Handle the add verification step
                    if verificationRecord.action == 'add':
                        passwordStorerecord.confirmed = 1
                        passwordStorerecord.put()
                        verificationRecord.key.delete()
                        logging.info(
                            'Updated records - verification (deleted) and passwordStore (updated)'
                        )
                        template = 'success.html'

                    # Handle the delete verification step
                    if verificationRecord.action == 'delete':
                        passwordStorerecord.key.delete()
                        verificationRecord.key.delete()
                        logging.info(
                            'Updated records - verification (deleted) and passwordStore (deleted)'
                        )
                        template = 'success.html'
                    else:
                        logging.info(
                            'Failed to retrieve Verification record id: ' +
                            str(verificationId))
                else:
                    logging.info(
                        'Failed to retrieve PasswordStore record id: ' +
                        str(verificationRecord.passwordStoreId))
            else:
                template = 'error.html'

        template = JINJA_ENVIRONMENT.get_template('templates/' + template)
        self.response.write(template.render(template_values))