Skip to content

Commit 36ea7f2

Browse files
committed
* remove unwated vars
* udate timeout loging to wait for 15 seconds only
1 parent cdece81 commit 36ea7f2

File tree

3 files changed

+33
-46
lines changed

3 files changed

+33
-46
lines changed

src/config.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,4 @@
1010

1111
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
1212
GITHUB_APP_TOKEN = os.environ.get("GITHUB_APP_TOKEN")
13-
APP_AUTH_ID = "Iv1.029716a4d1524dcd"
14-
APP_AUTH_PRIVATE_KEY_B64 = os.environ.get("APP_AUTH_PRIVATE_KEY_B64")
15-
APP_AUTH_INSTALLATION_ID = os.environ.get("APP_AUTH_INSTALLATION_ID")
16-
APP_AUTH_BASE_URL = "https://api.github.com"
13+
APP_AUTH_ID = "Iv1.029716a4d1524dcd"

src/helpers.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"""Helpers Module"""
2-
import datetime
32
import time
43
import re
54
import base64
65
import argparse
76
from concurrent.futures import ThreadPoolExecutor
8-
import jwt
97
import requests
108
from flask import request
119
from config import (
@@ -65,7 +63,7 @@ def get_token():
6563
raise Exception("Github APP token not found.")
6664
return token
6765

68-
def authenticate_with_device_flow():
66+
def authenticate_with_device_flow(logger):
6967
device_code_url = "https://github.com/login/device/code"
7068
client_id = APP_AUTH_ID
7169
payload = {
@@ -75,52 +73,46 @@ def authenticate_with_device_flow():
7573

7674
headers = {
7775
"Accept": "application/json"
78-
}
79-
80-
response = requests.post(device_code_url, json=payload, headers=headers)
81-
if response.status_code == 200:
82-
data = response.json()
83-
device_code = data['device_code']
84-
user_code = data['user_code']
85-
verification_uri = data['verification_uri']
86-
print(f"\nActivate the app by accessing: {verification_uri}")
87-
print(f"and enter this activation code: {user_code}")
88-
89-
token_url = "https://github.com/login/oauth/access_token"
90-
max_attempts = 2
91-
attempt = 0
92-
delay = 5
93-
94-
while attempt < max_attempts:
95-
attempt += 1
96-
76+
}
77+
78+
try:
79+
response = requests.post(device_code_url, json=payload, headers=headers)
80+
if response.status_code == 200:
81+
data = response.json()
82+
device_code = data['device_code']
83+
user_code = data['user_code']
84+
verification_uri = data['verification_uri']
85+
86+
print(f"\nActivate GitHub authentication at: {verification_uri}")
87+
print(f"Enter activation code: {user_code}")
88+
89+
print(f"\nWaiting 30 seconds for the user to authorize the device...\n")
90+
time.sleep(30)
91+
92+
token_url = "https://github.com/login/oauth/access_token"
9793
token_response = requests.post(token_url, json={
9894
"client_id": client_id,
9995
"device_code": device_code,
10096
"grant_type": "urn:ietf:params:oauth:grant-type:device_code"
10197
}, headers={"Accept": "application/json"})
10298

103-
if token_response.status_code == 200:
104-
token_data = token_response.json()
105-
return token_data.get("access_token")
106-
elif token_response.status_code == 400:
107-
error_data = token_response.json()
108-
if error_data.get("error") == "authorization_pending":
109-
print(f"Authorization pending... Attempt {attempt}/{max_attempts}. Retrying in {delay} seconds.")
110-
time.sleep(delay)
111-
else:
112-
print(f"Error: {error_data.get('error_description')}")
113-
return None
99+
token_data = token_response.json()
100+
token_value = token_data.get("access_token")
101+
102+
if token_value is not None:
103+
logger.info("Successfully obtained access token.")
104+
print(f"Please set env var GITHUB_APP_TOKEN={token_value}")
105+
print(f"and restart the app.")
114106
else:
115-
print(f"Unexpected response: {token_response.status_code} {token_response.text}")
107+
logger.error(f"Failed to obtain access token. Status: {token_response.status_code}, Response: {token_response.text}")
116108
return None
109+
else:
110+
logger.error(f"Failed to initiate device flow: {response.status_code} {response.text}")
111+
return None
117112

118-
print("Maximum attempts reached. Please try again.")
113+
except Exception as e:
114+
logger.exception(f"Error during device flow authentication: {str(e)}")
119115
return None
120-
else:
121-
raise Exception(f"Failed to initiate device flow: {response.status_code} {response.text}")
122-
123-
124116

125117
def get_workflows(owner, repo, headers):
126118
"""Get the workflows for a given owner and repo from the GitHub API.

src/routes.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,8 @@ def index():
8686
def auth():
8787
"""Endpoint for handling Device Flow authentication."""
8888
try:
89-
# Start the device flow in a separate thread
90-
threading.Thread(target=authenticate_with_device_flow).start()
89+
threading.Thread(target=authenticate_with_device_flow, args=(logger,)).start()
9190

92-
# Provide immediate feedback to the client
9391
return make_response("Authentication process started. Please check your console for instructions.", 200)
9492
except Exception as e:
9593
logger.error("An error occurred during authentication: %s", str(e))

0 commit comments

Comments
 (0)