MCPcopy Create free account
hub / github.com/ipython/ipython / get_auth_token

Function get_auth_token

tools/gh_api.py:40–79  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

38
39token = None
40def get_auth_token():
41 global token
42
43 if token is not None:
44 return token
45
46 import keyring
47 token = keyring.get_password('github', fake_username)
48 if token is not None:
49 return token
50
51 print("Please enter your github username and password. These are not "
52 "stored, only used to get an oAuth token. You can revoke this at "
53 "any time on Github.\n"
54 "Username: ", file=sys.stderr, end='')
55 user = input('')
56 pw = getpass.getpass("Password: ", stream=sys.stderr)
57
58 auth_request = {
59 "scopes": [
60 "public_repo",
61 "gist"
62 ],
63 "note": "IPython tools %s" % socket.gethostname(),
64 "note_url": "https://github.com/ipython/ipython/tree/master/tools",
65 }
66 response = requests.post('https://api.github.com/authorizations',
67 auth=(user, pw), data=json.dumps(auth_request))
68 if response.status_code == 401 and \
69 'required;' in response.headers.get('X-GitHub-OTP', ''):
70 print("Your login API requested a one time password", file=sys.stderr)
71 otp = getpass.getpass("One Time Password: ", stream=sys.stderr)
72 response = requests.post('https://api.github.com/authorizations',
73 auth=(user, pw),
74 data=json.dumps(auth_request),
75 headers={'X-GitHub-OTP':otp})
76 response.raise_for_status()
77 token = json.loads(response.text)['token']
78 keyring.set_password('github', fake_username, token)
79 return token
80
81def make_auth_header():
82 return {'Authorization': 'token ' + get_auth_token()}

Callers 1

make_auth_headerFunction · 0.85

Calls 1

inputFunction · 0.85

Tested by

no test coverage detected