Skip to content

Commit 68bbecf

Browse files
authored
Migrate to charon (#315)
* migrate to charon * add headers
1 parent f306870 commit 68bbecf

2 files changed

Lines changed: 65 additions & 38 deletions

File tree

util/invite-user.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,53 @@ const { defaultInvite } = require('./invite-types/default')
33
const { onboardInvite } = require('./invite-types/onboard')
44
const { metrics } = require('./metrics')
55

6-
async function inviteGuestToSlack({ email, channels, _customMessage }) {
6+
async function inviteGuestToSlack({ email, ip, channels, _customMessage }) {
77
// This is an undocumented API method found in https://github.com/ErikKalkoken/slackApiDoc/pull/70
88
// Unlike the documention in that PR, we're driving it not with a legacy token but a browser storage+cookie pair
99

1010
// The SLACK_COOKIE is a xoxd-* token found in browser cookies under the key 'd'
1111
// The SLACK_BROWSER_TOKEN is a xoxc-* token found in browser local storage using this script: https://gist.github.com/maxwofford/5779ea072a5485ae3b324f03bc5738e1
1212

1313
// I haven't yet found out how to add custom messages, so those are ignored for now
14-
const cookieValue = `d=${process.env.SLACK_COOKIE}`
14+
// const cookieValue = `d=${process.env.SLACK_COOKIE}`
1515

16-
// Create a new Headers object
17-
const headers = new Headers()
16+
// // Create a new Headers object
17+
// const headers = new Headers()
18+
19+
// // Add the cookie to the headers
20+
// headers.append('Cookie', cookieValue)
21+
// headers.append('Content-Type', 'application/json')
22+
// headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)
23+
// const data = JSON.stringify({
24+
// token: process.env.SLACK_BROWSER_TOKEN,
25+
// invites: [
26+
// {
27+
// email,
28+
// type: 'restricted',
29+
// mode: 'manual',
30+
// },
31+
// ],
32+
// restricted: true,
33+
// channels: channels.join(','),
34+
// })
1835

19-
// Add the cookie to the headers
20-
headers.append('Cookie', cookieValue)
36+
// const res = await fetch(`https://slack.com/api/users.admin.inviteBulk`, {
37+
// headers,
38+
// method: 'POST',
39+
// body: data,
40+
// })
41+
42+
const headers = new Headers()
2143
headers.append('Content-Type', 'application/json')
22-
headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)
44+
headers.append('Authorization', `Bearer ${process.env.CHARON_API_KEY}`)
45+
2346
const data = JSON.stringify({
24-
token: process.env.SLACK_BROWSER_TOKEN,
25-
invites: [
26-
{
27-
email,
28-
type: 'restricted',
29-
mode: 'manual',
30-
},
31-
],
32-
restricted: true,
33-
channels: channels.join(','),
47+
email,
48+
ip
3449
})
35-
36-
const res = await fetch(`https://slack.com/api/users.admin.inviteBulk`, {
50+
const res = await fetch("https://charon.hackclub.com/user/invite", {
3751
headers,
38-
method: 'POST',
52+
method: "POST",
3953
body: data,
4054
})
4155
metrics.increment('events.flow.invitetoslack', 1)
@@ -71,7 +85,7 @@ async function inviteUser({
7185
}
7286
const { channels, customMessage } = invite
7387

74-
return await inviteGuestToSlack({ email, channels, customMessage })
88+
return await inviteGuestToSlack({ email, ip, channels, customMessage })
7589
}
7690

7791
module.exports = { inviteUser }

util/upgrade-user.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,41 @@ async function upgradeUser(user) {
2424
// The SLACK_COOKIE is a xoxd-* token found in browser cookies under the key 'd'
2525
// The SLACK_BROWSER_TOKEN is a xoxc-* token found in browser local storage using this script: https://gist.github.com/maxwofford/5779ea072a5485ae3b324f03bc5738e1
2626

27-
const cookieValue = `d=${process.env.SLACK_COOKIE}`
27+
// const cookieValue = `d=${process.env.SLACK_COOKIE}`
2828

29-
// Create a new Headers object
30-
const headers = new Headers()
29+
// // Create a new Headers object
30+
// const headers = new Headers()
3131

3232
// Add the cookie to the headers
33-
headers.append('Cookie', cookieValue)
34-
headers.append('Content-Type', 'application/json')
35-
headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)
33+
// headers.append('Cookie', cookieValue)
34+
// headers.append('Content-Type', 'application/json')
35+
// headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)
3636

37-
const form = JSON.stringify({
38-
user,
39-
team_id,
37+
// const form = JSON.stringify({
38+
// user,
39+
// team_id,
40+
// })
41+
// return await fetch(
42+
// `https://slack.com/api/users.admin.setRegular?slack_route=${team_id}&user=${user}`,
43+
// {
44+
// headers,
45+
// method: 'POST',
46+
// body: form,
47+
// }
48+
// )
49+
50+
const headers = new Headers()
51+
headers.append('Content-Type', 'application/json')
52+
headers.append('Authorization', `Bearer ${process.env.CHARON_API_KEY}`)
53+
54+
const data = JSON.stringify({
55+
id: user
56+
})
57+
return await fetch("https://charon.hackclub.com/user/promote", {
58+
headers,
59+
method: "POST",
60+
body: data,
4061
})
41-
return await fetch(
42-
`https://slack.com/api/users.admin.setRegular?slack_route=${team_id}&user=${user}`,
43-
{
44-
headers,
45-
method: 'POST',
46-
body: form,
47-
}
48-
)
4962
.then((r) => {
5063
r.json()
5164
metrics.increment('events.flow.user_upgrade', 1)

0 commit comments

Comments
 (0)