def addcard() -> Dict[str, Any]: # Grab card, convert it card = request.get_json()['card'] try: cardid = CardCipher.decode(card['number']) except CardCipherException: raise Exception('Invalid card number!') # Make sure it is our card userid = g.data.local.user.from_username(card['owner']) if userid is None: raise Exception('Cannot find user to add card to!') # See if it is already claimed curuserid = g.data.local.user.from_cardid(cardid) if curuserid is not None: raise Exception('This card is already in use!') # Add it to the user's account g.data.local.user.add_card(userid, cardid) # Return new card list return { 'cards': [format_card(card) for card in g.data.local.user.get_all_cards()], }
def searchusers() -> Dict[str, Any]: # Grab card, convert it searchdetails = request.get_json()['user_search'] if len(searchdetails['card']) > 0: try: cardid = CardCipher.decode(searchdetails['card']) actual_userid = g.data.local.user.from_cardid(cardid) if actual_userid is None: # Force a non-match below actual_userid = UserID(-1) except CardCipherException: actual_userid = UserID(-1) else: actual_userid = None def match(user: User) -> bool: if actual_userid is not None: return user.id == actual_userid else: return True return { 'users': [ format_user(user) for user in g.data.local.user.get_all_users() if match(user) ], }
def removeusercard(userid: int) -> Dict[str, Any]: # Cast the userID. userid = UserID(userid) # Grab card, convert it card = request.get_json()['card'] try: cardid = CardCipher.decode(card) except CardCipherException: raise Exception('Invalid card number!') user = g.data.local.user.get_user(userid) # Make sure the user ID is valid if user is None: raise Exception('Cannot find user to update!') # Remove it from the user's account g.data.local.user.destroy_card(userid, cardid) # Return new card list return { 'cards': [ CardCipher.encode(card) for card in g.data.local.user.get_cards(userid) ], }
def addusercard(userid: int) -> Dict[str, Any]: # Cast the userID. userid = UserID(userid) # Grab card, convert it card = request.get_json()['card'] try: cardid = CardCipher.decode(card) except CardCipherException: raise Exception('Invalid card number!') user = g.data.local.user.get_user(userid) # Make sure the user ID is valid if user is None: raise Exception('Cannot find user to update!') # See if it is already claimed curuserid = g.data.local.user.from_cardid(cardid) if curuserid is not None: raise Exception('This card is already in use!') # Add it to the user's account g.data.local.user.add_card(userid, cardid) # Return new card list return { 'cards': [ CardCipher.encode(card) for card in g.data.local.user.get_cards(userid) ], }
def test_external_cipher(self) -> None: test_cards = [ ("S6E523E30ZK7ML1P", "E004010027A5FC68"), ("78B592HZSM9E6712", "E004010027A6102C"), ] for card in test_cards: back = card[0] db = card[1] decoded = CardCipher.decode(back) self.assertEqual(decoded, db, f"Card DB {decoded} doesn't match expected {db}") encoded = CardCipher.encode(db) self.assertEqual(encoded, back, f"Card back {encoded} doesn't match expected {back}")
def addbalance(arcadeid: int) -> Dict[str, Any]: credits = request.get_json()['credits'] card = request.get_json()['card'] # Make sure the arcade is valid arcade = g.data.local.machine.get_arcade(arcadeid) if arcade is None: raise Exception('Unable to find arcade to update!') if g.userID not in arcade.owners: raise Exception('You don\'t own this arcade, refusing to update!') try: cardid = CardCipher.decode(card) userid = g.data.local.user.from_cardid(cardid) except CardCipherException: userid = None if userid is None: raise Exception('Unable to find user by this card!') # Update balance balance = g.data.local.user.update_balance(userid, arcadeid, credits) if balance is not None: g.data.local.network.put_event( 'paseli_transaction', { 'delta': credits, 'balance': balance, 'reason': 'arcade operator adjustment', }, userid=userid, arcadeid=arcadeid, ) return { 'balances': { balance[0]: balance[1] for balance in g.data.local.machine.get_balances(arcadeid) }, 'users': {user.id: user.username for user in g.data.local.user.get_all_users()}, 'events': [ format_event(event) for event in g.data.local.network.get_events( arcadeid=arcadeid, event='paseli_transaction') ], }
def removecard() -> Dict[str, Any]: # Grab card, convert it card = request.get_json()['card'] try: cardid = CardCipher.decode(card) except CardCipherException: raise Exception('Invalid card number!') # Make sure it is our card userid = g.data.local.user.from_cardid(cardid) # Remove it from the user's account g.data.local.user.destroy_card(userid, cardid) # Return new card list return { 'cards': [format_card(card) for card in g.data.local.user.get_all_cards()], }
def main() -> None: parser = argparse.ArgumentParser( description= "A utility to convert between card IDs and back-of-card characters.") parser.add_argument( "number", help="card ID or back-of-card characters to convert.", type=str, ) args = parser.parse_args() try: print(CardCipher.decode(args.number)) except CardCipherException: try: back = CardCipher.encode(args.number) print(" ".join([back[i:(i + 4)] for i in range(0, len(back), 4)])) except CardCipherException: print('Bad card ID or back-of-card characters!')
def removecard() -> Dict[str, Any]: # Grab card, convert it card = request.get_json()['card'] try: cardid = CardCipher.decode(card) except CardCipherException: raise Exception('Invalid card number!') # Make sure it is our card userid = g.data.local.user.from_cardid(cardid) if userid != g.userID: raise Exception('This card is not yours to delete!') # Remove it from this user's account g.data.local.user.destroy_card(g.userID, cardid) # Return new card list cards = [CardCipher.encode(card) for card in g.data.local.user.get_cards(g.userID)] return { 'cards': cards, }
def addcard() -> Dict[str, Any]: # Grab card, convert it card = request.get_json()['card'] try: cardid = CardCipher.decode(card) except CardCipherException: raise Exception('Invalid card number!') # See if it is already claimed userid = g.data.local.user.from_cardid(cardid) if userid is not None: raise Exception('This card is already in use!') # Add it to this user's account g.data.local.user.add_card(g.userID, cardid) # Return new card list cards = [CardCipher.encode(card) for card in g.data.local.user.get_cards(g.userID)] return { 'cards': cards, }
def register() -> Response: card_number = request.form['card_number'] pin = request.form['pin'] username = request.form['username'] email = request.form['email'] password1 = request.form['password1'] password2 = request.form['password2'] # First, try to convert the card to a valid E004 ID try: cardid = CardCipher.decode(card_number) except CardCipherException: error('Invalid card number!') return register_display(card_number, username, email) # Now, see if this card ID exists already userid = g.data.local.user.from_cardid(cardid) if userid is None: error('This card has not been used on the network yet!') return register_display(card_number, username, email) # Now, make sure this user doesn't already have an account user = g.data.local.user.get_user(userid) if user.username is not None or user.email is not None: error('This card is already in use!') return register_display(card_number, username, email) # Now, see if the pin is correct if not g.data.local.user.validate_pin(userid, pin): error('The entered PIN does not match the PIN on the card!') return register_display(card_number, username, email) # Now, see if the username is valid if not valid_username(username): error('Invalid username!') return register_display(card_number, username, email) # Now, check whether the username is already in use if g.data.local.user.from_username(username) is not None: error('The chosen username is already in use!') return register_display(card_number, username, email) # Now, see if the email address is valid if not valid_email(email): error('Invalid email address!') return register_display(card_number, username, email) # Now, make sure that the passwords match if password1 != password2: error('Passwords do not match each other!') return register_display(card_number, username, email) # Now, make sure passwords are long enough if len(password1) < 6: error('Password is not long enough!') return register_display(card_number, username, email) # Now, create the account. user.username = username user.email = email g.data.local.user.put_user(user) g.data.local.user.update_password(userid, password1) # Now, log them into that created account! aes = AESCipher(g.config['secret_key']) sessionID = g.data.local.user.create_session(userid) success('Successfully registered account!') response = make_response(redirect(url_for('home_pages.viewhome'))) response.set_cookie('SessionID', aes.encrypt(sessionID)) return response