Skip to content

Instantly share code, notes, and snippets.

@viktor-evdokimov
Forked from okoye/authenticate.py
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viktor-evdokimov/fe1c58955999d0a34b24 to your computer and use it in GitHub Desktop.
Save viktor-evdokimov/fe1c58955999d0a34b24 to your computer and use it in GitHub Desktop.
'''
sample code to demonstrate using JIRA's oauth API
'''
from rauth.session import OAuth1Session
from rauth.oauth import RsaSha1Signature
from Crypto.PublicKey import RSA
from Crypto.Hash import MD5
from Crypto import Random
from jira.client import JIRA
############### helper routines ###############
def fetch_configuration(config_file='configuration.txt'):
#open file, parse config and return key, value pairs found in dict
config = {}
for line in open(config_file):
key, value = line.strip().split('=')
config[key] = value
return config
def generate_keys():
rng = Random.new().read
rsa_key = RSA.generate(2048, rng)
assert(rsa_key.has_private())
return rsa_key
############# main oauth method ##############
def dance0():
#first, compute RsaSha1Signature
#WARNING: PyCrypto must already be installed or this will __fail__
config = fetch_configuration()
crypt = RsaSha1Signature()
key = generate_keys()
session = OAuth1Session(config.get('consumer_key'),
key.exportKey(),
access_token=config.get('access_token'),
access_token_secret=config.get('access_token_secret'),
signature=RsaSha1Signature)
print session.get(config.get('url'), verify=False)
def dance1():
keys = generate_keys()
config = fetch_configuration()
oauth_parameters = {
'access_token': config.get('access_token'),
'access_token_secret': config.get('access_token_secret'),
'consumer_key': config.get('consumer_key'),
'key_cert': keys.exportKey()
}
options = {
'server': 'https://ecomjira.wsgc.com',
'verify': False
}
authenticated_jira = JIRA(oauth=oauth_parameters, options=options)
#print authenticated_jira.issue('PNP-8609')
print authenticated_jira.projects()
if __name__ == '__main__':
dance0()
dance1()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment