|
| 1 | +import axios from "axios"; |
| 2 | +import { window } from "vscode"; |
| 3 | +import { join } from "path"; |
| 4 | +import { Credential } from "tencentcloud-sdk-nodejs/tencentcloud/common/interface"; |
| 5 | + |
| 6 | +const LOGIN_SERVER = "https://test.ide.cloud.tencent.com"; |
| 7 | + |
| 8 | +const client = axios.create({ |
| 9 | + baseURL: LOGIN_SERVER, |
| 10 | + proxy: { |
| 11 | + protocol: "http", |
| 12 | + host: "9.135.97.58", |
| 13 | + port: 8899, |
| 14 | + }, |
| 15 | +}); |
| 16 | + |
| 17 | +function getQrState() { |
| 18 | + return client.get("/api/public/qcloud/oauth/qrcode").then((res) => { |
| 19 | + return res.data.data; |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +function getQrUrl(state: string) { |
| 24 | + return `${LOGIN_SERVER}/api/public/qcloud/oauth/scan?state=${state}`; |
| 25 | +} |
| 26 | + |
| 27 | +type State = "WAITING" | "SCANNED" | "OK"; |
| 28 | +function getState(state: string): Promise<State> { |
| 29 | + return client |
| 30 | + .get("/api/public/qcloud/oauth/status", { |
| 31 | + params: { |
| 32 | + state, |
| 33 | + }, |
| 34 | + }) |
| 35 | + .then((res) => res.data.data) |
| 36 | + .catch((err) => { |
| 37 | + return "WAITING"; |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +type TokenInfo = { |
| 42 | + scope: string; |
| 43 | + appId: string; |
| 44 | + requestId: string; |
| 45 | + userAccessToken: string; |
| 46 | + userOpenId: string; |
| 47 | + expiresAt: string; |
| 48 | + userRefreshToken: string; |
| 49 | + userUnionId: string; |
| 50 | +}; |
| 51 | + |
| 52 | +function getTokenInfo(state: string): Promise<TokenInfo> { |
| 53 | + return client |
| 54 | + .get("/api/public/qcloud/oauth/token", { params: { state } }) |
| 55 | + .then((res) => res.data.data); |
| 56 | +} |
| 57 | + |
| 58 | +interface CredentialsType { |
| 59 | + credentials: { |
| 60 | + token: string; |
| 61 | + tmpSecretId: string; |
| 62 | + tmpSecretKey: string; |
| 63 | + }; |
| 64 | + expiredTime: string; |
| 65 | +} |
| 66 | + |
| 67 | +function getSecret( |
| 68 | + userAccessToken: string, |
| 69 | + duration = 7200 * 24 |
| 70 | +): Promise<CredentialsType> { |
| 71 | + return client |
| 72 | + .get("/api/public/qcloud/oauth/federationToken", { |
| 73 | + params: { userAccessToken, duration }, |
| 74 | + }) |
| 75 | + .then((res) => res.data.data); |
| 76 | +} |
| 77 | + |
| 78 | +function refreshToken(userRefreshToken: string, userOpenId: string) { |
| 79 | + return client |
| 80 | + .get("/api/public/qcloud/oauth/refreshToken", { |
| 81 | + params: { userRefreshToken, userOpenId }, |
| 82 | + }) |
| 83 | + .then((res) => res.data.data); |
| 84 | +} |
| 85 | + |
| 86 | +export async function getCredentailByQr() { |
| 87 | + const stateCode = await getQrState(); |
| 88 | + const qrUrl = getQrUrl(stateCode); |
| 89 | + |
| 90 | + const terminal = window.createTerminal({ |
| 91 | + name: "登录腾讯云", |
| 92 | + shellPath: join(__dirname, "bin/qrcode.js"), |
| 93 | + shellArgs: [qrUrl], |
| 94 | + isTransient: true, |
| 95 | + }); |
| 96 | + terminal.show(); |
| 97 | + |
| 98 | + let state: State = "WAITING"; |
| 99 | + |
| 100 | + while (state !== "OK") { |
| 101 | + // await delay(500); |
| 102 | + const newState = await getState(stateCode); |
| 103 | + |
| 104 | + if (state !== "SCANNED" && newState === "SCANNED") { |
| 105 | + terminal.sendText("已扫码,等待授权... \n", true); |
| 106 | + } |
| 107 | + |
| 108 | + state = newState; |
| 109 | + } |
| 110 | + |
| 111 | + terminal.sendText("授权成功 \n", true); |
| 112 | + terminal.dispose(); |
| 113 | + |
| 114 | + const u = await getTokenInfo(stateCode); |
| 115 | + |
| 116 | + const { |
| 117 | + credentials: { token, tmpSecretId, tmpSecretKey }, |
| 118 | + } = await getSecret(u.userAccessToken); |
| 119 | + |
| 120 | + return { |
| 121 | + token, |
| 122 | + secretId: tmpSecretId, |
| 123 | + secretKey: tmpSecretKey, |
| 124 | + }; |
| 125 | +} |
0 commit comments