Skip to content

Commit ac14e65

Browse files
committed
feat(slack): refactor state decoding logic for custom bot handling
1 parent 80ebc4e commit ac14e65

3 files changed

Lines changed: 30 additions & 19 deletions

File tree

slack/actions/oauth/callback.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AppContext } from "../../mod.ts";
22
import { SlackOAuthResponse } from "../../utils/client.ts";
3+
import { decodeCustomBotState } from "../../utils/state-helpers.ts";
34

45
export interface Props {
56
code: string;
@@ -19,15 +20,6 @@ export interface Props {
1920
state?: string;
2021
}
2122

22-
function decodeState(state: string) {
23-
try {
24-
const decoded = atob(decodeURIComponent(state));
25-
return JSON.parse(decoded);
26-
} catch {
27-
return {};
28-
}
29-
}
30-
3123
/**
3224
* @name SLACK_OAUTH_CALLBACK
3325
* @title Slack OAuth Callback
@@ -54,7 +46,7 @@ export default async function callback(
5446
let finalBotName = botName;
5547

5648
if (state) {
57-
const stateData = decodeState(state);
49+
const stateData = decodeCustomBotState(state);
5850
if (stateData.isCustomBot) {
5951
finalClientSecret = stateData.customClientSecret || clientSecret;
6052
finalBotName = stateData.customBotName || botName;

slack/loaders/oauth/start.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { OAUTH_URL_AUTH, SCOPES } from "../../utils/constants.ts";
22
import { generateBotSelectionPage } from "../../utils/ui-templates/page-generator.ts";
3+
import { decodeState } from "../../utils/state-helpers.ts";
34

45
export interface Props {
56
clientId: string;
@@ -12,15 +13,6 @@ export interface Props {
1213
botName?: string;
1314
}
1415

15-
function decodeState(state: string) {
16-
try {
17-
const decoded = atob(decodeURIComponent(state));
18-
return JSON.parse(decoded);
19-
} catch {
20-
return {};
21-
}
22-
}
23-
2416
export default function start(props: Props, req: Request) {
2517
const url = new URL(req.url);
2618
const useDecoChatBot = url.searchParams.get("useDecoChatBot");

slack/utils/state-helpers.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Decodes a base64-encoded state parameter from OAuth flow
3+
* @param state - The base64-encoded state string
4+
* @returns The decoded state object or empty object if decoding fails
5+
*/
6+
export function decodeState(state: string): Record<string, unknown> {
7+
try {
8+
const decoded = atob(decodeURIComponent(state));
9+
return JSON.parse(decoded);
10+
} catch {
11+
return {};
12+
}
13+
}
14+
15+
/**
16+
* Type-safe version of decodeState for custom bot state
17+
*/
18+
export interface CustomBotState {
19+
customClientSecret?: string;
20+
customBotName?: string;
21+
isCustomBot?: boolean;
22+
[key: string]: unknown;
23+
}
24+
25+
export function decodeCustomBotState(state: string): CustomBotState {
26+
return decodeState(state) as CustomBotState;
27+
}

0 commit comments

Comments
 (0)