Skip to content

Commit

Permalink
Merge pull request #8980 from OfficeDev/fix/dev-tunnel-token
Browse files Browse the repository at this point in the history
fix: convert token fail return empty object (#8809)
  • Loading branch information
kimizhu authored Jun 9, 2023
2 parents 4ea9bbf + a1061da commit 5798c86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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
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

0 comments on commit 5798c86

Please sign in to comment.