Example #1
0
def closeAuction(sock, clientCert, symmetricEncryption, sessionId):

    # Gets clientID to receive open auctions
    clientID, clientName = getUserInfo(clientCert)

    # Verify if the current client has auction open
    if clientID not in CLIENTS.keys() or len(CLIENTS[clientID]) == 0:
        sendEncryptedMessage(sock, {"openAuctionList": []},
                             symmetricEncryption, sessionId, 4)
        return

    # Gather info of all open auction of the client
    openAuctionList = []
    for auctionId in CLIENTS[clientID]:
        openAuctionList.append(AUCTIONS[auctionId].auctionInfo)

    # Sends open auctions
    sendEncryptedMessage(sock, {'openAuctionList': openAuctionList},
                         symmetricEncryption, sessionId, 4)

    # Gets auctionID to close
    mandatoryFields = []
    mandatoryFields.append(Field("auctionID", int))
    msgRecv = readMessage(sock, mandatoryFields, symmetricEncryption,
                          sessionId, 5)
    auctionID = msgRecv['auctionID']
    auction = AUCTIONS[auctionID]

    # Connect to the repository to close the auction
    sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock2.connect(constants.REPOSITORY_ADDR)
    symmetricEncryption2, sessionId2 = handshake.clientHandShake(
        sock2, privateKey, certificate, POSSIB_CIPHERS, POSSIB_MODES,
        POSSIB_PADDING)

    key = b64encode(auction.key).decode("ascii")

    # Sends the request to Repository
    sendEncryptedMessage(sock2, {'action': ActionTypes.CLOSE_AUCTION},
                         symmetricEncryption2, sessionId2, 3)
    sendEncryptedMessage(sock2, {
        "auctionID": auctionID,
        'key': key
    }, symmetricEncryption2, sessionId2, 4)

    # Receive the winner bid
    msgRecv = readMessage(sock2, [], symmetricEncryption2, sessionId2, 5)

    # Send the winner bid to the creator
    sendEncryptedMessage(sock, {
        'winnerBid': msgRecv["winnerBid"],
        "key": key
    }, symmetricEncryption, sessionId, 6)

    # Remove local info
    AUCTIONS.pop(auctionID)
    CLIENTS[clientID].remove(auctionID)
Example #2
0
    def run(self):
        symmetricEncryption, clientCert, clientIsServer, sessionId = handshake.serverHandShake(
            self.conn, privateKey, certificate)

        mandatoryFields = []
        mandatoryFields.append(Field("action", int))
        msg = readMessage(self.conn, mandatoryFields, symmetricEncryption,
                          sessionId, 3)

        # client's requests
        if msg["action"] == ActionTypes.CREATE_AUCTION:
            if clientIsServer:
                sendEncryptedMessage(self.conn,
                                     {"error": "Action not allowed"},
                                     symmetricEncryption, sessionId, 4)
                return
            createAuction(self.conn, clientCert, symmetricEncryption,
                          sessionId)
        elif msg["action"] == ActionTypes.CLOSE_AUCTION:
            if clientIsServer:
                sendEncryptedMessage(self.conn,
                                     {"error": "Action not allowed"},
                                     symmetricEncryption, sessionId, 4)
                return
            closeAuction(self.conn, clientCert, symmetricEncryption, sessionId)
        # repository's requests
        elif msg["action"] == ActionTypes.VALIDATE_BID:
            if not clientIsServer:
                sendEncryptedMessage(self.conn,
                                     {"error": "Action not allowed"},
                                     symmetricEncryption, sessionId, 4)
                return
            validateBid(self.conn, symmetricEncryption, sessionId)
        elif msg["action"] == ActionTypes.CLOSE_AUCTION_TIME:
            if not clientIsServer:
                sendEncryptedMessage(self.conn,
                                     {"error": "Action not allowed"},
                                     symmetricEncryption, sessionId, 4)
                return
            closeAuctionTime(self.conn, symmetricEncryption, sessionId)
        else:
            sendEncryptedMessage(self.conn, {"error": "Invalid action field"},
                                 symmetricEncryption, sessionId, 4)

        self.conn.close()