From a957bca46222cff0877cf0999a0d01b3f46c1d22 Mon Sep 17 00:00:00 2001 From: Manas Date: Jun 13 2019 13:50:29 +0000 Subject: functionality division as per functions/services ,bz,tga,pagure --- diff --git a/bz/bugzilla_auth.py b/bz/bugzilla_auth.py new file mode 100644 index 0000000..ae5603c --- /dev/null +++ b/bz/bugzilla_auth.py @@ -0,0 +1,41 @@ +import requests +import json +import keyring +import random +import os +import binascii +import getpass +import configparser + +config = configparser.ConfigParser() +config.read('config.ini') + +service_id = config['BUGZILLA']['SERVICE_ID'] # service id +service_token = config['BUGZILLA']['SERVICE_TOKEN'] # service d for auth +bz_url = config['BUGZILLA']['DEFAULT_BUGZILLA_URL'] # taiga server note +custom_bz = config['BUGZILLA']['CUSTOM_BUGZILLA_URL'] + +""" +bugzilla uses a api key to enable the api acess +Currently there is a xml-rpc method to acess +That method is going to be deprecated soon +Thus the latest method uses API key to acess +the api token.is sued by the modern rest api +""" +def reset(): + print("You User Id and password will be securly store in the Keyring") + username = input("Enter your user id for bugzilla: ") + password = getpass.getpass("Enter your password for bugzilla: ") + api_key = getpass.getpass("Enter your bugzilla api: ") + try: + # keyring.set_password("taiga", username) + # keyring.set_password(service_id, "username", username) + keyring.set_password(service_id, username, str(password)) + keyring.set_password(service_id, username, api_key) + username = binascii.hexlify(os.urandom(20)) # randomising username to avoid sneak memory attacks + password = binascii.hexlify(os.urandom(20)) # ranomising pass to avoid sneak memory attacks + # username = random.choices(string.ascii_uppercase + string.digits, k=random.randint(1,10)) + # passwd = random.choices(string.ascii_uppercase + string.digits, k=random.randint(1,10)) + print("Password and api key reset succesfully") + except (keyring.errors.PasswordSetError, Exception) as e: + print("Failed to reset password & api key", e) diff --git a/config_load.py b/config_load.py new file mode 100644 index 0000000..b2b55e7 --- /dev/null +++ b/config_load.py @@ -0,0 +1,34 @@ +from configparser import SafeConfigParser +import os + + +class Config: + """Interact with configuration variables.""" + + configParser = SafeConfigParser() + configFilePath = (os.path.join(os.getcwd(), 'config.ini')) + + @classmethod + def initialize(cls, new_config): + """Start config by reading config.ini.""" + cls.configParser.read(cls.configFilePath) + + @classmethod + def taiga(cls, key): + """Get taiga values from config.ini.""" + return cls.configParser.get('TAIGA', key) + + @classmethod + def pagure(cls, key): + """Get pagure values from config.ini.""" + return cls.configParser.get('PAGURE', key) + + @classmethod + def bugzilla(cls, key): + """Get bugzilla values from config.ini""" + return cls.configParser.get('BUGZILLA', key) + + @classmethod + def email(cls, key): + """Get bugzilla values from config.ini""" + return cls.configParser.get('EMAIL', key) diff --git a/pgure/pagure_auth.py b/pgure/pagure_auth.py new file mode 100644 index 0000000..dfe3ebb --- /dev/null +++ b/pgure/pagure_auth.py @@ -0,0 +1,26 @@ +import requests +import json +import keyring +import random +import os +import binascii +import getpass +import configparser + +config = configparser.ConfigParser() +config.read('config.ini') + + +def reset(): + print("Your auth token will be securly store in the Keyring") + # username = input("Enter your user id for taiga: ") + api_token = "api_token" + repo_name = input("Enter the repo name you want to set api key for: ") + token = getpass.getpass("Enter your token for" + ' ' + repo_name + ' ' "repository: ") + try: + # keyring.set_password("taiga", username) + keyring.set_password(repo_name, api_token, token) + print("token reset succesfully") + except (keyring.errors.PasswordSetError, Exception) as e: + print("Failed to reset token") + # print(e + "Contact the developers asap") diff --git a/tga/taiga_auth.py b/tga/taiga_auth.py new file mode 100644 index 0000000..f046771 --- /dev/null +++ b/tga/taiga_auth.py @@ -0,0 +1,91 @@ +import requests +import json +import keyring +import random +import os +import binascii +import getpass +import configparser +import keyring + +config = configparser.ConfigParser() +config.read('config.ini') + +service_id = config['TAIGA']['SERVICE_ID'] # service id +service_token = config['TAIGA']['SERVICE_TOKEN'] # service d for auth +# taiga_url = config['TAIGA']['DEFAULT_TAIGA_URL'] # taiga server note +# custom_taiga = config['TAIGA']['CUSTOM_TAIGA_URL'] +project_id = config['TAIGA']['PROJECT_ID'] +if config['TAIGA']['TEST'] == 'yes': + taiga_url = config['TAIGA']['CUSTOM_TAIGA_URL'] +elif config['TAIGA']['TEST'] == 'no': + taiga_url = config['TAIGA']['DEFAULT_TAIGA_URL'] +else: + print("please set test status in config") + + +headers = { + 'Content-Type': 'application/json', +} + + +def refresh_token(): + """ + Used only for testing and Demo + Encrypted Config file/Application token + used in production. + Doing the follwoing in prod is security risk. + """ + username = keyring.get_password(service_id, "taiga_username") + print(username) # debug only + payload = { + 'type': 'normal', + 'username': username, + 'password': keyring.get_password(service_id, username) + } + print(payload) # only for debugging + + try: + r = requests.post( + str(taiga_url) + "/api/v1/auth", + data=json.dumps(payload), + headers=headers) + print(r.status_code) + if r.status_code == 200: + """ + with open('auth.txt', 'w') as a_file: + a_file.write(r.json()['auth_token']) + """ + try: + keyring.set_password(service_token, "taiga_token", r.json()['auth_token']) + print("succesful authentication") + except (keyring.errors.PasswordSetError, Exception) as e: + print("Failed to set auth token") + elif r.status_code == 400: + print("auth error") + except requests.exceptions.RequestException as e: + print(e) + + +def reset(): + print("You User Id and password will be securly store in the Keyring") + username = input("Enter your user id for taiga: ") + password = getpass.getpass("Enter your password for taiga: ") + try: + """ + So a workaround to store username along with pass is being used + the only drawback ofthis work around is in the gui keyring broswer + a seperate username keyring will show,sadly this cannot be avoided + at the point of writing this peice of software + """ + """ + The above comment is not true + """ + keyring.set_password(service_id, "taiga_username", username) # refer line 67 + keyring.set_password(service_id, username, str(password)) + username = binascii.hexlify(os.urandom(20)) # randomising username to avoid sneak memory attacks + password = binascii.hexlify(os.urandom(20)) # ranomising pass to avoid sneak memory attacks + print("Password reset succesfully") + except (keyring.errors.PasswordSetError, Exception) as e: + print("Failed to reset password") +