-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·76 lines (64 loc) · 2.37 KB
/
Copy pathapp.py
File metadata and controls
executable file
·76 lines (64 loc) · 2.37 KB
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
#!/usr/bin/env python
from re import search
from os import environ
from requests import Session
from flask import Flask, request
app = Flask(__name__)
app.config.update(TOKEN=environ['TOKEN'],
SLACK_TEAM=environ['SLACK_TEAM'],
SLACK_EMAIL=environ['SLACK_EMAIL'],
SLACK_PASSWORD=environ['SLACK_PASSWORD'])
slack_url = 'https://{teamname}.slack.com'.format(teamname=app.config['SLACK_TEAM'])
invite_url = '{}/admin/invites'.format(slack_url)
gateway_url = '{}/account/gateways'.format(slack_url)
crumb_re = r'type="hidden" name="crumb" value="(.*)" />'
invite_form = """
<h1>join the qhack chat slack</h1>
<form action="" method="post">
<label for="email">email address</label>
<input type="email" name="email" id="email" placeholder="email address" />
<button type="submit">join</button>
</form>
"""
welcome = """
<h1>invited. check your email.</h1>
<p>Once you're in slack, you can either use their UI, or connect via IRC or XMPP.
IRC and XMPP config details will be at <a href="{gateway_url}">/account/gateways</a></p>
""".format(gateway_url=gateway_url)
def login(session):
login_page_resp = session.get(slack_url)
assert login_page_resp.ok, 'could not load login page'
crumb = search(crumb_re, login_page_resp.text).groups()[0]
auth = {'signin': 1,
'email': app.config['SLACK_EMAIL'],
'password': app.config['SLACK_PASSWORD'],
'crumb': crumb}
resp = session.post(slack_url, data=auth)
assert resp.ok, 'could not log in'
def post_invite(session, email):
invite_page_resp = session.get(invite_url)
assert invite_page_resp.ok, 'could not load invite page'
crumb = search(crumb_re, invite_page_resp.text).groups()[0]
invite = {'invite': 1,
'crumb': crumb,
'emails': email}
resp = session.post(invite_url, data=invite)
assert resp.ok, 'could not invite {}'.format(email)
def invite(email):
session = Session()
login(session)
post_invite(session, email)
@app.route('/join-slack/<token>', methods=['GET', 'POST'])
def signup(token):
if token != app.config['TOKEN']:
return 'nope', 401
email = request.form.get('email')
if email is not None:
invite(email)
return welcome
return invite_form
@app.route('/')
def hello():
return 'nope', 401
if __name__ == '__main__':
app.run(debug=True)