@@ -18,10 +18,13 @@ type Auth0RuntimeEnvironment = RuntimeEnvironment<
18
18
client_id ?: string ;
19
19
issuer_base_url ?: string ;
20
20
private_key ?: string ;
21
+ client_secret ?: string ;
21
22
}
22
23
> ;
23
- type Auth0RuntimeContext = RuntimeContext < Auth0RuntimeEnvironment > ;
24
24
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 > ;
25
28
const handleFetchEvent : FetchEventCallback < Auth0RuntimeContext > = async ( request , context ) => {
26
29
const { environment } = context ;
27
30
const installationURL = environment . spaceInstallation ?. urls ?. publicEndpoint ;
@@ -33,10 +36,15 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
33
36
// eslint-disable-next-line no-console
34
37
console . log ( 'redirecting bby' ) ;
35
38
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 });
36
44
// return Response.json({ error: installationURL });
37
45
try {
38
46
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 } `
40
48
) ;
41
49
} catch ( e ) {
42
50
return Response . json ( { error : e . stack } ) ;
@@ -54,7 +62,8 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
54
62
) ;
55
63
// eslint-disable-next-line no-console
56
64
console . log ( 'space' , space ) ;
57
- // return Response.redirect('https://www.google.no'); WORKS
65
+ // return Response.json({ url: request.url });
66
+ // WORKS;
58
67
const obj = space . data ;
59
68
const privateKey = context . environment . spaceInstallation . configuration . private_key ;
60
69
// eslint-disable-next-line no-console
@@ -68,38 +77,54 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
68
77
privateKey ? privateKey : ''
69
78
) ;
70
79
} catch ( e ) {
71
- return Response . json ( { error : privateKey } ) ;
80
+ return Response . json ( { error : e . stack } ) ;
72
81
}
73
82
// 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
+ }
103
128
// // return Response.redirect('https://www.google.no');
104
129
// return Response.redirect(
105
130
// obj.urls?.published && token
@@ -109,7 +134,7 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
109
134
}
110
135
// eslint-disable-next-line no-console
111
136
console . log ( 'noting here' ) ;
112
- return Response . redirect ( 'https://www.google.com' ) ;
137
+ // return Response.redirect('https://www.google.com');
113
138
} ) ;
114
139
/**
115
140
* Handle GitHub App webhook events
@@ -135,86 +160,6 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
135
160
}
136
161
} ;
137
162
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
-
218
163
export default createIntegration ( {
219
164
fetch : handleFetchEvent ,
220
165
events : {
@@ -239,23 +184,16 @@ export default createIntegration({
239
184
{
240
185
configuration : {
241
186
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,
244
192
} ,
245
193
}
246
194
) ;
247
195
// eslint-disable-next-line no-console
248
196
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 ) ;
259
197
}
260
198
} ,
261
199
} ,
0 commit comments