-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_command.py
276 lines (256 loc) · 9.68 KB
/
slack_command.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
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
import re
import validators
from flask import Blueprint, abort, jsonify, request
from core import SlashpassError
from environ import CONFIGURATION_GUIDE_URL, SLACK_SERVER
from server import Team, cmd, db
from utils import error, success, valid_slack_request, warning
view = Blueprint("slack_command", __name__)
@view.route("", methods=["POST"])
def api():
data = request.values.to_dict()
try:
command = re.split("\s+", data["text"])
team_id = data["team_id"]
team_domain = data["team_domain"]
channel = data["channel_id"]
except KeyError:
abort(400)
# ensuring that the request comes from slack
if not valid_slack_request(request):
return abort(403)
team = db.session.query(Team).filter_by(team_id=team_id).first()
if not team:
return error(
"You are not registered in our proxy server, try removig the app "
"and adding it to slack again."
)
if command[0] == "help":
fields = [
{
"title": "`/pass` _or_ `/pass list`",
"value": "List the available passwords in the channel.",
"short": True,
},
{
"title": "`/pass <secret>` _or_ `/pass show <secret>`",
"value": (
"Displays a one-time-use link containing the secret content. "
"This link expires in 15 minutes."
),
"short": True,
},
{
"title": "`/pass insert <secret>`",
"value": (
"Displays a link to the editor for creating a new secret. "
"This link expires in 15 minutes."
),
"short": True,
},
{
"title": "`/pass remove <secret>`",
"value": ("Deletes the secret from the channel."),
"short": True,
},
{
"title": "`/pass configure <private_server_url>` _or_ `/pass configure`",
"value": (
"Configures the password server. "
"This only needs to be executed once."
),
"short": True,
},
{
"title": "`/pass help`",
"value": "Displays this dialog :robot_face:",
"short": True,
},
]
return jsonify(
{
"attachments": [
{
"fallback": ("_Usage:_ https://slashpass.co/configure"),
"text": "*_Usage:_*",
"fields": fields,
}
]
}
)
if command[0] == "configure" and len(command) == 2:
url = command[1]
if not validators.url(url):
return error("Invalid URL format, use: https://<domain>")
if team.url:
msg = (
"You already have a password server configured, "
"do you want to replace the current server?"
)
return jsonify(
{
"attachments": [
{
"fallback": "You already have a password server configured",
"text": msg,
"callback_id": "configure_password_server",
"color": "warning",
"actions": [
{
"name": "reconfigure_server",
"text": "Yes",
"type": "button",
"value": url,
},
{
"name": "no_reconfigure",
"text": "No",
"style": "danger",
"type": "button",
"value": "no",
},
],
}
]
}
)
if not team.register_server(url):
return error("Unable to retrieve the _public_key_ from the server")
return success(f"{team_domain} team successfully configured!")
if command[0] == "configure" and len(command) == 1 or not team.url:
color = "warning"
if team.url:
msg = (
f"*{team.team_name}* team already have a server configured, if you want to "
f"swap select some of the options below"
)
elif command[0] == "configure":
color = "good"
msg = "What type of server do you want to use?"
else:
msg = (
f"*{team_domain}* team does not have a slashpass server configured, select "
f"one of the options below to start."
)
warning_msg = (
"You are choosing the TEST server. Any information stored on this server "
"may be deleted at any time without prior notice."
)
return jsonify(
{
"attachments": [
{
"fallback": msg,
"text": msg,
"color": color,
"callback_id": "configure_password_server",
"actions": [
{
"name": "use_demo_server",
"text": "Use Test Server",
"type": "button",
"value": "no",
"confirm": {
"title": "Confirm",
"text": warning_msg,
"ok_text": "I understand",
"dismiss_text": "No",
},
},
{
"text": "Configure Private Server",
"type": "button",
"url": CONFIGURATION_GUIDE_URL,
},
{
"name": "no_configure",
"text": "Later",
"type": "button",
"value": "no",
},
],
}
]
}
)
if command[0] in ["", "list"]:
try:
dir_ls = cmd.list(team, channel)
except SlashpassError as e:
return error(f"_{e.message}_")
if not dir_ls:
return warning(
"You have not created any passwords for this channel, use "
"`/pass insert <secret>` to create the first one!"
)
return jsonify(
{
"attachments": [
{
"fallback": dir_ls,
"text": f"Password Store\n{dir_ls}",
"footer": (
"Use the command `/pass <key_name>` to retrieve "
"some of the keys"
),
}
]
}
)
if command[0] == "insert" and len(command) == 2:
app = command[1]
token = cmd.generate_insert_token(team, channel, app)
msg = f"Adding password for *{app}* in this channel"
return jsonify(
{
"attachments": [
{
"fallback": msg,
"text": msg,
"footer": "This editor will be valid for 15 minutes",
"color": "good",
"actions": [
{
"text": "Open editor",
"style": "primary",
"type": "button",
"url": f"{SLACK_SERVER}/insert/{token}",
}
],
}
]
}
)
if command[0] == "remove" and len(command) == 2:
app = command[1]
if cmd.remove(team, channel, app):
return success(f"The secret *{app}* was removed successfully.")
return warning(
f"Looks like the secret *{app}* is not in your repository "
f":thinking_face: use the command `/pass list` "
f"to verify your storage."
)
app = command[1] if command[0] == "show" and len(command) == 2 else command[0]
if onetime_link := cmd.show(team, channel, app):
return jsonify(
{
"attachments": [
{
"fallback": f"Password: {onetime_link}",
"text": f"Password for *{app}*",
"footer": "This secret will be valid for 15 minutes",
"color": "good",
"actions": [
{
"text": "Open secret",
"style": "primary",
"type": "button",
"url": onetime_link,
}
],
}
]
}
)
else:
return warning(f"*{app}* is not in the password store.")