Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 23f8d47

Browse files
committed
support new auth protocol & fix useless tips
1 parent 4351023 commit 23f8d47

12 files changed

Lines changed: 158 additions & 75 deletions

File tree

lib/auth/auth.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6-
const http_1 = require("http");
76
const os_1 = __importDefault(require("os"));
7+
const http_1 = require("http");
88
const portfinder_1 = __importDefault(require("portfinder"));
99
const query_string_1 = __importDefault(require("query-string"));
1010
const open_1 = __importDefault(require("open"));
1111
const ora_1 = __importDefault(require("ora"));
1212
const request_1 = __importDefault(require("request"));
1313
const crypto_1 = require("crypto");
1414
const logger_1 = __importDefault(require("../logger"));
15+
const os_release_1 = require("../utils/os-release");
1516
const logger = new logger_1.default('Auth');
1617
const defaultPort = 9012;
1718
const CliAuthBaseUrl = 'https://console.cloud.tencent.com/tcb/auth';
@@ -35,6 +36,13 @@ function getMacAddress() {
3536
const mac = (netInterface.length && netInterface[0].mac) || '';
3637
return mac;
3738
}
39+
function getOSInfo() {
40+
const hostname = os_1.default.hostname();
41+
const platform = os_1.default.platform();
42+
const release = os_1.default.release();
43+
const platformRelease = os_release_1.getPlatformRelease(platform, release);
44+
return [hostname, platformRelease].join('/');
45+
}
3846
function md5(str) {
3947
const hash = crypto_1.createHash('md5');
4048
hash.update(str);
@@ -63,8 +71,9 @@ async function getAuthTokenFromWeb() {
6371
try {
6472
const { server, port } = await createLocalServer();
6573
const mac = getMacAddress();
74+
const os = getOSInfo();
6675
const hash = md5(mac);
67-
const CliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}`;
76+
const CliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`;
6877
await open_1.default(CliAuthUrl);
6978
authSpinner.succeed('已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!');
7079
server.on('request', (req, res) => {

lib/commands/login.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ const error_1 = require("../error");
1212
const utils_1 = require("../utils");
1313
const logger_1 = require("../logger");
1414
async function checkLogin() {
15-
const tcbrc = utils_1.getCredentialConfig();
16-
if (tcbrc.secretId && tcbrc.secretKey) {
15+
const credential = utils_1.getCredentialConfig();
16+
if (credential.secretId && credential.secretKey) {
1717
return true;
1818
}
19-
const tmpExpired = Number(tcbrc.tmpExpired) || 0;
20-
const now = Date.now();
21-
if (now < tmpExpired) {
22-
return true;
19+
if (credential.refreshToken) {
20+
if (Date.now() < Number(credential.tmpExpired)) {
21+
return true;
22+
}
23+
else if (Date.now() < Number(credential.expired)) {
24+
return true;
25+
}
2326
}
2427
return false;
2528
}
@@ -31,9 +34,13 @@ commander_1.default
3134
.action(async function (options) {
3235
const checkSpin = ora_1.default('检验登录状态').start();
3336
const hasLogin = await checkLogin();
34-
checkSpin.succeed('您已登录,无需再次登录!');
35-
if (hasLogin)
37+
if (hasLogin) {
38+
checkSpin.succeed('您已登录,无需再次登录!');
3639
return;
40+
}
41+
else {
42+
checkSpin.stop();
43+
}
3744
let skey;
3845
if (options.key || options.skey) {
3946
const { secretId } = await inquirer_1.default.prompt({

lib/env/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ exports.getEnvInfo = getEnvInfo;
4242
async function listEnvs() {
4343
const res = await tcbService.request('DescribeEnvs');
4444
const { EnvList = [] } = res;
45-
console.log(res.EnvList[0].Functions);
4645
return EnvList;
4746
}
4847
exports.listEnvs = listEnvs;

lib/utils/os-release.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const macOSMap = new Map([
4+
[19, 'Catalina'],
5+
[18, 'Mojave'],
6+
[17, 'High Sierra'],
7+
[16, 'Sierra'],
8+
[15, 'El Capitan'],
9+
[14, 'Yosemite'],
10+
[13, 'Mavericks'],
11+
[12, 'Mountain Lion'],
12+
[11, 'Lion'],
13+
[10, 'Snow Leopard'],
14+
[9, 'Leopard'],
15+
[8, 'Tiger'],
16+
[7, 'Panther'],
17+
[6, 'Jaguar'],
18+
[5, 'Puma']
19+
]);
20+
const winMap = new Map([
21+
['10.0', '10'],
22+
['6.3', '8.1'],
23+
['6.2', '8'],
24+
['6.1', '7'],
25+
['6.0', 'Vista'],
26+
['5.2', 'Server 2003'],
27+
['5.1', 'XP'],
28+
['5.0', '2000'],
29+
['4.9', 'ME'],
30+
['4.1', '98'],
31+
['4.0', '95']
32+
]);
33+
function getPlatformRelease(platform, release) {
34+
if (platform === 'darwin') {
35+
release = Number(release.split('.')[0]);
36+
const name = macOSMap.get(release);
37+
const version = '10.' + (release - 4);
38+
return `${name} ${version}`;
39+
}
40+
if (platform === 'win32') {
41+
const version = (/\d+\.\d/.exec(release) || [])[0];
42+
return `Windows ${winMap.get(version)}`;
43+
}
44+
return 'Linux';
45+
}
46+
exports.getPlatformRelease = getPlatformRelease;

src/auth/auth.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createServer, IncomingMessage, ServerResponse, Server } from 'http'
21
import os from 'os'
2+
import { createServer, IncomingMessage, ServerResponse, Server } from 'http'
33
import portfinder from 'portfinder'
44
import queryString from 'query-string'
55
import open from 'open'
@@ -8,6 +8,7 @@ import request from 'request'
88
import { createHash } from 'crypto'
99
import Logger from '../logger'
1010
import { Credential } from '../types'
11+
import { getPlatformRelease } from '../utils/os-release'
1112

1213
const logger = new Logger('Auth')
1314

@@ -44,6 +45,17 @@ function getMacAddress(): string {
4445
return mac
4546
}
4647

48+
// 获取 hostname 和平台信息
49+
function getOSInfo() {
50+
const hostname = os.hostname()
51+
const platform = os.platform()
52+
const release = os.release()
53+
54+
const platformRelease = getPlatformRelease(platform, release)
55+
56+
return [hostname, platformRelease].join('/')
57+
}
58+
4759
// MD5
4860
function md5(str: string): string {
4961
const hash = createHash('md5')
@@ -76,8 +88,10 @@ export async function getAuthTokenFromWeb(): Promise<Credential> {
7688
try {
7789
const { server, port } = await createLocalServer()
7890
const mac = getMacAddress()
91+
const os = getOSInfo()
7992
const hash = md5(mac)
80-
const CliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}`
93+
94+
const CliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`
8195
await open(CliAuthUrl)
8296

8397
authSpinner.succeed(

src/auth/login.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ export async function loginWithKey(secretId?: string, secretKey?: string) {
108108
return LoginRes.SUCCESS
109109
}
110110

111-
112-
113111
export async function login(
114112
options?: ILoginOptios
115113
): Promise<{ code: string; msg: string }> {

src/commands/login.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ import { Credential } from '../types'
99
import { warnLog } from '../logger'
1010

1111
async function checkLogin() {
12-
const tcbrc: Credential = getCredentialConfig()
12+
const credential: Credential = getCredentialConfig()
1313
// 已有永久密钥
14-
if (tcbrc.secretId && tcbrc.secretKey) {
14+
if (credential.secretId && credential.secretKey) {
1515
return true
1616
}
1717

18-
// 如果存在临时密钥,校验临时密钥是否有效,是否需要续期
19-
const tmpExpired = Number(tcbrc.tmpExpired) || 0
20-
const now = Date.now()
21-
22-
if (now < tmpExpired) {
23-
return true
18+
// 存在临时密钥信息
19+
if (credential.refreshToken) {
20+
// 临时密钥在有效期内,可以直接使用
21+
if (Date.now() < Number(credential.tmpExpired)) {
22+
return true
23+
} else if (Date.now() < Number(credential.expired)) {
24+
// 临时密钥超过两小时有效期,但在 1 个月 refresh 有效期内,刷新临时密钥
25+
return true
26+
}
2427
}
28+
2529
return false
2630
}
2731

@@ -34,8 +38,12 @@ program
3438
.action(async function(options) {
3539
const checkSpin = ora('检验登录状态').start()
3640
const hasLogin = await checkLogin()
37-
checkSpin.succeed('您已登录,无需再次登录!')
38-
if (hasLogin) return
41+
if (hasLogin) {
42+
checkSpin.succeed('您已登录,无需再次登录!')
43+
return
44+
} else {
45+
checkSpin.stop()
46+
}
3947
// 兼容临时密钥和永久密钥登录
4048
let skey
4149
if (options.key || options.skey) {

src/env/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export async function getEnvInfo(envId: string) {
4848
export async function listEnvs() {
4949
const res: any = await tcbService.request('DescribeEnvs')
5050
const { EnvList = [] } = res
51-
console.log(res.EnvList[0].Functions)
5251
return EnvList
5352
}
5453

src/utils/os-release.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const macOSMap = new Map([
2+
[19, 'Catalina'],
3+
[18, 'Mojave'],
4+
[17, 'High Sierra'],
5+
[16, 'Sierra'],
6+
[15, 'El Capitan'],
7+
[14, 'Yosemite'],
8+
[13, 'Mavericks'],
9+
[12, 'Mountain Lion'],
10+
[11, 'Lion'],
11+
[10, 'Snow Leopard'],
12+
[9, 'Leopard'],
13+
[8, 'Tiger'],
14+
[7, 'Panther'],
15+
[6, 'Jaguar'],
16+
[5, 'Puma']
17+
])
18+
19+
const winMap = new Map([
20+
['10.0', '10'],
21+
['6.3', '8.1'],
22+
['6.2', '8'],
23+
['6.1', '7'],
24+
['6.0', 'Vista'],
25+
['5.2', 'Server 2003'],
26+
['5.1', 'XP'],
27+
['5.0', '2000'],
28+
['4.9', 'ME'],
29+
['4.1', '98'],
30+
['4.0', '95']
31+
])
32+
33+
export function getPlatformRelease(platform, release) {
34+
// macOS
35+
if (platform === 'darwin') {
36+
release = Number(release.split('.')[0])
37+
const name = macOSMap.get(release)
38+
const version = '10.' + (release - 4)
39+
return `${name} ${version}`
40+
}
41+
42+
// windows
43+
if (platform === 'win32') {
44+
const version = (/\d+\.\d/.exec(release) || [])[0]
45+
46+
return `Windows ${winMap.get(version)}`
47+
}
48+
49+
// 其他 Linux
50+
return 'Linux'
51+
}

src/utils/request.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ export class CloudService {
7474
...params
7575
}
7676

77-
// console.log(_params)
78-
7977
req.deserialize(_params)
8078

8179
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)