This repository was archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathslack-invite.js
More file actions
279 lines (244 loc) · 14.5 KB
/
slack-invite.js
File metadata and controls
279 lines (244 loc) · 14.5 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var logo_url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAEJGlDQ1BJQ0MgUHJvZmlsZQAAOBGFVd9v21QUPolvUqQWPyBYR4eKxa9VU1u5GxqtxgZJk6XtShal6dgqJOQ6N4mpGwfb6baqT3uBNwb8AUDZAw9IPCENBmJ72fbAtElThyqqSUh76MQPISbtBVXhu3ZiJ1PEXPX6yznfOec7517bRD1fabWaGVWIlquunc8klZOnFpSeTYrSs9RLA9Sr6U4tkcvNEi7BFffO6+EdigjL7ZHu/k72I796i9zRiSJPwG4VHX0Z+AxRzNRrtksUvwf7+Gm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GRq4pyvKQII0lRcFDs1AQSZmjjna4oAgjQVF8VOTQBBpibO+ZoigCBNxUWxUxNYD9DphhmA3UymOM3qcKaTiVKzPRksJTUNQ2o2g5yYWDP1RRL5fIMgy+VlOhUKFw9P0rk8athmIsjZ2UW6lAq7+WI1iq2GwqjBNYFuPk/94oaM/VvdKAZBFvv76bn063T4wvPp4vSsCgBano4mPJAKl/fuDoPX9N6xuZFUUSVFeCOgciy//H/qj98ZWpO5mH1/8vBROj8/lzmZevlV9BVmT75jPXiQ+r//VSyRiY9Dfd4ioZ+qCMidoz8+TotXf5Nuvvm2Do6tbXRsVbgoJjIBUeN7Xxo/r8Bp4qQAAAAASUVORK5CYII=';
Function.prototype.stringify = function () {
var match = this.toString().match(/[^]*\/\*([^]*)\*\/\s*\}$/);
return match ? match[1] : '';
}
module.exports = function (ctx, req, res) {
// Validate webtask parameters
var required_params = ['SLACK_ORG', 'SLACK_TOKEN'];
for (var i = 0; i < required_params.length; i++) {
if (typeof ctx.secrets[required_params[i]] !== 'string')
return handle_error({
code: 400,
message: 'You must specify the `' + required_params[i]
+ '` parameter when creating the webtask using `wt create --secret '
+ required_params[i] + '={value}` argument.'
});
}
// Configure routing
if (req.method === 'GET')
return handle_get_invite();
else if (req.method === 'POST') {
if (typeof req.body.email !== 'string' || !req.body.email.trim().match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/i))
return handle_error({ code: 400, message: 'Missing or invalid e-mail address.'});
else
return handle_send_invite();
}
else
return handle_error({ code: 405 });
function handle_error(error) {
res.writeHead(200, { 'Content-Type': 'text/html' });
return res.end(require('ejs').render(oops.stringify(), { logo_url: logo_url, ctx: ctx, error: error }));
}
function handle_get_invite() {
res.writeHead(200, { 'Content-Type': 'text/html' });
return res.end(require('ejs').render(slack_invite.stringify(), { logo_url: logo_url, ctx: ctx }));
}
function handle_send_invite() {
require('superagent')
.post('https://' + ctx.secrets.SLACK_ORG + '.slack.com/api/users.admin.invite')
.type('form')
.send({
email: req.body.email.trim(),
token: ctx.secrets.SLACK_TOKEN,
set_active: true
})
.end(function (error, sres) {
if (error)
return handle_error({ code: 502, message: error.toString() });
if (sres.status !== 200)
return handle_error({ code: 502, message: 'Invalid response from Slack: ' + sres.status });
if (!sres.body.ok) {
if (sres.body.error === 'missing_scope' && sres.body.needed === 'admin')
return handle_error({ code: 400, message: 'The Slack token is missing `admin` scope required to invite users via Slack API. Use a token from Slack admin account.'})
else if (sres.body.error === 'already_invited')
return handle_error({ code: 400, message: 'You have been previously invited to this Slack team. Search for email from feedback@slack.com.' });
else if (sres.body.error === 'already_in_team')
return handle_error({ code: 400, message: 'You are already a member of this Slack team.'})
else
return handle_error({ code: 502, message: 'Response from Slack: ' + sres.body.error });
}
res.writeHead(200, { 'Content-Type': 'text/html' });
return res.end(require('ejs').render(invite_sent.stringify(), { logo_url: logo_url, ctx: ctx }));
});
}
}
function slack_invite() {/*
<html>
<head>
<title><%= ctx.secrets.SLACK_ORG %> signup</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,user-scalable=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<style>
body {
font-family: "Avenir Next",Avenir,"Helvetica Neue",Hevetica,sans-serif;
}
.header-image {
position: relative;
height: 235px;
width: 100%;
overflow: hidden;
margin-bottom: 70px;
}
.header-image .image{
background: transparent url("<%- ctx.secrets.BG_URL || ctx.secrets.LOGO_URL || logo_url %>") center center no-repeat;
background-size: cover;
-webkit-filter: blur(12px);
-moz-filter: blur(12px);
-o-filter: blur(12px);
-ms-filter: blur(12px);
filter: blur(12px);
position: absolute;
top: -20px;
right: -20px;
bottom: -20px;
left: -20px;
opacity: 0.9;
}
.user-logo {
width: 75px;
height: 75px;
border-radius: 50%;
box-shadow: 0px 2px 4px 0px #D0D2D3;
position: absolute;
top: 212px;
left: 49.5%;
}
.slack-logo {
width: 115px;
height: 115px;
border-radius: 50%;
box-shadow: 0px 2px 4px 0px #D0D2D3;
position: absolute;
top: 180px;
right: 48.5%;
}
p {
font-size: 20px;
margin: 15px 0 0;
}
.input-email {
max-width: 325px;
margin: 40px auto;
}
.button-invitation {
background: #E76D5F;
border-radius: 25px;
font-size: 16px;
height: 50px;
border: none;
color: white;
padding: 0 30px;
}
.button-invitation:hover {
background: #DD6557;
}
.footer {
position: relative;
overflow: hidden;
margin: 40px 0;
}
.separator-line {
height: 1px;
width: 100%;
border-top: 1px solid #E76D5F;
position: absolute;
top: 50%;
left: 0;
}
.footer-copy {
background: white;
}
.webtask-copy a {
color: #333;
}
.webtask-copy a:hover {
text-decoration: none
}
.create-copy {
font-size: 16px;
}
.create-copy a {
color: #E76D5F;
}
.create-copy a:hover {
color: #DD6557;
}
</style>
</head>
<body>
<header>
<div class="header-image">
<div class="image"></div>
</div>
<img src="https://cdn.auth0.com/website/webtask/assets/slack-webtask-logo.png" alt="Slack" class="slack-logo">
<img class="user-logo" src="<%- ctx.secrets.LOGO_URL || logo_url %>" width="100" heigth="100">
</header>
<div class="container">
<div class=" col-md-6 col-md-offset-3 text-center">
<h2>Join <span><%= ctx.secrets.SLACK_ORG %></span> on Slack</h2>
<p>Enter your e-mail below to receive an invitation:</p>
<form method="POST">
<div class="form-group">
<input type="email" class="form-control input-email" name="email" placeholder="foo@example.com" required>
</div>
<button type="submit" class="button-invitation">Get invitation</button>
</form>
</div>
</div>
<div class="footer">
<div class="separator-line"></div>
<div class="col-md-4 col-md-offset-4 text-center footer-copy">
<p class="webtask-copy">Powered by <a href="https://webtask.io"><img src="https://webtask.io/images/symbol.svg" alt="Webtasks" width="30px" height="30px"> Auth0 Webtasks</a></p>
<p class="create-copy">Create your own <a href="https://github.com/auth0/webtask-slack-signup">Slack invite</a></p>
</div>
</div>
</body>
</html>
*/}
function invite_sent() {/*
<html>
<head>
<title><%= ctx.secrets.SLACK_ORG %> signup</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,user-scalable=no">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
</head>
<body style="padding-top: 30px;">
<div class="container">
<div class="jumbotron col-md-6 col-md-offset-3 text-center">
<img src="<%- ctx.secrets.LOGO_URL || logo_url %>" width="100" heigth="100">
<h2>Invite to <strong><%= ctx.secrets.SLACK_ORG %></strong> is on its way!</h2>
<p><small>Check for e-mail from feedback@slack.com</small></p>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3 text-center">
<p>Powered by <a href="https://webtask.io">Auth0 Webtasks</a> | Create <a href="https://tomasz.janczuk.org/2016/02/create-slack-signup-page-with-webtasks.html">your own Slack invite</a></p>
</div>
</div>
</div>
</body>
</html>
*/}
function oops() {/*
<html>
<head>
<title><%= ctx.secrets.SLACK_ORG %> signup</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,user-scalable=no">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
</head>
<body style="padding-top: 30px;">
<div class="container">
<div class="jumbotron col-md-6 col-md-offset-3 text-center">
<img src="<%- ctx.secrets.LOGO_URL || logo_url %>" width="100" heigth="100">
<h2>Something is not quite right...</h2>
<p><%= error.message %></p>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3 text-center">
<p>Powered by <a href="https://webtask.io">Auth0 Webtasks</a> | Create <a href="https://tomasz.janczuk.org/2016/02/create-slack-signup-page-with-webtasks.html">your own Slack invite</a></p>
</div>
</div>
</div>
</body>
</html>
*/}