1
1
import { sign } from '@tsndr/cloudflare-worker-jwt' ;
2
2
import { Router } from 'itty-router' ;
3
- // import * as jwt from 'jsonwebtoken';
4
3
4
+ // import * as jwt from 'jsonwebtoken';
5
+ import { IntegrationInstallationConfiguration } from '@gitbook/api' ;
5
6
import {
6
7
createIntegration ,
7
8
FetchEventCallback ,
8
9
Logger ,
9
10
RuntimeContext ,
10
11
RuntimeEnvironment ,
11
12
EventCallback ,
13
+ createComponent ,
12
14
} from '@gitbook/runtime' ;
13
15
14
16
// import type { GithubRuntimeContext } from '../../github/src/types';
@@ -17,15 +19,94 @@ const logger = Logger('auth0.visitor-auth');
17
19
18
20
type Auth0RuntimeEnvironment = RuntimeEnvironment <
19
21
{ } ,
20
- {
21
- client_id ?: string ;
22
- issuer_base_url ?: string ;
22
+ Auth0SpaceInstallationConfiguration & {
23
23
private_key ?: string ;
24
- client_secret ?: string ;
25
24
}
26
25
> ;
27
26
28
27
type Auth0RuntimeContext = RuntimeContext < Auth0RuntimeEnvironment > ;
28
+
29
+ type Auth0SpaceInstallationConfiguration = {
30
+ client_id ?: string ;
31
+ issuer_base_url ?: string ;
32
+ client_secret ?: string ;
33
+ } ;
34
+
35
+ type Auth0State = Auth0SpaceInstallationConfiguration ;
36
+
37
+ type Auth0Props = {
38
+ installation : {
39
+ configuration ?: IntegrationInstallationConfiguration ;
40
+ } ;
41
+ spaceInstallation : {
42
+ configuration ?: Auth0SpaceInstallationConfiguration ;
43
+ } ;
44
+ } ;
45
+
46
+ export type Auth0Action = { action : 'save.config' } ;
47
+
48
+ const helloWorldBlock = createComponent < Auth0Props , Auth0State , Auth0Action , Auth0RuntimeContext > ( {
49
+ componentId : 'hello' ,
50
+ initialState : ( props ) => {
51
+ return {
52
+ client_id : props . spaceInstallation . configuration ?. client_id ?. toString ( ) || '' ,
53
+ issuer_base_url :
54
+ props . spaceInstallation . configuration ?. issuer_base_url ?. toString ( ) || '' ,
55
+ client_secret : props . spaceInstallation . configuration ?. client_secret ?. toString ( ) || '' ,
56
+ } ;
57
+ } ,
58
+ action : async ( element , action , context ) => {
59
+ switch ( action . action ) {
60
+ case 'save.config' :
61
+ // await saveConfig();
62
+ return element ;
63
+ }
64
+ } ,
65
+ render : async ( element , context ) => {
66
+ // eslint-disable-next-line no-console
67
+ console . log (
68
+ 'disabled?' ,
69
+ element . dynamicState ( 'client_id' ) ,
70
+ ! element . state . client_id ||
71
+ ! element . state . issuer_base_url ||
72
+ ! element . state . client_secret
73
+ ) ;
74
+ const showButton =
75
+ element . state . client_id && element . state . issuer_base_url && element . state . client_secret ;
76
+ return (
77
+ < block >
78
+ < textinput state = "client_id" placeholder = "Enter Client Id" />
79
+ < textinput state = "issuer_base_url" placeholder = "Enter Issuer Base URL" />
80
+ < textinput state = "client_secret" placeholder = "Enter Client Secret" />
81
+ { true ? (
82
+ < input
83
+ label = ""
84
+ hint = ""
85
+ element = {
86
+ < button
87
+ style = "primary"
88
+ disabled = {
89
+ ! element . state . client_id ||
90
+ ! element . state . client_secret ||
91
+ ! element . state . issuer_base_url
92
+ }
93
+ label = "Configure"
94
+ tooltip = "Save configuration"
95
+ onPress = { {
96
+ action : 'save.config' ,
97
+ client_id : element . dynamicState ( 'client_id' ) ,
98
+ client_secret : element . dynamicState ( 'client_secret' ) ,
99
+ issuer_base_url : element . dynamicState ( 'issuer_base_url' ) ,
100
+ } }
101
+ />
102
+ }
103
+ />
104
+ ) : null }
105
+ </ block >
106
+ ) ;
107
+ } ,
108
+ } ) ;
109
+
29
110
const handleFetchEvent : FetchEventCallback < Auth0RuntimeContext > = async ( request , context ) => {
30
111
const { environment } = context ;
31
112
const installationURL = environment . spaceInstallation ?. urls ?. publicEndpoint ;
@@ -180,8 +261,27 @@ const handleFetchEvent: FetchEventCallback<Auth0RuntimeContext> = async (request
180
261
181
262
export default createIntegration ( {
182
263
fetch : handleFetchEvent ,
264
+ components : [ helloWorldBlock ] ,
265
+ fetch_visitor_authentication : async ( event , context ) => {
266
+ const { environment } = context ;
267
+ const installationURL = environment . spaceInstallation ?. urls ?. publicEndpoint ;
268
+ const issuerBaseUrl = environment . spaceInstallation ?. configuration . issuer_base_url ;
269
+ const clientId = environment . spaceInstallation ?. configuration . client_id ;
270
+ const location = '' ;
271
+
272
+ try {
273
+ return Response . redirect (
274
+ `${ issuerBaseUrl } /authorize?response_type=code&client_id=${ clientId } &redirect_uri=${ installationURL } /visitor-auth/response&state=${ location } `
275
+ ) ;
276
+ } catch ( e ) {
277
+ return Response . json ( { error : e . stack } ) ;
278
+ }
279
+
280
+ // await triggerExport(context, spaceInstallation);
281
+ } ,
183
282
events : {
184
283
space_installation_setup : async ( event , context ) => {
284
+ // check event status to be active
185
285
if ( ! context . environment . spaceInstallation ?. configuration . private_key ) {
186
286
const res = await context . api . integrations . updateIntegrationSpaceInstallation (
187
287
context . environment . integration . name ,
@@ -195,22 +295,5 @@ export default createIntegration({
195
295
) ;
196
296
}
197
297
} ,
198
- fetch_visitor_authentication : async ( event , context ) => {
199
- const { environment } = context ;
200
- const installationURL = environment . spaceInstallation ?. urls ?. publicEndpoint ;
201
- const issuerBaseUrl = environment . spaceInstallation ?. configuration . issuer_base_url ;
202
- const clientId = environment . spaceInstallation ?. configuration . client_id ;
203
- const location = event . location || '' ;
204
-
205
- try {
206
- return Response . redirect (
207
- `${ issuerBaseUrl } /authorize?response_type=code&client_id=${ clientId } &redirect_uri=${ installationURL } /visitor-auth/response&state=${ location } `
208
- ) ;
209
- } catch ( e ) {
210
- return Response . json ( { error : e . stack } ) ;
211
- }
212
-
213
- // await triggerExport(context, spaceInstallation);
214
- } ,
215
298
} ,
216
299
} ) ;
0 commit comments