Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 18 additions & 62 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from flask import Flask, redirect, request, session, url_for, render_template
from glueops.setup_logging import configure as go_configure_logging
from requests_oauthlib import OAuth2Session
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from werkzeug.middleware.proxy_fix import ProxyFix
Expand All @@ -25,20 +24,13 @@
app.secret_key = secrets.token_urlsafe(24)

try:
client_id = os.environ['GITHUB_CLIENT_ID']
client_secret = os.environ['GITHUB_CLIENT_SECRET']
slack_token = os.environ['SLACK_API_TOKEN']
slack_channel = os.environ['SLACK_CHANNEL']
except KeyError:
logger.exception('could not retrieve environment secret')
raise


authorization_base_url = 'https://github.com/login/oauth/authorize'
token_url = 'https://github.com/login/oauth/access_token'
user_url = 'https://api.github.com/user'
emails_url = 'https://api.github.com/user/emails'


slack_client = WebClient(token=slack_token)

Expand All @@ -48,63 +40,27 @@



@app.route('/')
@app.route('/', methods=["POST"])
def login():
email = request.form.get('email')
Copy link

Copilot AI May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The email input is used without validation – if an empty or invalid email is submitted, it may lead to issues downstream. It is recommended to add basic validation or error handling for this input.

Suggested change
email = request.form.get('email')
email = request.form.get('email')
if not email or not re.match(r"[^@]+@[^@]+\.[^@]+", email):
logger.error(f"Invalid email submitted: {email}")
return "Invalid email address. Please try again.", 400

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@venkatamutyala do you wanna add this ?

session['email'] = email
logger.info(f'email: {email}')
try:
github = OAuth2Session(client_id)
authorization_url, state = github.authorization_url(authorization_base_url)
session['oauth_state'] = state
logger.info(f'oauth_state: {state}')
return redirect(authorization_url)
except Exception:
logger.exception('failed to create GitHub OAuth2Session')

@app.route('/callback')
def callback():
try:
github = OAuth2Session(client_id, state=session['oauth_state'])
token = github.fetch_token(
token_url,
client_secret=client_secret,
authorization_response=request.url
)
session['oauth_token'] = token
return redirect(url_for('.profile'))
except Exception:
logger.exception('failed to generate session oauth token')

@app.route('/profile')
def profile():
try:
github = OAuth2Session(client_id, token=session['oauth_token'])
user = github.get(user_url).json()
logger.info(f'gh user info: {user}')
github_handle = user["login"]
github_emails = github.get(emails_url).json()
email_list = "\n".join([
f"- {email['email']} (Primary: {email['primary']}, Verified: {email['verified']})"
for email in github_emails
])


try:
response = slack_client.chat_postMessage(
channel=slack_channel,
blocks=[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*New signup*\n*GitHub Handle:* `{github_handle}`\n*Email Addresses:*\n{email_list}"
}
response = slack_client.chat_postMessage(
channel=slack_channel,
blocks=[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*New signup*\n*Email Addresses:*\n{email}"
}
]
)
logger.info(f'slack response: {response}')
except SlackApiError as e:
logger.exception(f"Error sending message to Slack: {e.response['error']}")
except Exception:
logger.exception(f'failed to retrieve use metatdata from github')
}
]
)
logger.info(f'slack response: {response}')
except SlackApiError as e:
logger.exception(f"Error sending message to Slack: {e.response['error']}")
return redirect(redirect_url)

@app.route('/logout')
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Flask==3.1.0
requests-oauthlib==2.0.0
slack-sdk==3.33.4
Werkzeug==3.1.3
gunicorn==23.0.0
Expand Down
Loading