1- const Koa = require ( " koa" ) ;
2- const Router = require ( " @koa/router" ) ;
3- const { WebClient } = require ( " @slack/web-api" ) ; // requires v6.4 or higher
4- const jwt = require ( " jsonwebtoken" ) ;
5- const uuid = require ( " uuid" ) ;
1+ const Koa = require ( ' koa' ) ;
2+ const Router = require ( ' @koa/router' ) ;
3+ const { WebClient } = require ( ' @slack/web-api' ) ; // requires v6.4 or higher
4+ const jwt = require ( ' jsonwebtoken' ) ;
5+ const uuid = require ( ' uuid' ) ;
66
77const app = new Koa ( ) ;
88const router = new Router ( ) ;
99
10- router . get ( "/" , async ( ctx ) => {
11- ctx . redirect ( " /slack/install" ) ;
10+ router . get ( '/' , async ( ctx ) => {
11+ ctx . redirect ( ' /slack/install' ) ;
1212} ) ;
1313
1414const clientId = process . env . SLACK_CLIENT_ID ;
1515const clientSecret = process . env . SLACK_CLIENT_SECRET ;
16- const oidcScopes = " openid,email,profile" ; // openid is required at least
16+ const oidcScopes = ' openid,email,profile' ; // openid is required at least
1717const redirectUri = process . env . SLACK_REDIRECT_URI ;
1818
1919class MyStateStore {
@@ -36,14 +36,14 @@ class MyStateStore {
3636}
3737const myStateStore = new MyStateStore ( ) ;
3838
39- router . get ( " /slack/install" , async ( ctx ) => {
39+ router . get ( ' /slack/install' , async ( ctx ) => {
4040 const state = await myStateStore . generate ( ) ;
4141 // (optional) you can pass nonce parameter as well
4242 // refer to https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest for details
43- const nonce = " your-own-nonce-value" ;
43+ const nonce = ' your-own-nonce-value' ;
4444 const url = `https://slack.com/openid/connect/authorize?response_type=code&state=${ state } &client_id=${ clientId } &scope=${ oidcScopes } &redirect_uri=${ redirectUri } &nonce=${ nonce } ` ;
4545
46- ctx . headers [ " content-type" ] = " text/html; charset=utf-8" ;
46+ ctx . headers [ ' content-type' ] = ' text/html; charset=utf-8' ;
4747 ctx . body = `<html>
4848 <head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
4949 <body>
@@ -55,10 +55,10 @@ router.get("/slack/install", async (ctx) => {
5555
5656const client = new WebClient ( ) ;
5757
58- router . get ( " /slack/oauth_redirect" , async ( ctx ) => {
58+ router . get ( ' /slack/oauth_redirect' , async ( ctx ) => {
5959 if ( ! ( await myStateStore . validate ( ctx . query . state ) ) ) {
6060 ctx . status = 400 ;
61- ctx . headers [ " content-type" ] = " text/html; charset=utf-8" ;
61+ ctx . headers [ ' content-type' ] = ' text/html; charset=utf-8' ;
6262 ctx . body = `<html>
6363 <head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
6464 <body>
@@ -72,12 +72,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
7272 const token = await client . openid . connect . token ( {
7373 client_id : clientId ,
7474 client_secret : clientSecret ,
75- grant_type : " authorization_code" ,
75+ grant_type : ' authorization_code' ,
7676 code : ctx . query . code ,
7777 } ) ;
78- console . log (
79- `openid.connect.token response: ${ JSON . stringify ( token , null , 2 ) } `
80- ) ;
78+ console . log ( `openid.connect.token response: ${ JSON . stringify ( token , null , 2 ) } ` ) ;
8179
8280 let userAccessToken = token . access_token ;
8381
@@ -88,16 +86,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
8886 const refreshedToken = await client . openid . connect . token ( {
8987 client_id : clientId ,
9088 client_secret : clientSecret ,
91- grant_type : " refresh_token" ,
89+ grant_type : ' refresh_token' ,
9290 refresh_token : token . refresh_token ,
9391 } ) ;
94- console . log (
95- `openid.connect.token (refresh) response: ${ JSON . stringify (
96- refreshedToken ,
97- null ,
98- 2
99- ) } `
100- ) ;
92+ console . log ( `openid.connect.token (refresh) response: ${ JSON . stringify ( refreshedToken , null , 2 ) } ` ) ;
10193 userAccessToken = refreshedToken . access_token ;
10294 }
10395
@@ -111,11 +103,9 @@ router.get("/slack/oauth_redirect", async (ctx) => {
111103 // You don't need to do this here.
112104 const tokenWiredClient = new WebClient ( userAccessToken ) ;
113105 const userInfo = await tokenWiredClient . openid . connect . userInfo ( ) ;
114- console . log (
115- `openid.connect.userInfo response: ${ JSON . stringify ( userInfo , null , 2 ) } `
116- ) ;
106+ console . log ( `openid.connect.userInfo response: ${ JSON . stringify ( userInfo , null , 2 ) } ` ) ;
117107
118- ctx . headers [ " content-type" ] = " text/html; charset=utf-8" ;
108+ ctx . headers [ ' content-type' ] = ' text/html; charset=utf-8' ;
119109 ctx . body = `<html>
120110 <head><style>body h2 {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
121111 <body>
@@ -130,4 +120,6 @@ router.get("/slack/oauth_redirect", async (ctx) => {
130120// Enable the routes
131121app . use ( router . routes ( ) ) . use ( router . allowedMethods ( ) ) ;
132122// Start the web app, which is available at http://localhost:3000/slack/*
133- app . listen ( 3000 ) ;
123+ app . listen ( 3000 , ( ) => {
124+ console . log ( 'app started' ) ;
125+ } ) ;
0 commit comments