From 6c3dcc2fbfb63e2d22936569dd1172d96fbcd8fd Mon Sep 17 00:00:00 2001 From: Cheng CHENG Date: Thu, 1 Sep 2022 03:19:30 +0800 Subject: [PATCH] fix: lark calendar refresh token bug (#4066) Co-authored-by: chengcheng.frontend Co-authored-by: Joe Au-Yeung Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Peer Richelsen --- .../larkcalendar/lib/AppAccessToken.ts | 4 ++-- .../larkcalendar/lib/CalendarService.ts | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/app-store/larkcalendar/lib/AppAccessToken.ts b/packages/app-store/larkcalendar/lib/AppAccessToken.ts index e151352396..a0aa2040e8 100644 --- a/packages/app-store/larkcalendar/lib/AppAccessToken.ts +++ b/packages/app-store/larkcalendar/lib/AppAccessToken.ts @@ -107,7 +107,7 @@ export const getAppAccessToken: () => Promise = async () => { const expireDate = appKeys.expire_date; if (appAccessToken && expireDate && !isExpired(expireDate)) { - log.debug("get app access token not expired", appAccessToken); + log.debug("get app access token not expired"); return appAccessToken; } @@ -137,7 +137,7 @@ export const getAppAccessToken: () => Promise = async () => { where: { slug: "lark-calendar" }, data: { keys: { ...appKeys, app_ticket: "" } }, }); - return getAppAccessToken(); + throw new Error("app_ticket invalid, please try again"); } } diff --git a/packages/app-store/larkcalendar/lib/CalendarService.ts b/packages/app-store/larkcalendar/lib/CalendarService.ts index ee86978bff..b98008ef84 100644 --- a/packages/app-store/larkcalendar/lib/CalendarService.ts +++ b/packages/app-store/larkcalendar/lib/CalendarService.ts @@ -71,7 +71,7 @@ export default class LarkCalendarService implements Calendar { } try { const appAccessToken = await getAppAccessToken(); - const resp = await this.fetcher(`/authen/v1/refresh_access_token`, { + const resp = await fetch(`${this.url}/authen/v1/refresh_access_token`, { method: "POST", headers: { Authorization: `Bearer ${appAccessToken}`, @@ -84,8 +84,15 @@ export default class LarkCalendarService implements Calendar { }); const data = await handleLarkError(resp, this.log); + this.log.debug( + "LarkCalendarService refreshAccessToken data refresh_expires_in", + data.data.refresh_expires_in, + "and access token expire in", + data.data.expires_in + ); const newLarkAuthCredentials: LarkAuthCredentials = { - ...larkAuthCredentials, + refresh_token: data.data.refresh_token, + refresh_expires_date: Math.round(+new Date() / 1000 + data.data.refresh_expires_in), access_token: data.data.access_token, expiry_date: Math.round(+new Date() / 1000 + data.data.expires_in), }; @@ -107,7 +114,13 @@ export default class LarkCalendarService implements Calendar { }; private fetcher = async (endpoint: string, init?: RequestInit | undefined) => { - const accessToken = await this.auth.getToken(); + let accessToken = ""; + try { + accessToken = await this.auth.getToken(); + } catch (error) { + throw new Error("get access token error"); + } + return fetch(`${this.url}${endpoint}`, { method: "GET", headers: {