slack_oauth illustration

Slack Deno Oauth

Setup the OAuth flow for Slack apps easily. Deno port of @slack/oauth

build status language code size issues license version

View on deno.land




Note on Compatiblity

This is module is mostly compatible with its node counterpart. However, InstallProvider does not contain a handleCallback function taking request and response handlers. Instead, it has a handle function which takes a code and a state and does not handle responding and returns a promise which resolves / rejects to the default HTML for successes / errors, which you can then respond with or override. This is to maximize compatiblity with various Deno HTTP servers.

Usage

Handling requests

The following is an example of handling a request using Oak as the HTTP server

import "https://deno.land/x/dotenv@v2.0.0/load.ts"

import { nanoid } from "https://deno.land/x/nanoid@v3.0.1/mod.ts"
import { Application, Router } from "https://deno.land/x/oak/mod.ts"
import { InstallProvider } from "https://deno.land/x/slack_oauth@3.0.1/mod.ts"

// initialize the installProvider
const installer = new InstallProvider({
    clientId: Deno.env.get("SLACK_CLIENT_ID")!,
    clientSecret: Deno.env.get("SLACK_CLIENT_SECRET")!,
    stateSecret: nanoid(),
})

const router = new Router()
router.get("/", async (ctx) => {
    ctx.response.redirect(
        await installer.generateInstallUrl({
            scopes: ["chat:write"],
        }),
    )
})

router.get("/slack/oauth_redirect", async (ctx) => {
    const code = ctx.request.url.searchParams.get("code")
    const state = ctx.request.url.searchParams.get("state")

    if (!code || !state) {
        ctx.response.status = 401
        ctx.response.body = "Unauthorized - Missing code or state"
        return
    }

    try {
        const successMessage = await installer.handle(code, state)
        ctx.response.status = 200
        ctx.response.body = successMessage
    } catch (e) {
        ctx.response.status = 500
        ctx.response.body = e.message
    }
})

const app = new Application()
app.use(router.routes())
app.use(router.allowedMethods())
await app.listen({ port: 8000 })

Authorization

You can use the the installer.authorize() function to fetch data that has been saved in your installation store.

import { InstallProvider } from "https://deno.land/x/slack_oauth@3.0.1/mod.ts"
const installer = new InstallProvider({
    clientId: Deno.env.get("SLACK_CLIENT_ID")!,
    clientSecret: Deno.env.get("SLACK_CLIENT_SECRET")!,
    stateSecret: nanoid(),
})

const result = await installer.authorize({ teamId: "my-team-ID" })

console.log(result)

API

Supporters

Stargazers repo roster for @slack-deno/oauth

Forkers repo roster for @slack-deno/oauth