def clear_manager_login(server=None, password=None): """Force clear the "manager" authorisation, e.g., after a crash. Args: server (str): in the form "example.com" or "example.com:41984". password (str): if not specified, prompt on the command line. """ if server and ":" in server: s, p = server.split(":") msgr = Messenger(s, port=p) else: msgr = Messenger(server) msgr.start() if not password: password = getpass('Please enter the "manager" password: ') msgr.clearAuthorisation("manager", password) print("Manager login cleared.") msgr.stop()
) parser.add_argument("-w", "--password", type=str) parser.add_argument("-u", "--user", type=str) parser.add_argument( "-s", "--server", metavar="SERVER[:PORT]", action="store", help="Which server to contact.", ) global messenger args = parser.parse_args() if args.server and ":" in args.server: s, p = args.server.split(":") messenger = Messenger(s, port=p) else: messenger = Messenger(args.server) messenger.start() # If user not specified then default to scanner if args.user is None: user = "******" else: user = args.user # get the password if not specified if args.password is None: pwd = getpass.getpass("Please enter the '{}' password:".format(user)) else: pwd = args.password
def getInfo(self): self.partial_parse_address() server = self.ui.serverLE.text() self.ui.serverLE.setText(server) if not server: log.warning("No server URI") return mport = self.ui.mportSB.value() # save those settings # self.saveDetails() # TODO? # TODO: might be nice, but needs another thread? # self.ui.infoLabel.setText("connecting...") # self.ui.infoLabel.repaint() try: messenger = Messenger(server, mport) r = messenger.start() except PlomBenignException as e: ErrorMessage("Could not connect to server.\n\n" "{}".format(e)).exec_() return self.ui.infoLabel.setText(r) try: spec = messenger.get_spec() except PlomSeriousException: try: spec = messenger.getInfoGeneral() except PlomSeriousException: ErrorMessage("Could not connect to server.").exec_() return self.ui.markGBox.setTitle("Marking information for “{}”".format( spec["name"])) question = self.getQuestion() v = self.getv() self.ui.pgSB.setVisible(False) self.ui.vSB.setVisible(False) self.ui.vDrop.clear() self.ui.vDrop.addItems( [str(x + 1) for x in range(0, spec["numberOfVersions"])]) if v: if v >= 1 and v <= spec["numberOfVersions"]: self.ui.vDrop.setCurrentIndex(v - 1) self.ui.vDrop.setVisible(True) self.ui.pgDrop.clear() self.ui.pgDrop.addItems([ get_question_label(spec, n + 1) for n in range(spec["numberOfQuestions"]) ]) if question: if question >= 1 and question <= spec["numberOfQuestions"]: self.ui.pgDrop.setCurrentIndex(question - 1) self.ui.pgDrop.setVisible(True) # TODO should we also let people type in? self.ui.pgDrop.setEditable(False) self.ui.vDrop.setEditable(False) # put focus at username or password line-edit if len(self.ui.userLE.text()) > 0: self.ui.passwordLE.setFocus(True) else: self.ui.userLE.setFocus(True)
def validate(self): # Check username is a reasonable string user = self.ui.userLE.text().strip() self.ui.userLE.setText(user) if (not user.isalnum()) or (not user): return # check password at least 4 char long # Don't strip whitespace from passwords pwd = self.ui.passwordLE.text() if len(pwd) < 4: log.warning("Password too short") return self.partial_parse_address() server = self.ui.serverLE.text() self.ui.serverLE.setText(server) if not server: log.warning("No server URI") return mport = self.ui.mportSB.value() # save those settings self.saveDetails() try: # TODO: re-use existing messenger? messenger = Messenger(server, mport) messenger.start() except PlomBenignException as e: ErrorMessage("Could not connect to server.\n\n" "{}".format(e)).exec_() return try: messenger.requestAndSaveToken(user, pwd) except PlomAPIException as e: ErrorMessage("Could not authenticate due to API mismatch." "Your client version is {}.\n\n" "Error was: {}".format(__version__, e)).exec_() return except PlomAuthenticationException as e: # not PlomAuthenticationException(blah) has args [PlomAuthenticationException, "you are not authenticated, blah] - we only want the blah. ErrorMessage("Could not authenticate: {}".format( e.args[-1])).exec_() return except PlomExistingLoginException as e: if (SimpleMessage( "You appear to be already logged in!\n\n" " * Perhaps a previous session crashed?\n" " * Do you have another client running,\n" " e.g., on another computer?\n\n" "Should I force-logout the existing authorisation?" " (and then you can try to log in again)\n\n" "The other client will likely crash.").exec_() == QMessageBox.Yes): messenger.clearAuthorisation(user, pwd) return except PlomSeriousException as e: ErrorMessage("Could not get authentication token.\n\n" "Unexpected error: {}".format(e)).exec_() return # Now run the appropriate client sub-application if self.runIt == "Marker": # Run the marker client. question = self.getQuestion() v = self.getv() self.setEnabled(False) self.hide() markerwin = marker.MarkerClient(self.parent) markerwin.my_shutdown_signal.connect(self.on_marker_window_close) markerwin.show() markerwin.setup(messenger, question, v, lastTime) self.parent.marker = markerwin elif self.runIt == "IDer": # Run the ID client. self.setEnabled(False) self.hide() idwin = identifier.IDClient() idwin.my_shutdown_signal.connect(self.on_other_window_close) idwin.show() idwin.getToWork(messenger) self.parent.identifier = idwin
def test_invalid_url_too_many_colons(): m = Messenger("example.com:1234:1234") raises(PlomBenignException, lambda: m.start())