Skip to content

Commit

Permalink
chore: updates to example apps (#2007)
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj authored Sep 5, 2024
1 parent d9c9a66 commit e930db3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 78 deletions.
46 changes: 19 additions & 27 deletions examples/legacy-sign-in-with-slack/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// https://api.slack.com/legacy/sign-in-with-slack
const Koa = require("koa");
const Router = require("@koa/router");
const { WebClient } = require("@slack/web-api");
const uuid = require("uuid");
const Koa = require('koa');
const Router = require('@koa/router');
const { WebClient } = require('@slack/web-api');
const uuid = require('uuid');

const app = new Koa();
const router = new Router();

router.get("/", async (ctx) => {
ctx.redirect("/slack/install");
router.get('/', async (ctx) => {
ctx.redirect('/slack/install');
});

const clientId = process.env.SLACK_CLIENT_ID;
const clientSecret = process.env.SLACK_CLIENT_SECRET;
const legacyIdentityScopes = "identity.basic,identity.email"; // identity.basic is required at least
const legacyIdentityScopes = 'identity.basic,identity.email'; // identity.basic is required at least

class MyStateStore {
constructor() {
Expand All @@ -35,11 +35,11 @@ class MyStateStore {
}
const myStateStore = new MyStateStore();

router.get("/slack/install", async (ctx) => {
router.get('/slack/install', async (ctx) => {
const state = await myStateStore.generate();
const url = `https://slack.com/oauth/v2/authorize?state=${state}&client_id=${clientId}&user_scope=${legacyIdentityScopes}`;

ctx.headers["content-type"] = "text/html; charset=utf-8";
ctx.headers['content-type'] = 'text/html; charset=utf-8';
ctx.body = `<html>
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
<body>
Expand All @@ -51,10 +51,10 @@ router.get("/slack/install", async (ctx) => {

const client = new WebClient();

router.get("/slack/oauth_redirect", async (ctx) => {
router.get('/slack/oauth_redirect', async (ctx) => {
if (!(await myStateStore.validate(ctx.query.state))) {
ctx.status = 400;
ctx.headers["content-type"] = "text/html; charset=utf-8";
ctx.headers['content-type'] = 'text/html; charset=utf-8';
ctx.body = `<html>
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
<body>
Expand All @@ -70,9 +70,7 @@ router.get("/slack/oauth_redirect", async (ctx) => {
client_secret: clientSecret,
code: ctx.query.code,
});
console.log(
`oauth.v2.access response: ${JSON.stringify(token, null, 2)}`
);
console.log(`oauth.v2.access response: ${JSON.stringify(token, null, 2)}`);

let userAccessToken = token.authed_user.access_token;

Expand All @@ -83,16 +81,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
const refreshedToken = await client.oauth.v2.access({
client_id: clientId,
client_secret: clientSecret,
grant_type: "refresh_token",
grant_type: 'refresh_token',
refresh_token: token.refresh_token,
});
console.log(
`oauth.v2.access (refresh) response: ${JSON.stringify(
refreshedToken,
null,
2
)}`
);
console.log(`oauth.v2.access (refresh) response: ${JSON.stringify(refreshedToken, null, 2)}`);
userAccessToken = refreshedToken.access_token;
}

Expand All @@ -103,11 +95,9 @@ router.get("/slack/oauth_redirect", async (ctx) => {
// You don't need to do this here.
const tokenWiredClient = new WebClient(userAccessToken);
const userInfo = await tokenWiredClient.users.identity();
console.log(
`users.identity response: ${JSON.stringify(userInfo, null, 2)}`
);
console.log(`users.identity response: ${JSON.stringify(userInfo, null, 2)}`);

ctx.headers["content-type"] = "text/html; charset=utf-8";
ctx.headers['content-type'] = 'text/html; charset=utf-8';
ctx.body = `<html>
<head><style>body h2 {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
<body>
Expand All @@ -120,4 +110,6 @@ router.get("/slack/oauth_redirect", async (ctx) => {
// Enable the routes
app.use(router.routes()).use(router.allowedMethods());
// Start the web app, which is available at http://localhost:3000/slack/*
app.listen(3000);
app.listen(3000, () => {
console.log('app started');
});
1 change: 1 addition & 0 deletions examples/legacy-sign-in-with-slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"repository": "slackapi/node-slack-sdk",
"dependencies": {
"@koa/router": "^13.0.0",
"@slack/bolt": "^3.21.2",
"koa": "^2.13.1",
"uuid": "^10.0.0"
}
Expand Down
57 changes: 36 additions & 21 deletions examples/oauth-v2/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET, {
// Set path to receive events
app.use('/slack/events', slackEvents.requestListener());

const scopes = ['app_mentions:read', 'channels:read', 'groups:read', 'channels:manage', 'chat:write', 'incoming-webhook'];
const scopes = [
'app_mentions:read',
'channels:read',
'groups:read',
'channels:manage',
'chat:write',
'incoming-webhook',
];
const userScopes = ['chat:write'];

const installer = new InstallProvider({
Expand All @@ -27,14 +34,19 @@ const installer = new InstallProvider({
logLevel: LogLevel.DEBUG,
});

app.get('/', (req, res) => res.send('go to /slack/install'));
app.get('/', (_req, res) => res.send('go to /slack/install'));

app.get('/slack/install', async (req, res, next) => {
await installer.handleInstallPath(req, res, {}, {
scopes,
userScopes,
metadata: 'some_metadata',
});
app.get('/slack/install', async (req, res, _next) => {
await installer.handleInstallPath(
req,
res,
{},
{
scopes,
userScopes,
metadata: 'some_metadata',
},
);
});

// This works since @slack/[email protected] or newer
Expand Down Expand Up @@ -120,24 +132,27 @@ slackEvents.on('app_home_opened', async (event, body) => {
const web = new WebClient(DBInstallData.botToken);
await web.views.publish({
user_id: event.user,
view: {
"type":"home",
"blocks":[
view: {
type: 'home',
blocks: [
{
"type": "section",
"block_id": "section678",
"text": {
"type": "mrkdwn",
"text": "Welcome to the App Home!"
type: 'section',
block_id: 'section678',
text: {
type: 'mrkdwn',
text: 'Welcome to the App Home!',
},
}
]
},
],
},
});
}
}
catch (error) {
} catch (error) {
console.error(error);
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}! Go to http://localhost:3000/slack/install to initiate oauth flow`))
app.listen(port, () =>
console.log(
`Example app listening on port ${port}! Go to http://localhost:3000/slack/install to initiate oauth flow`,
),
);
52 changes: 22 additions & 30 deletions examples/openid-connect/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const Koa = require("koa");
const Router = require("@koa/router");
const { WebClient } = require("@slack/web-api"); // requires v6.4 or higher
const jwt = require("jsonwebtoken");
const uuid = require("uuid");
const Koa = require('koa');
const Router = require('@koa/router');
const { WebClient } = require('@slack/web-api'); // requires v6.4 or higher
const jwt = require('jsonwebtoken');
const uuid = require('uuid');

const app = new Koa();
const router = new Router();

router.get("/", async (ctx) => {
ctx.redirect("/slack/install");
router.get('/', async (ctx) => {
ctx.redirect('/slack/install');
});

const clientId = process.env.SLACK_CLIENT_ID;
const clientSecret = process.env.SLACK_CLIENT_SECRET;
const oidcScopes = "openid,email,profile"; // openid is required at least
const oidcScopes = 'openid,email,profile'; // openid is required at least
const redirectUri = process.env.SLACK_REDIRECT_URI;

class MyStateStore {
Expand All @@ -36,14 +36,14 @@ class MyStateStore {
}
const myStateStore = new MyStateStore();

router.get("/slack/install", async (ctx) => {
router.get('/slack/install', async (ctx) => {
const state = await myStateStore.generate();
// (optional) you can pass nonce parameter as well
// refer to https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest for details
const nonce = "your-own-nonce-value";
const nonce = 'your-own-nonce-value';
const url = `https://slack.com/openid/connect/authorize?response_type=code&state=${state}&client_id=${clientId}&scope=${oidcScopes}&redirect_uri=${redirectUri}&nonce=${nonce}`;

ctx.headers["content-type"] = "text/html; charset=utf-8";
ctx.headers['content-type'] = 'text/html; charset=utf-8';
ctx.body = `<html>
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
<body>
Expand All @@ -55,10 +55,10 @@ router.get("/slack/install", async (ctx) => {

const client = new WebClient();

router.get("/slack/oauth_redirect", async (ctx) => {
router.get('/slack/oauth_redirect', async (ctx) => {
if (!(await myStateStore.validate(ctx.query.state))) {
ctx.status = 400;
ctx.headers["content-type"] = "text/html; charset=utf-8";
ctx.headers['content-type'] = 'text/html; charset=utf-8';
ctx.body = `<html>
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
<body>
Expand All @@ -72,12 +72,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
const token = await client.openid.connect.token({
client_id: clientId,
client_secret: clientSecret,
grant_type: "authorization_code",
grant_type: 'authorization_code',
code: ctx.query.code,
});
console.log(
`openid.connect.token response: ${JSON.stringify(token, null, 2)}`
);
console.log(`openid.connect.token response: ${JSON.stringify(token, null, 2)}`);

let userAccessToken = token.access_token;

Expand All @@ -88,16 +86,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
const refreshedToken = await client.openid.connect.token({
client_id: clientId,
client_secret: clientSecret,
grant_type: "refresh_token",
grant_type: 'refresh_token',
refresh_token: token.refresh_token,
});
console.log(
`openid.connect.token (refresh) response: ${JSON.stringify(
refreshedToken,
null,
2
)}`
);
console.log(`openid.connect.token (refresh) response: ${JSON.stringify(refreshedToken, null, 2)}`);
userAccessToken = refreshedToken.access_token;
}

Expand All @@ -111,11 +103,9 @@ router.get("/slack/oauth_redirect", async (ctx) => {
// You don't need to do this here.
const tokenWiredClient = new WebClient(userAccessToken);
const userInfo = await tokenWiredClient.openid.connect.userInfo();
console.log(
`openid.connect.userInfo response: ${JSON.stringify(userInfo, null, 2)}`
);
console.log(`openid.connect.userInfo response: ${JSON.stringify(userInfo, null, 2)}`);

ctx.headers["content-type"] = "text/html; charset=utf-8";
ctx.headers['content-type'] = 'text/html; charset=utf-8';
ctx.body = `<html>
<head><style>body h2 {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
<body>
Expand All @@ -130,4 +120,6 @@ router.get("/slack/oauth_redirect", async (ctx) => {
// Enable the routes
app.use(router.routes()).use(router.allowedMethods());
// Start the web app, which is available at http://localhost:3000/slack/*
app.listen(3000);
app.listen(3000, () => {
console.log('app started');
});

0 comments on commit e930db3

Please sign in to comment.