-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
117 lines (96 loc) · 3.72 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import secrets
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
# configure logger
logger = go_configure_logging(
name='SIGNUP',
level=os.getenv('PYTHON_LOG_LEVEL', 'INFO')
)
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1, x_prefix=1)
try:
app.secret_key = os.environ['APP_SECRET_KEY']
except KeyError:
logger.warn(f'could not retrieve APP_SECRET_KEY from env, falling back to generated secret')
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)
redirect_url = os.getenv('REDIRECT_URL', 'https://www.glueops.dev/book-a-demo/')
APP_PORT = os.getenv('APP_PORT', 5000)
DEBUG_ENABLED = os.getenv('DEBUG_ENABLED', False)
@app.route('/')
def login():
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}"
}
}
]
)
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')
return redirect(redirect_url)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=APP_PORT, debug=DEBUG_ENABLED)