Skip to content

Commit 2c0a381

Browse files
committed
Use variables
1 parent 1db0c5f commit 2c0a381

File tree

2 files changed

+70
-127
lines changed

2 files changed

+70
-127
lines changed

integrations/va-auth0/gitbook-manifest.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ configurations:
2323
client_id:
2424
type: string
2525
title: Client ID
26-
description: Client ID of your Auth0 instance
26+
description: Client ID of your Auth0 app
2727
issuer_base_url:
2828
type: string
2929
title: Issuer Base URL
3030
description: Issuer Base URL of your Auth0 instance
31+
client_secret:
32+
type: string
33+
title: Client Secret
34+
description: The Client Secret of your Auth0 app
3135

3236
required:
3337
- client_id
3438
- issuer_base_url
39+
- client_secret

integrations/va-auth0/src/index.ts

Lines changed: 64 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ type Auth0RuntimeEnvironment = RuntimeEnvironment<
1818
client_id?: string;
1919
issuer_base_url?: string;
2020
private_key?: string;
21+
client_secret?: string;
2122
}
2223
>;
23-
type Auth0RuntimeContext = RuntimeContext<Auth0RuntimeEnvironment>;
2424

25+
// https://dev-qyd2bk185i3mltdi.us.auth0.com/authorize?response_type=code&client_id=xEyiJiDYHQ6JQrOVBvhgXQxhi2KY4cC8&redirect_uri=${installationURL}/visitor-auth/response&state=${location}`
26+
27+
type Auth0RuntimeContext = RuntimeContext<Auth0RuntimeEnvironment>;
2528
const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request, context) => {
2629
const { environment } = context;
2730
const installationURL = environment.spaceInstallation?.urls?.publicEndpoint;
@@ -33,10 +36,15 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
3336
// eslint-disable-next-line no-console
3437
console.log('redirecting bby');
3538
logger.debug('Got a request');
39+
const location = request.query.location;
40+
const issuerBaseUrl = environment.spaceInstallation?.configuration.issuer_base_url;
41+
const clientId = environment.spaceInstallation?.configuration.client_id;
42+
43+
// return Response.json({ url_received: request.url, query: request.query });
3644
// return Response.json({ error: installationURL });
3745
try {
3846
return Response.redirect(
39-
`https://dev-qyd2bk185i3mltdi.us.auth0.com/authorize?response_type=code&client_id=xEyiJiDYHQ6JQrOVBvhgXQxhi2KY4cC8&redirect_uri=${installationURL}/visitor-auth/response`
47+
`${issuerBaseUrl}/authorize?response_type=code&client_id=${clientId}&redirect_uri=${installationURL}/visitor-auth/response&state=${location}`
4048
);
4149
} catch (e) {
4250
return Response.json({ error: e.stack });
@@ -54,7 +62,8 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
5462
);
5563
// eslint-disable-next-line no-console
5664
console.log('space', space);
57-
// return Response.redirect('https://www.google.no'); WORKS
65+
// return Response.json({ url: request.url });
66+
// WORKS;
5867
const obj = space.data;
5968
const privateKey = context.environment.spaceInstallation.configuration.private_key;
6069
// eslint-disable-next-line no-console
@@ -68,38 +77,54 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
6877
privateKey ? privateKey : ''
6978
);
7079
} catch (e) {
71-
return Response.json({ error: privateKey });
80+
return Response.json({ error: e.stack });
7281
}
7382
// return Response.json({ query: request.query });
74-
const searchParams = new URLSearchParams({
75-
grant_type: 'authorization_code',
76-
client_id: 'xEyiJiDYHQ6JQrOVBvhgXQxhi2KY4cC8',
77-
client_secret:
78-
'twYDeVkbjC9ZjhVs1cgII-0oX5CAsa1gsg2b7NDLJm7Vtv0ngOl54WlrKtnGMjhF',
79-
code: `${request.query.code}`,
80-
redirect_uri: `${installationURL}/visitor-auth/response`,
81-
});
82-
// return Response.json({ searchParams });
83-
const url = `https://dev-qyd2bk185i3mltdi.us.auth0.com/oauth/token/`;
84-
// return Response.json({ url });
85-
const resp = await fetch(url, {
86-
method: 'POST',
87-
headers: { 'content-type': 'application/x-www-form-urlencoded' },
88-
body: searchParams,
89-
}).then((response) => response.json());
90-
// .then((response) => response.json())
91-
// .then((data) => {
92-
// return data;
93-
// // return Response.redirect(
94-
// // obj.urls?.published && token
95-
// // ? `${obj.urls?.published}/?jwt_token=${token}`
96-
// // : 'https://www.google.dk'
97-
// // );
98-
// })
99-
// .catch((err) => {
100-
// return Response.json({ err });
101-
// });
102-
return Response.json({ resp });
83+
const issuerBaseUrl = environment.spaceInstallation?.configuration.issuer_base_url;
84+
const clientId = environment.spaceInstallation?.configuration.client_id;
85+
const clientSecret = environment.spaceInstallation?.configuration.client_secret;
86+
if (clientId && clientSecret) {
87+
const searchParams = new URLSearchParams({
88+
grant_type: 'authorization_code',
89+
client_id: clientId,
90+
client_secret: clientSecret,
91+
code: `${request.query.code}`,
92+
redirect_uri: `${installationURL}/visitor-auth/response`,
93+
});
94+
// return Response.json({ searchParams });
95+
const url = `${issuerBaseUrl}/oauth/token/`;
96+
// return Response.json({ url });
97+
const resp: any = await fetch(url, {
98+
method: 'POST',
99+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
100+
body: searchParams,
101+
})
102+
.then((response) => response.json())
103+
.catch((err) => {
104+
return Response.json({ err });
105+
});
106+
107+
if ('access_token' in resp) {
108+
let url;
109+
// return Response.json({ state: request.query.state });
110+
if (request.query.state) {
111+
url = `${obj.urls?.published}${request.query.state}/?jwt_token=${token}`;
112+
} else {
113+
url = `${obj.urls?.published}/?jwt_token=${token}`;
114+
}
115+
return Response.redirect(
116+
obj.urls?.published && token ? url : 'https://www.google.dk'
117+
);
118+
} else {
119+
return Response.json({
120+
Error: 'No Access Token found in the response from Auth0',
121+
});
122+
}
123+
} else {
124+
return Response.json({
125+
Error: 'Either ClientId or ClientSecret is missing',
126+
});
127+
}
103128
// // return Response.redirect('https://www.google.no');
104129
// return Response.redirect(
105130
// obj.urls?.published && token
@@ -109,7 +134,7 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
109134
}
110135
// eslint-disable-next-line no-console
111136
console.log('noting here');
112-
return Response.redirect('https://www.google.com');
137+
// return Response.redirect('https://www.google.com');
113138
});
114139
/**
115140
* Handle GitHub App webhook events
@@ -135,86 +160,6 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
135160
}
136161
};
137162

138-
/*
139-
* Handle content being updated: Trigger an export to GitHub
140-
*/
141-
// const handleSpaceContentUpdated: EventCallback<
142-
// 'space_content_updated',
143-
// GithubRuntimeContext
144-
// > = async (event, context) => {
145-
// const { data: revision } = await context.api.spaces.getRevisionById(
146-
// event.spaceId,
147-
// event.revisionId
148-
// );
149-
// if (revision.git?.oid) {
150-
// const revisionStatus = revision.git.createdByGitBook ? 'exported' : 'imported';
151-
// logger.info(
152-
// `skipping Git Sync for space ${event.spaceId} revision ${revision.id} as it was already ${revisionStatus}`
153-
// );
154-
// return;
155-
// }
156-
157-
// const spaceInstallation = context.environment.spaceInstallation;
158-
// if (!spaceInstallation) {
159-
// logger.debug(`missing space installation, skipping`);
160-
// return;
161-
// }
162-
163-
// await triggerExport(context, spaceInstallation);
164-
// };
165-
166-
// /*
167-
// * Handle git sync started: Update commit status
168-
// */
169-
// const handleGitSyncStarted: EventCallback<'space_gitsync_started', GithubRuntimeContext> = async (
170-
// event,
171-
// context
172-
// ) => {
173-
// logger.info(
174-
// `Git Sync started for space ${event.spaceId} revision ${event.revisionId}, updating commit status`
175-
// );
176-
177-
// const spaceInstallation = context.environment.spaceInstallation;
178-
// if (!spaceInstallation) {
179-
// logger.debug(`missing space installation, skipping`);
180-
// return;
181-
// }
182-
183-
// await updateCommitWithPreviewLinks(
184-
// context,
185-
// spaceInstallation,
186-
// event.revisionId,
187-
// event.commitId,
188-
// GitSyncOperationState.Running
189-
// );
190-
// };
191-
192-
// /**
193-
// * Handle git sync completed: Update commit status
194-
// */
195-
// const handleGitSyncCompleted: EventCallback<
196-
// 'space_gitsync_completed',
197-
// GithubRuntimeContext
198-
// > = async (event, context) => {
199-
// logger.info(
200-
// `Git Sync completed (${event.state}) for space ${event.spaceId} revision ${event.revisionId}, updating commit status`
201-
// );
202-
203-
// const spaceInstallation = context.environment.spaceInstallation;
204-
// if (!spaceInstallation) {
205-
// logger.debug(`missing space installation, skipping`);
206-
// return;
207-
// }
208-
209-
// await updateCommitWithPreviewLinks(
210-
// context,
211-
// spaceInstallation,
212-
// event.revisionId,
213-
// event.commitId,
214-
// event.state as GitSyncOperationState
215-
// );
216-
// };
217-
218163
export default createIntegration({
219164
fetch: handleFetchEvent,
220165
events: {
@@ -239,23 +184,16 @@ export default createIntegration({
239184
{
240185
configuration: {
241186
private_key: crypto.randomUUID(),
242-
client_id: 'test',
243-
issuer_base_url: 'url_test',
187+
// client_id:
188+
// context.environment.spaceInstallation?.configuration.client_id,
189+
// issuer_base_url:
190+
// context.environment.spaceInstallation?.configuration
191+
// .issuer_base_url,
244192
},
245193
}
246194
);
247195
// eslint-disable-next-line no-console
248196
console.log('recevied response', res.data);
249-
} else {
250-
// eslint-disable-next-line no-console
251-
console.log('already has oprivate key');
252-
const res = await context.api.integrations.getIntegrationSpaceInstallation(
253-
context.environment.integration.name,
254-
event.installationId,
255-
event.spaceId
256-
);
257-
// eslint-disable-next-line no-console
258-
console.log('existing config', res.data);
259197
}
260198
},
261199
},

0 commit comments

Comments
 (0)