· 5 years ago · May 06, 2020, 04:12 PM
1#!/usr/bin/env python3
2
3import argparse
4import requests
5import requests_oauthlib
6from requests_oauthlib import OAuth1Session
7import sys
8
9parser = argparse.ArgumentParser(description='Atlassian OAuth helper')
10parser.add_argument('BASEURL', help='The base URL (with context) of \
11 your JIRA/Confluence instance. e.g. \
12 "https://www.example.com/confluence"')
13parser.add_argument('CONSUMER', help='The consumer key specified \
14 when the Jira/Confluence application link was set \
15 up.')
16parser.add_argument('KEYFILE', help='The private RSA key used to set \
17 up your Jira/Confluence application link.')
18args = parser.parse_args()
19
20client_key = args.CONSUMER
21base_url = '{}/plugins/servlet/oauth/request-token'.format(args.BASEURL)
22
23try:
24 client_secret = open(args.KEYFILE).read()
25except FileNotFoundError:
26 print('Unable to open RSA key. Check path/permissions to file.')
27 sys.exit()
28
29#########################################
30# OAuth Stage 1: Obtain a request token #
31#########################################
32
33oauth = OAuth1Session(client_key, signature_method='RSA-SHA1',
34 rsa_key=client_secret, callback_uri='oob')
35
36fetch_response = oauth.fetch_request_token(base_url)
37print(fetch_response)
38
39try:
40 fetch_response = oauth.fetch_request_token(base_url + 'request-token')
41except requests.exceptions.ConnectionError:
42 print(('Unable to resolve host or connection refused. '
43 'Check URL and context is correct.'))
44 sys.exit()
45except requests.exceptions.Timeout:
46 print(('Connection timed out. Is your Jira/Confluence '
47 'instance available/reachable?'))
48 sys.exit()
49except requests_oauthlib.oauth1_session.TokenRequestDenied:
50 print(('Authorization failed. Is your application link '
51 'configured correctly?'))
52 sys.exit()
53resource_owner_key = fetch_response.get('oauth_token')
54resource_owner_secret = fetch_response.get('oauth_token_secret')
55
56#################################################
57# OAuth Stage 2: Obtain authorization from user #
58#################################################
59
60authorization_url = oauth.authorization_url(base_url + 'authorize')
61print(('Click on the following link, and log in with the user you '
62 'want to use for this application. Make a note of the code '
63 'JIRA spits out, and then come back here.'))
64print(authorization_url + '\n')
65verifier = input('Paste the verification code here: ')
66
67##########################################
68# OAuth Stage 3: Request an access token #
69##########################################
70
71oauth = OAuth1Session(client_key, signature_method='RSA-SHA1',
72 rsa_key=client_secret,
73 resource_owner_key=resource_owner_key,
74 resource_owner_secret=resource_owner_secret,
75 verifier=verifier)
76try:
77 oauth_token = oauth.fetch_access_token(base_url + 'access-token')
78except requests_oauthlib.oauth1_session.TokenRequestDenied:
79 print('Authorization failed. Verification code incorrect?')
80 sys.exit()
81resource_owner_key = oauth_token.get('oauth_token')
82resource_owner_secret = oauth_token.get('oauth_token_secret')
83
84print('OAuth key: ' + resource_owner_key)
85print('OAuth secret: ' + resource_owner_secret)