@@ -19,34 +19,39 @@ async function getToken(context, code: string, client_secret: string) {
1919}
2020
2121export const onRequestPost : PagesFunction < Env > = async ( context ) => {
22- const { code } = await context . request . json < { code : string } > ( ) ;
23- let vimResponse = await getToken ( context , code , context . env . CLIENT_SECRET ) ;
24- if (
25- vimResponse . status >= 400 &&
26- vimResponse . status < 500 &&
27- context . env . CLIENT_SECRET_FALLBACK
28- ) {
29- vimResponse = await getToken (
30- context ,
31- code ,
22+ try {
23+ const { code } = await context . request . json < { code : string } > ( ) ;
24+ let vimResponse = await getToken ( context , code , context . env . CLIENT_SECRET ) ;
25+ if (
26+ vimResponse . status >= 400 &&
27+ vimResponse . status < 500 &&
3228 context . env . CLIENT_SECRET_FALLBACK
33- ) ;
34- }
35- const tokenData = await vimResponse . json ( ) ;
36- if (
37- ! ( await isAuthorized (
38- tokenData ,
39- context . env . CLIENT_ID ,
40- context . env . VIM_ISSUER
41- ) )
42- ) {
43- return new Response ( "" , {
44- status : 403 ,
45- statusText : "Forbidden: You do not have access to this resource." ,
46- } ) ;
47- }
29+ ) {
30+ vimResponse = await getToken (
31+ context ,
32+ code ,
33+ context . env . CLIENT_SECRET_FALLBACK
34+ ) ;
35+ }
36+ const tokenData = await vimResponse . json ( ) ;
37+ if (
38+ ! ( await isAuthorized (
39+ tokenData ,
40+ context . env . CLIENT_ID ,
41+ context . env . VIM_ISSUER
42+ ) )
43+ ) {
44+ return new Response ( "" , {
45+ status : 403 ,
46+ statusText : "Forbidden: You do not have access to this resource." ,
47+ } ) ;
48+ }
4849
49- return Response . json ( tokenData ) ;
50+ return Response . json ( tokenData ) ;
51+ } catch ( error ) {
52+ console . log ( "Error parsing body" , { request : context . request . text ( ) } ) ;
53+ return error ;
54+ }
5055} ;
5156
5257async function isAuthorized (
0 commit comments