-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_server.py
73 lines (61 loc) · 2.1 KB
/
callback_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, request, jsonify, redirect
import threading
import requests
import os
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
CLIENT_ID = os.getenv('CLIENT_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
REDIRECT_URI = os.getenv('REDIRECT_URI')
AUTH_URL = os.getenv('AUTH_URL')
TOKEN_URL = os.getenv('TOKEN_URL')
SCOPES = os.getenv('SCOPES')
access_token = None # Global variable to store the access token
@app.route('/callback')
def callback():
global access_token
code = request.args.get('code')
if code:
data = {
'grant_type': 'authorization_code',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'code': code,
'redirect_uri': REDIRECT_URI
}
response = requests.post(TOKEN_URL, data=data)
if response.status_code == 200:
access_token = response.json()['access_token']
return redirect("/success")
else:
return jsonify({"error": "Failed to authenticate with Atlassian"}), 400
else:
return jsonify({"error": "Missing authorization code"}), 400
@app.route('/success')
def success():
return "Authentication successful. You can now close this window and return to the application."
@app.route('/shutdown', methods=['POST'])
def shutdown():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return 'Server shutting down...'
def run_server():
app.run(host='0.0.0.0', port=8000)
def start_server():
server_thread = threading.Thread(target=run_server)
server_thread.daemon = True # Allow server to be killed when main thread exits
server_thread.start()
def stop_server():
# Send a request to the shutdown endpoint
try:
requests.post('http://localhost:8000/shutdown')
except requests.exceptions.RequestException as e:
print(f"Error shutting down server: {e}")
def get_access_token():
global access_token
return access_token
if __name__ == '__main__':
start_server()