def process(self, response=None, error=None): if error: logging.debug("Error during authentication: %r", error) self._error(AuthenticationError(error)) return if self._state == "start": self._state = "nonce" logging.debug("Sending nonce") msg = message.query(0, "%s.$cmd" % self.pool._dbname, 0, 1, SON({'getnonce': 1}), SON({})) self.connection._send_message(msg, self.process) elif self._state == "nonce": # this is the nonce response self._state = "finish" try: nonce = response['data'][0]['nonce'] logging.debug("Nonce received: %r", nonce) key = helpers._auth_key(nonce, self.dbuser, self.dbpass) except Exception, e: self._error(AuthenticationError(e)) return msg = message.query( 0, "%s.$cmd" % self.pool._dbname, 0, 1, SON([('authenticate', 1), ('user', self.dbuser), ('nonce', nonce), ('key', key)]), SON({})) self.connection._send_message(msg, self.process)
def unlock(self, username, password, create_new): user_data_dir = os.path.join(self.data_directory, username) if create_new: if os.path.exists(user_data_dir): raise AuthenticationError('User {} already exists'.format(username)) else: os.mkdir(user_data_dir) else: if not os.path.exists(user_data_dir): raise AuthenticationError('User {} does not exist'.format(username)) if not os.path.exists(os.path.join(user_data_dir, 'rotkehlchen.db')): # This is bad. User directory exists but database is missing. # Make a backup of the directory that user should probably remove # on his own. At the same time delete the directory so that a new # user account can be created shutil.move( user_data_dir, os.path.join(self.data_directory, 'backup_%s' % username) ) raise AuthenticationError( 'User {} exists but DB is missing. Somehow must have been manually ' 'deleted or is corrupt. Please recreate the user account.'.format(username)) self.db = DBHandler(user_data_dir, username, password) self.user_data_dir = user_data_dir return user_data_dir
def authenticate(self, verbose=False): """Authenticates with the Spotify API by prompting the user for information that we need. :verbose: Enables verbose mode. :returns: The authentication token, if it was successful. """ # Get the username self.username = self._getUsername() # Authenticate with the web API if it's not already provided. token = util.prompt_for_user_token(self.username, self.AUTH_SCOPE, client_id=self.CLIENT_ID, client_secret=self.CLIENT_SECRET, redirect_uri=self.REDIRECT_URI) if token: if verbose: print("Authenticated successfully.") self.token = token self.sp = spotipy.Spotify(auth=token) self.authenticated = True else: raise AuthenticationError("Authentication unsuccessful")
def __init__(self, config=None): """Creates new Sklik API client instance. :param config: Sklik API client configuration instance """ self.__session = None if not config: raise SklikApiError("No config given") self.__proxy = _create_server_proxy(config.namespace, verbose=config.debug, allow_none=True) res = self.__proxy.client.login(config.username, config.password) if res["status"] == 400: raise ArgumentError(res["statusMessage"], res["errors"]) elif res["status"] == 401: raise AuthenticationError(res["statusMessage"]) elif res["status"] != 200: raise SklikApiError(res["statusMessage"]) self.__session = res["session"]
def __init__(self, config=None): """Creates new Sklik API client instance Keyword arguments: config: sklik API client configuration instance """ self.__session = None if not config: raise SklikApiError("No config given") #endif self.__config = config self.__proxy = ServerProxy(self.__config.namespace, allow_none=True) res = self.__proxy.client.login(self.__config.username, self.__config.password) if res["status"] == 400: raise ArgumentError(res["statusMessage"], res["errors"]) elif res["status"] == 401: raise AuthenticationError(res["statusMessage"]) elif res["status"] != 200: raise SklikApiError(res["statusMessage"]) #endif self.__session = res["session"]
def cat_creation(): cat_name = request.values["name"] if not session['authenticated'] is None and session['authenticated']: cat = make_cat(session['username'], cat_name) return jsonify({ 'success': True, 'cat': cat.to_dict(), 'username': session['username'] }) else: raise AuthenticationError()
def _finish_authentication(self, response, error=None): if error: self.__deferred_message = None self.__deferred_callback = None raise AuthenticationError(error) assert response['number_returned'] == 1 response = response['data'][0] if response['ok'] != 1: logging.error('Failed authentication %s' % response['errmsg']) self.__deferred_message = None self.__deferred_callback = None raise AuthenticationError(response['errmsg']) message = self.__deferred_message callback = self.__deferred_callback self.__deferred_message = None self.__deferred_callback = None self.__callback = callback # continue the original request self._send_message(message)
def __del__(self): """Logs out.""" if self.__session == None: return res = self.__proxy.client.logout(self.__session) if res["status"] == 400: raise ArgumentError(res["statusMessage"], res["errors"]) elif res["status"] == 401: raise AuthenticationError(res["statusMessage"]) elif res["status"] != 200: raise SklikApiError(res["statusMessage"])
def _start_authentication(self, response, error=None): # this is the nonce response if error: logging.error(error) logging.error(response) raise AuthenticationError(error) nonce = response['data'][0]['nonce'] key = helpers._auth_key(nonce, self.__dbuser, self.__dbpass) self.__callback = self._finish_authentication self._send_message( message.query( 0, "%s.$cmd" % self.__pool._dbname, 0, 1, SON([('authenticate', 1), ('user', self.__dbuser), ('nonce', nonce), ('key', key)]), SON({})))
def authenticate(self): """ Initiates authentication with the remote service and returns a two-tuple containing the storage system URL and session token. """ conn = self.conn_class(self.host, self.port, timeout=self.timeout) conn.request('GET', '/' + self.uri, headers=self.headers) response = conn.getresponse() response.read() # A status code of 401 indicates that the supplied credentials # were not accepted by the authentication service. if response.status == 401: raise AuthenticationFailed() # Raise an error for any response that is not 2XX if response.status // 100 != 2: raise ResponseError(response.status, response.reason) for hdr in response.getheaders(): if hdr[0].lower() == "x-auth-token": auth_token = hdr[1] if hdr[0].lower() == "x-server-management-url": (pnetloc, pport, puri, pis_ssl) = parse_url(hdr[1]) puri = "/" + puri _dns_management_host = dns_management_host if 'lon.' in pnetloc: _dns_management_host = 'lon.' + _dns_management_host dns_management_url = [] if pis_ssl: dns_management_url.append("https://") else: dns_management_url.append("http://") for x in (_dns_management_host, puri): dns_management_url.append(x) conn.close() if not (auth_token, dns_management_host): raise AuthenticationError("Invalid response from the " \ "authentication service.") return ("".join(dns_management_url), auth_token)
def authenticate(self): """ Initiates authentication with the remote service and returns a two-tuple containing the storage system URL and session token. """ # 产生连接的实例 # 传递的三个参数,是httplib.HTTPConnection需要的 conn = self.conn_class(self.host, self.port, timeout=self.timeout) conn.request('GET', '/' + self.uri, headers=self.headers) # 获得服务器端的响应 response = conn.getresponse() response.read() # A status code of 401 indicates that the supplied credentials # were not accepted by the authentication service. if response.status == 401: raise AuthenticationFailed() # Raise an error for any response that is not 2XX # 为什么使用 // ? #if response.status // 100 != 2: if response.status / 100 != 2: raise ResponseError(response.status, response.reason) storage_url = auth_token = None # 解析http headers for hdr in response.getheaders(): if hdr[0].lower() == "x-storage-url": storage_url = hdr[1] # swift 不支持CDN,我们把它搞去 # 保留此处是为了记住包含CDN信息的HTTP头的处理方法 #if hdr[0].lower() == "x-cdn-management-url": # cdn_url = hdr[1] if hdr[0].lower() == "x-storage-token": auth_token = hdr[1] if hdr[0].lower() == "x-auth-token": auth_token = hdr[1] # 连接关闭 conn.close() if not (auth_token and storage_url): raise AuthenticationError("Invalid response from the " \ "authentication service.") return (storage_url, auth_token)
def ret_cat(): if not session['authenticated'] == None and session['authenticated']: cat = get_cat(session['username']).to_dict() if not cat: return jsonify({ 'success': False, 'cat_exists': False, 'username': session['username'] }) else: return jsonify({ 'success': True, 'cat_exists': True, 'username': session['username'], 'cat': cat }) else: raise AuthenticationError()
class AuthorizeJob(AsyncJob): def __init__(self, connection, dbuser, dbpass, pool, err_callback): super(AuthorizeJob, self).__init__(connection, "start", err_callback) self.dbuser = dbuser self.dbpass = dbpass self.pool = pool def process(self, response=None, error=None): if error: logging.debug("Error during authentication: %r", error) self._error(AuthenticationError(error)) return if self._state == "start": self._state = "nonce" logging.debug("Sending nonce") msg = message.query(0, "%s.$cmd" % self.pool._dbname, 0, 1, SON({'getnonce': 1}), SON({})) self.connection._send_message(msg, self.process) elif self._state == "nonce": # this is the nonce response self._state = "finish" try: nonce = response['data'][0]['nonce'] logging.debug("Nonce received: %r", nonce) key = helpers._auth_key(nonce, self.dbuser, self.dbpass) except Exception, e: self._error(AuthenticationError(e)) return msg = message.query( 0, "%s.$cmd" % self.pool._dbname, 0, 1, SON([('authenticate', 1), ('user', self.dbuser), ('nonce', nonce), ('key', key)]), SON({})) self.connection._send_message(msg, self.process) elif self._state == "finish": self._state = "done" try: assert response['number_returned'] == 1 response = response['data'][0] except Exception, e: self._error(AuthenticationError(e)) return
def authenticate(self): """ Initiates authentication with the remote service and returns a two-tuple containing the storage system URL and session token. """ conn = self.conn_class(self.host, self.port, timeout=self.timeout) #conn = self.conn_class(self.host, self.port) conn.request('GET', '/' + self.uri, headers=self.headers) response = conn.getresponse() response.read() # A status code of 401 indicates that the supplied credentials # were not accepted by the authentication service. if response.status == 401: raise AuthenticationFailed() # Raise an error for any response that is not 2XX if response.status // 100 != 2: raise ResponseError(response.status, response.reason) storage_url = cdn_url = auth_token = None for hdr in response.getheaders(): if hdr[0].lower() == "x-storage-url": storage_url = hdr[1] if hdr[0].lower() == "x-cdn-management-url": cdn_url = hdr[1] if hdr[0].lower() == "x-storage-token": auth_token = hdr[1] if hdr[0].lower() == "x-auth-token": auth_token = hdr[1] conn.close() if not (auth_token and storage_url): raise AuthenticationError("Invalid response from the " \ "authentication service.") return (storage_url, cdn_url, auth_token)
def _checkAuthentication(): """ Check the received request and authenticate the user. Returns ---------- tuple The user identifier and a list of contact identifiers Exceptions ---------- MissingDataError The request contains no body data InvalidDataError The received body data is not a valid protobuf object AuthenticationError The user identifier or authentication token is invalid """ user, auth_token, identifiers = _getDataFromRequest() # Check that the user exists, and that the authentication is valid if not s1.isValidUser(user, auth_token): raise AuthenticationError() return user, identifiers
SON([('authenticate', 1), ('user', self.dbuser), ('nonce', nonce), ('key', key)]), SON({})) self.connection._send_message(msg, self.process) elif self._state == "finish": self._state = "done" try: assert response['number_returned'] == 1 response = response['data'][0] except Exception, e: self._error(AuthenticationError(e)) return if response.get("ok") != 1: logging.debug("Failed authentication %s", response.get("errmsg")) self._error(AuthenticationError(response.get("errmsg"))) return self.connection._next_job() else: self._error(ValueError("Unexpected state: %s" % self._state)) class ConnectRSJob(AsyncJob): def __init__(self, connection, seed, rs, secondary_only, err_callback): super(ConnectRSJob, self).__init__(connection, "seed", err_callback) self.known_hosts = set(seed) self.rs = rs self._blacklisted = set() self._primary = None self._sec_only = secondary_only
def _auth(self, username, password, key_filenames, allow_agent, look_for_keys): saved_exception = None for key_filename in key_filenames: for cls in (paramiko.RSAKey, paramiko.DSSKey): try: key = cls.from_private_key_file(key_filename, password) logger.debug( "Trying key %s from %s" % (hexlify( key.get_fingerprint()), key_filename)) self._transport.auth_publickey(username, key) return except Exception as e: saved_exception = e logger.debug(e) if allow_agent: for key in paramiko.Agent().get_keys(): try: logger.debug("Trying SSH agent key %s" % hexlify(key.get_fingerprint())) self._transport.auth_publickey(username, key) return except Exception as e: saved_exception = e logger.debug(e) keyfiles = [] if look_for_keys: rsa_key = os.path.expanduser("~/.ssh/id_rsa") dsa_key = os.path.expanduser("~/.ssh/id_dsa") if os.path.isfile(rsa_key): keyfiles.append((paramiko.RSAKey, rsa_key)) if os.path.isfile(dsa_key): keyfiles.append((paramiko.DSSKey, dsa_key)) # look in ~/ssh/ for windows users: rsa_key = os.path.expanduser("~/ssh/id_rsa") dsa_key = os.path.expanduser("~/ssh/id_dsa") if os.path.isfile(rsa_key): keyfiles.append((paramiko.RSAKey, rsa_key)) if os.path.isfile(dsa_key): keyfiles.append((paramiko.DSSKey, dsa_key)) for cls, filename in keyfiles: try: key = cls.from_private_key_file(filename, password) logger.debug("Trying discovered key %s in %s" % (hexlify(key.get_fingerprint()), filename)) self._transport.auth_publickey(username, key) return except Exception as e: saved_exception = e logger.debug(e) if password is not None: try: self._transport.auth_password(username, password) return except Exception as e: saved_exception = e logger.debug(e) if saved_exception is not None: # need pep-3134 to do this right raise AuthenticationError(repr(saved_exception)) raise AuthenticationError("No authentication methods available")
def getmail(search_condition=None, dir_path='~/sym_data'): """ Fetch, parse and arrange email contents from specified mail account according to inputted natural language search criteria :param search_condition: natural language english input with email search criteria :param dir_path: path where email attachments are downloaded and stored :return: list of emails retrieved according to selected parameters :raises AuthenticationError: if login fails/could not authenticate :raises SearchCriteriaError: if search criteria could not be built from natural language input """ mail_box, search_criteria = nlp_process(search_condition) response, _ = imap_conn.login(_username, get_password(_username)) if response != 'OK': raise AuthenticationError('Login Failed') # Select gmail mail box to retrieve emails from imap_conn.select(mail_box) mail_list = [] if not isdir(dir_path): mkdir(dir_path) # Filter emails based on IMAP rules response, items = imap_conn.search(None, search_criteria) if response != 'OK': raise SearchCriteriaError( 'Search criteria building failed. Could not parse conditions') mail_items = items[0].split() for email_id in mail_items: # Fetch and parse entire email's contents response, data = imap_conn.fetch(email_id, "(RFC822)") email_body = data[0][1] mail = email.message_from_string(email_body) # Parse sender's name from the email object's from field sender = mail['from'].split()[:-1] sender = ' '.join(sender)[1:-1] attachment = None content = '' counter = 1 if mail.is_multipart(): # Directory to store attachments associated with multipart email email_dir_path = join( dir_path, '{email_num}_{subject}'.format(email_num=email_id, subject=mail['subject'][:5])) mkdir(email_dir_path) for part in mail.walk(): # Skip containers/multipart objects if part.is_multipart(): continue # If part is text/not an attachment if part.get('Content-Disposition') is None: content_type = part.get_content_type() if content_type == 'text/plain' or content_type == 'text/html': # Retrieve body text/contents of email as plain text content = content + '' + part.get_payload() continue # Download and save files attached to email filename = part.get_filename() # If no filename, create filename using a counter to avoid duplicates if not filename: filename = "file_{count}".format(count=counter) counter += 1 attach_dir_path = join(email_dir_path, filename) # Check if file already exists if not isfile(attach_dir_path): with io.open(attach_dir_path, 'wb') as attach_file_path: attach_file_path.write(part.get_payload(decode=True)) attach_file_path.close() else: content = mail.get_payload() # Email contents and data dictionary mail_dict = { 'from': mail['return-path'], 'to': mail['to'], 'date': mail['date'], 'subject': mail['subject'], 'sender': sender, 'content': content, 'attachment': attachment, 'email': mail } mail_list.append(mail_dict) return mail_list
def checkAuthentication(self): """Raises an error if the instance is not authenticated. """ if not self.authenticated: raise AuthenticationError("The instance is not authenticated.")
def get_purchases(): if session['authenticated'] is None or session['authenticated'] == False: raise AuthenticationError() return jsonify(fetch_purchases(session['username']))