Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert token fail return empty object (#8809) #8980

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/vscode-extension/src/commonlib/codeFlowLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,15 @@ export function UserCancelError(source: string): UserError {
});
}

// if connot convert token via base64, return empty object
export function ConvertTokenToJson(token: string): object {
const array = token.split(".");
const buff = Buffer.from(array[1], "base64");
return JSON.parse(buff.toString(UTF8));
try {
const array = token.split(".");
const buff = Buffer.from(array[1], "base64");
return JSON.parse(buff.toString(UTF8));
} catch (e) {
return {};
}
}

export async function checkIsOnline(): Promise<boolean> {
Expand Down
15 changes: 15 additions & 0 deletions packages/vscode-extension/src/commonlib/m365Login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ export class M365Login extends BasicLogin implements M365TokenProvider {
);
if (tokenRes.isOk()) {
const tokenJson = ConvertTokenToJson(tokenRes.value);
// if token is empty, try to get token by app studio scope, normally this should only affect UX
swatDong marked this conversation as resolved.
Show resolved Hide resolved
if (Object.keys(tokenJson).length === 0) {
const appStudioRes = await M365Login.codeFlowInstance.getTokenByScopes(
AppStudioScopes,
false
);
if (appStudioRes.isOk()) {
const appStudioJson = ConvertTokenToJson(appStudioRes.value);
return ok({
status: signedIn,
token: appStudioRes.value,
accountInfo: appStudioJson as any,
});
}
}
return ok({ status: signedIn, token: tokenRes.value, accountInfo: tokenJson as any });
} else {
if (
Expand Down