Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from unittest import TestCase
from nineapi.client import Client, APIException
import os
import sys
import keyring
#While using the program the follwing command should be run only once,the system will store the creds.
username=""
password="" #This should be removed after setting the password
keyring.set_password('9gag',username,password)
class APITest(TestCase):
def setUp(self):
self.client = Client()
self.username = username
self.password = keyring.get_password('9gag',username)
def test_log_in_good(self):
response = self.client.log_in(self.username, self.password)
self.assertEqual(response, True)
def test_log_in_bad(self):
self.assertRaises(APIException, lambda: self.client.log_in(self.username, self.password + 'wrong'))
def test_get_posts(self):
self.test_log_in_good()
def save_accounts(accounts, filename):
content = {}
for acc in accounts:
# Save password.
set_password(SERVICE_NAME, acc.username, acc.password)
# Save infos.
infos = acc.infos
infos['is_md5'] = acc.is_md5
infos['last_check'] = acc.last_check
infos['valid'] = acc.valid
content[acc.username] = infos
yaml.dump(content, open(filename, 'w', encoding='utf-8'),
default_flow_style=False,
allow_unicode=True)
else:
config.password = util.Util.get_password("Google Password: ")
else:
config.password = util.Util.get_password("Google Password: ")
# Validate Options
config.raise_if_invalid()
google_client = google.Google(config, args.save_failure_html)
google_client.do_login()
saml_xml = google_client.parse_saml()
logging.debug('%s: saml assertion is: %s', __name__, saml_xml)
# If we logged in correctly and we are using keyring then store the password
if config.keyring and keyring_password is None:
keyring.set_password(
"aws-google-auth", config.username, config.password)
# We now have a new SAML value that can get cached (If the user asked
# for it to be)
if args.saml_cache:
config.saml_cache = saml_xml
# The amazon_client now has the SAML assertion it needed (Either via the
# cache or freshly generated). From here, we can get the roles and continue
# the rest of the workflow regardless of cache.
amazon_client = amazon.Amazon(config, saml_xml)
roles = amazon_client.roles
# Determine the provider and the role arn (if the the user provided isn't an option)
if config.role_arn in roles and not config.ask_role:
config.provider = roles[config.role_arn]
def set_keychain(journal_name, password):
import keyring
if password is None:
try:
keyring.delete_password('jrnl', journal_name)
except RuntimeError:
pass
else:
keyring.set_password('jrnl', journal_name, password)
except:
#TODO: If system keyring daemon is not running, an assertion will throw here -- need to be more informative to the user
password = None
if password is not None:
d['password'].entry.set_text(password)
d['remember'].set_active(True)
response=d.run()
dfd = d.get_form_data()
d.destroy()
if response == 1:
password = dfd['password']
remember = dfd['remember']
if remember and password != '':
try:
keyring.set_password(KEYRING_SERVICE_NAME, user, password)
except:
#TODO: If system keyring daemon is not running, an error will be raised -- need to be more informative to the user
pass
elif not remember:
try:
# only available in newer versions
keyring.delete_password(KEYRING_SERVICE_NAME, user)
except:
# maybe give a warning?
pass
return password
return None
def set_secret(name, secret):
"""Stores a secret into the keyring, creating or updating the relevant stanza
in `ansible.cfg' in the process.
"""
c = load_ansible_cfg()
if not c.has_section("vault"):
c.add_section("vault")
c.set(CFG_SECTION, CFG_OPTION, name)
with open(get_ansible_cfg_path(), "w") as fp:
c.write(fp)
keyring.set_password(KEYRING_SERVICE, name, secret)
user = input("Username: ")
pw = getpass.getpass("Password: ")
auth_request = {
"scopes": [
"public_repo",
"gist"
],
"note": "IPython tools",
"note_url": "https://github.com/ipython/ipython/tree/master/tools",
}
response = requests.post('https://api.github.com/authorizations',
auth=(user, pw), data=json.dumps(auth_request))
response.raise_for_status()
token = json.loads(response.text)['token']
keyring.set_password('github', fake_username, token)
return token
def _prompt_password_storing(https_username):
https_password = getpass.getpass()
store_password = raw_input(
"Do you want to store your password in encrypted form (y or n)? ")
if store_password == "y":
keyring.set_password(KEYRING_KEY, https_username, https_password)
return https_password
"refresh_token": "bar",
"id_token": "baz",
"token_type": "Bearer"
}
"""
response_body = auth_token_resp.json()
if "access_token" not in response_body:
raise ValueError('Expected "access_token" in response from oauth server')
if "refresh_token" in response_body:
self._refresh_token = response_body["refresh_token"]
access_token = response_body["access_token"]
id_token = response_body["id_token"]
refresh_token = response_body["refresh_token"]
_keyring.set_password(_keyring_service_name, _keyring_access_token_storage_key, access_token)
_keyring.set_password(_keyring_service_name, _keyring_id_token_storage_key, id_token)
_keyring.set_password(_keyring_service_name, _keyring_refresh_token_storage_key, refresh_token)
self._credentials = Credentials(access_token=access_token, id_token=id_token)
def store_password_in_keyring(username, password):
return keyring.set_password(
KEYRING_SYSTEM,
username,
password,
)