Skip to content

Commit

Permalink
Added password storage and retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
mohoromitch committed Aug 28, 2020
1 parent e6afa9c commit 0670299
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
14 changes: 14 additions & 0 deletions auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import keyring

class Auth:
@staticmethod
def getCredential(app, username):
return keyring.get_credential(app, username)

@staticmethod
def getPassword(app, username):
return keyring.get_password(app, username)

@staticmethod
def setPassword(app, username, password):
keyring.set_password(app, username, password)
13 changes: 7 additions & 6 deletions constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
INSTAGRAM = "instagram"
TWITTER = "twitter"
class AppConstant:
INSTAGRAM = "instagram"
TWITTER = "twitter"

APP_INPUT_MAP = {
"instagram": INSTAGRAM,
"i": INSTAGRAM,
"twitter": TWITTER,
"t": TWITTER
"instagram": AppConstant.INSTAGRAM,
"i": AppConstant.INSTAGRAM,
"twitter": AppConstant.TWITTER,
"t": AppConstant.TWITTER
}
5 changes: 3 additions & 2 deletions instaClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
class InstaClient:
def __init__(self, username, password):
self.username = username,
self.client = InstagramAPI(username, password)
self.client.login()
self.client = InstagramAPI(username, password)
self.followers = []
self.following = []

self.client.login()

def getFollowersUsernames(self):
"""return the account's followers, lazy loaded"""
if not self.followers:
Expand Down
47 changes: 33 additions & 14 deletions vankit.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import click
from pprint import pprint
from auth import Auth
from instaClient import InstaClient

from constants import INSTAGRAM, TWITTER
from constants import AppConstant
from constants import APP_INPUT_MAP

def start_twitter(app, username, password):
click.echo("Twitter isn't supported yet.")

def start_instagram(app, username, password):
click.echo("Attempting logging in as %s..." % username)
instaClient = InstaClient(username, password)
pprint(instaClient.getFollowersUsernames())
pprint(instaClient.getFollowingUsernames())
pprint(instaClient.getFollowersNotFollowingBack())
pprint(instaClient.getFollowingNotFollowingBack())
try:
print("Attempting logging in as %s..." % username)
instaClient = InstaClient(username, password)
pprint(instaClient.getFollowersUsernames())
pprint(instaClient.getFollowingUsernames())
pprint(instaClient.getFollowersNotFollowingBack())
pprint(instaClient.getFollowingNotFollowingBack())
except Exception:
print(f"Couldn't log into {app}!")
return

app_router = {
INSTAGRAM: start_instagram,
TWITTER: start_twitter
AppConstant.INSTAGRAM: start_instagram,
AppConstant.TWITTER: start_twitter
}

def validate_app(ctx, param, value):
if value not in APP_INPUT_MAP:
click.echo("{} is not a supported application.".format(value))
click.echo(APP_INPUT_MAP)
ctx.exit()
return value
return APP_INPUT_MAP.get(value)

def validate_username(ctx, param, value):
if " " in value:
Expand All @@ -37,10 +42,24 @@ def validate_username(ctx, param, value):
@click.command()
@click.option("--app", prompt="Which app to log into", help="The webapp you want to use.", callback=validate_app)
@click.option("--username", prompt="Please enter your username", help="Your app specific login username", callback=validate_username)
@click.option("--password", prompt="Please enter your password", help="Your app specific login password", hide_input=True)
def start(app, username, password):
click.echo("Logging into {} for {}".format(APP_INPUT_MAP[app], username))
def prompt_main(app, username):

password = Auth.getPassword(app, username)

if not password:
print(f"No saved password found for {username}...")
password = click.prompt("Please enter your password", type=str, hide_input=True)
else:
print(f"{app} password found for {username}!")

should_save_password = str(click.prompt("Do you want to save this password in Keychain? [y/n]", type=str)).strip().lower() == "y"

if should_save_password:
print("Saving password in keychain...")
Auth.setPassword(app, username, password)

print("Logging into {} for {}".format(APP_INPUT_MAP[app], username))
app_router[APP_INPUT_MAP[app]](app, username, password)

if __name__ == "__main__":
start(None, None, None)
prompt_main(None, None)

0 comments on commit 0670299

Please sign in to comment.