-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OAuth support to bolt #478
Comments
The code example in the PR(#479) is not complete: const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET
stateSecret: 'my-state-secret',
scopes: ['channels:read', 'groups:read', 'channels:manage', 'chat:write', 'incoming-webhook'],
installationStore: {
storeInstallation: (installation) => {
// change the line below so it saves to your database
return database.set(installation.team.id, installation);
},
fetchInstallation: (InstallQuery) => {
// change the line below so it fetches from your database
return database.get(InstallQuery.teamId);
},
},
}); It is missing the
What do you think about adding the needed param for the code example? Something like: const authorizeFn = async (installation) => {
return {
botToken: process.env.SLACK_BOT_TOKEN,
botId: "xxx",
botUserId: "xxx",
};
};
const app = new App({
authorize: authorizeFn,
signingSecret: process.env.SLACK_SIGNING_SECRET,
//...
}); |
hey @junjizhi, When using the built in OAuth, you don't provide Let me know if that fixes it for you! |
@stevengill You are right. The default |
Would this work locally in combination with ngrok? I followed the example in the docs but can't get it to work using any URL combination. The worst part is that I'm not getting any errors thrown beside 404's. |
@realappie it should work fine with ngrok. In your App config page on api.slack.com, navigate to Once your app is running, go to |
@stevengill Do you have a fully working example of this anywhere? I pretty much wrote everything in the documentation regarding oauth here. Without any success :/ I have also tried the install link without any success. I also currently have a working setup with a custom express route, but would love to switch over to the built-in implementation. My custom implementationThis is perhaps useful to share for anyone that can't get the built-in implementation to work either.export const createSlackAuthServer = (slackExpressReceiver: ExpressReceiver, slackApp: App, firestore: FirebaseFirestore.Firestore) => {
const app_express = slackExpressReceiver.app;
/* oauth callback function */
app_express.get('/auth', async (req, res, next) => {
const code: string = req.query['code'] as string;
if (isNil(code)) {
res.status(500).send('No code query param found');
}
try {
const result = await slackApp.client.oauth.v2.access({
client_id: config.slack.client_id,
client_secret: config.slack.client_secret,
code: code
});
const teamId: string = get(result, 'team.id') as string;
const accessToken = get(result, 'access_token') as string;
const authTestResponse: unknown = await slackApp.client.auth.test({
token: accessToken
});
const authTestResponseTyped = authTestResponse as SlackAuthTestResponse;
const installationDocument: SlackInstallationDocument = {
botId: authTestResponseTyped.bot_id,
teamId,
botUserId: get(result, 'bot_user_id') as string,
botToken: accessToken,
user_id: authTestResponseTyped.user_id,
};
const installingUserId = get(result, 'authed_user.id') as string;
const enterpriseId = get(result, 'enterprise.id') as string;
if (enterpriseId) {
installationDocument.enterprise_id = enterpriseId;
}
await firestore.collection('installations').doc(teamId).set(installationDocument);
// Open up the conversation in slack upon success
//
res.redirect(`https://slack.com/app_redirect?app=${config.slack.app_id}`)
await slackApp.client.chat.postMessage({
token: accessToken,
channel: installingUserId,
text: '',
blocks: [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Hi there! Welcome to the bitrise bot. To start using this bot please set a bitrise access token first.`
}
},
slackBlockCreator.generateSetBitriseTokenModalActionBlock()
]
});
console.error(`[slack-auth] installation successful for teamId="${teamId}" enterpriseId="${enterpriseId}"`);
} catch (error) {
console.error('[slack-auth] error calling client.oauth.v2.access', error);
res.status(500).send('Error authenticating');
}
});
} And wherever you're creating a new slack const slackExpressReceiver = new ExpressReceiver({
signingSecret: config.slack.signing_secret,
endpoints: '/events'
});
const slackApp = new App({
receiver: slackExpressReceiver,
authorize: async ({ teamId }) => {
const documentSnapshot = await firestore.collection('installations').doc(teamId).get();
if ( !documentSnapshot.exists ) {
throw new Error(`no matching installations for team="${teamId}"`);
} else {
console.log(`[slackApp:authorize] matching installations found for teamId="${teamId}"`)
}
const installation = documentSnapshot.data() as SlackInstallationDocument;
return installation;
}
});
createSlackAuthServer(slackExpressReceiver, slackApp, firestore); |
@realappie
As mentioned in the Bolt JS document, you need to pass at least If you have a reason to manually initialize |
@realappie see my answer at #583 (comment) |
Description
Let's add OAuth support to bolt!
It can use the new OAuth package we've been developing at slackapi/node-slack-sdk#963.
The idea being, when you are initializing your app, by passing in a few new options, bolt would automatically setup a route for direct install and a route for oauth redirect. It would also take care of creating a state param for you and exchanging that + the
code
for an access token. Lastly, it will provide an interface to plug your own database solution to store and retrieve access tokens and other installation related info.What type of issue is this? (place an
x
in one of the[ ]
)Requirements (place an
x
in each of the[ ]
)The text was updated successfully, but these errors were encountered: