mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 22:43:42 +00:00
Create GitHub authentication module.
This commit is contained in:
committed by
Tim Abbott
parent
fe1848377c
commit
89d1f96db9
@@ -1,25 +1,56 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# The purpose of this file is to handle all requests
|
||||
# to the Github API, which will make sure that all
|
||||
# requests are to the same account, and that all requests
|
||||
# authenticated correctly and securely
|
||||
# The sole purpose of this is to authenticate, and it will
|
||||
# return the requests session that is properly authenticated
|
||||
import logging
|
||||
import os
|
||||
import requests
|
||||
import six.moves.configparser
|
||||
|
||||
CONFIG_FILE = '~/.github-issue-bot/github-issue-bot.conf'
|
||||
|
||||
# This file contains the oauth token for a github user, their username, and optionally, a repository
|
||||
# name and owner. All in the format:
|
||||
# github_repo
|
||||
# github_repo_owner
|
||||
# github_username
|
||||
# github_token
|
||||
CONFIG_FILE = '~/.github-auth.conf'
|
||||
|
||||
global config
|
||||
config = six.moves.configparser.ConfigParser() # Sets up the configParser to read the .conf file
|
||||
config.read([os.path.expanduser(CONFIG_FILE)]) # Reads the config file
|
||||
|
||||
|
||||
def auth():
|
||||
# Sets up the configParser to read the .conf file
|
||||
config = six.moves.configparser.ConfigParser()
|
||||
# Reads the config file
|
||||
config.read([os.path.expanduser(CONFIG_FILE)])
|
||||
# Gets the properties from the config file
|
||||
oauth_token = config.get('github', 'github_token')
|
||||
username = config.get('github', 'github_username')
|
||||
# Creates and authorises a requests session
|
||||
session = requests.session()
|
||||
session.auth = (username, oauth_token)
|
||||
# Returns the session
|
||||
session.auth = (get_username(), get_oauth_token())
|
||||
|
||||
return session
|
||||
|
||||
|
||||
def get_oauth_token():
|
||||
_get_data('token')
|
||||
|
||||
|
||||
def get_username():
|
||||
_get_data('username')
|
||||
|
||||
|
||||
def get_repo():
|
||||
_get_data('repo')
|
||||
|
||||
|
||||
def get_repo_owner():
|
||||
_get_data('repo_owner')
|
||||
|
||||
|
||||
def _get_data(key):
|
||||
try:
|
||||
return config.get('github', 'github_%s' % (key))
|
||||
except Exception:
|
||||
logging.exception('GitHub %s not supplied in ~/.github-auth.conf.' % (key))
|
||||
|
||||
Reference in New Issue
Block a user