Skip to content

Commit a2a6870

Browse files
authored
Merge pull request #2 from thoughtspot/oauth-impl
encode state in /callback
2 parents 14a344a + 66f8c12 commit a2a6870

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/handlers.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Props } from './utils';
44
import { parseRedirectApproval, renderApprovalDialog } from './oauth-manager/oauth-utils';
55
import { renderTokenCallback } from './oauth-manager/token-utils';
66
import { any } from 'zod';
7+
import { encodeBase64, decodeBase64 } from 'hono/utils/encode';
78

89

910
const app = new Hono<{ Bindings: Env & { OAUTH_PROVIDER: OAuthHelpers } }>()
@@ -54,7 +55,8 @@ app.post("/authorize", async (c) => {
5455
// The callback endpoint will get the encrypted token and decrypt it to get the user's access token.
5556
const targetURLPath = new URL("/callback", c.req.url);
5657
targetURLPath.searchParams.append('instanceUrl', instanceUrl);
57-
targetURLPath.searchParams.append('oauthReqInfo', JSON.stringify(state.oauthReqInfo));
58+
const encodedState = btoa(JSON.stringify(state.oauthReqInfo));
59+
targetURLPath.searchParams.append('oauthReqInfo', encodedState);
5860
redirectUrl.searchParams.append('targetURLPath', targetURLPath.href);
5961
console.log("redirectUrl", redirectUrl.toString());
6062

@@ -63,15 +65,15 @@ app.post("/authorize", async (c) => {
6365

6466
app.get("/callback", async (c) => {
6567
const instanceUrl = c.req.query('instanceUrl');
66-
const oauthReqInfo = c.req.query('oauthReqInfo');
68+
const encodedOauthReqInfo = c.req.query('oauthReqInfo');
6769
if (!instanceUrl) {
6870
return c.text('Missing instance URL', 400);
6971
}
70-
if (!oauthReqInfo) {
72+
if (!encodedOauthReqInfo) {
7173
return c.text('Missing OAuth request info', 400);
7274
}
73-
74-
return new Response(renderTokenCallback(instanceUrl, oauthReqInfo), {
75+
const decodedOAuthReqInfo = JSON.parse(atob(encodedOauthReqInfo));
76+
return new Response(renderTokenCallback(instanceUrl, decodedOAuthReqInfo), {
7577
headers: {
7678
'Content-Type': 'text/html',
7779
},
@@ -84,8 +86,6 @@ app.post("/store-token", async (c) => {
8486
return c.text('Missing token or OAuth request info or instanceUrl', 400);
8587
}
8688

87-
console.log('Token received and stored', token);
88-
8989
// Complete the authorization with the provided information
9090
const { redirectTo } = await c.env.OAUTH_PROVIDER.completeAuthorization({
9191
request: oauthReqInfo,
@@ -95,7 +95,7 @@ app.post("/store-token", async (c) => {
9595
},
9696
scope: oauthReqInfo.scope,
9797
props: {
98-
accessToken: token.token,
98+
accessToken: token.data.token,
9999
instanceUrl: instanceUrl,
100100
} as Props,
101101
});

0 commit comments

Comments
 (0)