def delete(): """ Delete a user session. Removes the domain (config.DOMAIN) section from the native netrc file. """ # Get the native netrc file. netrc = Netrc() # If our domain exists in the netrc file, remove it and save. if config.DOMAIN in netrc.keys(): del netrc[config.DOMAIN] netrc.save()
def _authentication_crenedtials(self, access_token=None, key=None): netrc = Netrc() self._token = access_token or os.getenv( 'ACCESS_TOKEN') or netrc['api.stackexchangepy.com']['login'] self._key = key or os.getenv( 'KEY') or netrc['api.stackexchangepy.com']['password']
def __getLoginData(self): """Gets user login credentials.""" #email = self.configuration.get("LOGIN_DATA", 'email') #password = self.configuration.get("LOGIN_DATA", 'password') netrc = Netrc() email = netrc['localhost']['login'] password = netrc['localhost']['password'] return email, password
def get_creds(): """ Get user credentials from the native netrc file. :return: Dict representation of netrc domain info :rtype: dict """ return Netrc().get(config.DOMAIN, {})
def create(password=None): """ Creates a new user session. Upserts a section to the netrc file, with the following specs: domain --> config.DOMAIN password --> 'password' param :param str password: Session token """ # Get the native netrc file. netrc = Netrc() # Upsert domain/password group netrc[config.DOMAIN]['password'] = password # Save dat bish. netrc.save()
def set_auth(): """Set HTTP auth (used by `requests`) """ # In due course, we should use Github OAuth for this netrc_path = os.path.join(os.path.expanduser("~"), ".netrc") if not os.path.exists(netrc_path): with open(netrc_path, "w") as f: f.write("") netrc = Netrc() if netrc[JOB_SERVER]["password"]: login = netrc[JOB_SERVER]["login"] password = netrc[JOB_SERVER]["password"] else: login = input("Job server username: "******"login": login, "password": password} netrc.save() return (login, password)
def authorization_url(self, client_id=None, scope=[], redirect_uri=None, state=None): """ Returns the address from which to get the code, in order to obtain an access token. There are 3 possible ways for providing crenedtials, which will be used by the package. 1. Give them as a parameter, when you initialize the client. 2. Give them as a enviroment variable. 3. Give them as a netrc file. In that case, provide address to the machine to be api.stackexchange.com, with login your client_id and password - client_secret. :param client_id: The CLIENT ID provided from Stackexchange. :param scope: List of scopes, with which you want the access token to be grant. Scope can be read_inbox, no_expiry, write_access, private_info. For more information, check the official documentation: http://api.stackexchange.com/docs/authentication#scope :param redirect_uri: The uri which was provided to the Stackexchange in which your app will be redirected, to get the code. :param state: :exception: If some of the needed information is missing, like client id, redirect uri or client secret, and error will be raised. """ if not redirect_uri: raise ExchangeException('Redirect URI must be provided.', 'authentication', 400) netrc = Netrc() if not client_id: client_id = os.getenv( 'CLIENT_ID') or netrc['api.stackexchange.com']['login'] if not client_id: raise ExchangeException('Client ID must be provided.', 'authentication', 400) url = '{}?client_id={}&redirect_uri={}'.format(self.OAUTH_URL, client_id, redirect_uri) if scope: url += '&scope={}'.format(','.join(scope)) if state: url += '&state={}'.format(state) return url
def access_token(self, url, client_id=None, client_secret=None, redirect_uri=None): """ Return an access token, and if provided expire date of the token. There are 3 possible ways for providing crenedtials, which will be used by the package. 1. Give them as a parameter, when you initialize the client. 2. Give them as a enviroment variable. 3. Give them as a netrc file. In that case, provide address to the machine to be api.stackexchange.com, with login your client_id and password - client_secret. :param url: Which was returned from authenticating your app, with the code. :param client_id: Client ID which was provided from the api. :param client_secret: Client Secret which was provided from the api. :param redirect_uri: The redirect uri which was provided in the api. """ if not redirect_uri: raise ExchangeException('Redirect URI must be provided.', 'authentication', 400) netrc = Netrc() if not client_id: client_id = os.getenv( 'CLIENT_ID') or netrc['api.stackexchange.com']['login'] if not client_id: raise ExchangeException('Client ID must be provided.', 'authentication', 400) if not client_secret: client_secret = os.getenv( 'CLIENT_SECRET') or netrc['api.stackexchange.com']['password'] if not client_secret: raise ExchangeException('Client secret must be provided.', 'authentication', 400) code = re.match(r".*(code=)(.*)", url) if not code: raise ExchangeException('Something went wrong. Code is not found.', 'authentication', 404) params = { 'client_id': client_id, 'client_secret': client_secret, 'redirect_uri': redirect_uri, 'code': code.group(2) } response = requests.post(self.ACCESS_TOKEN_URL, params=params) if response.status_code != requests.codes.ok: response = response.text raise ExchangeException(response['error_message'], response['error_name'], response['error_id']) response_info = response.text.split("&") access_token = response_info[0].split("=")[1] expire = response_info[1].split("=")[1] return {'access_token': access_token, 'expire': expire}
def get_netrc_obj(): filepath = pathlib.Path().home().joinpath(".netrc") filepath.touch(exist_ok=True) netrc = Netrc(str(filepath)) host = netrc["pyintelowl"] return netrc, host