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

Commit 864f1fb

Browse files
committed
optimize login check flow
1 parent 8f57f0b commit 864f1fb

15 files changed

Lines changed: 128 additions & 51 deletions

File tree

deps/tencentcloud-sdk-nodejs/tencentcloud/scf/v20180416/models.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2762,6 +2762,7 @@ class UpdateFunctionConfigurationRequest extends AbstractModel {
27622762
*/
27632763
this.ClsTopicId = null;
27642764

2765+
this.L5Enable = null;
27652766
}
27662767

27672768
/**
@@ -2794,7 +2795,7 @@ class UpdateFunctionConfigurationRequest extends AbstractModel {
27942795
this.InstallDependency = 'InstallDependency' in params ? params.InstallDependency : null;
27952796
this.ClsLogsetId = 'ClsLogsetId' in params ? params.ClsLogsetId : null;
27962797
this.ClsTopicId = 'ClsTopicId' in params ? params.ClsTopicId : null;
2797-
2798+
this.L5Enable = 'L5Enable' in params ? params.L5Enable : null;
27982799
}
27992800
}
28002801

@@ -3507,6 +3508,8 @@ class CreateFunctionRequest extends AbstractModel {
35073508

35083509

35093510
this.CodeSecret = null;
3511+
3512+
this.L5Enable = null;
35103513
}
35113514

35123515
/**
@@ -3548,6 +3551,7 @@ class CreateFunctionRequest extends AbstractModel {
35483551
this.ClsLogsetId = 'ClsLogsetId' in params ? params.ClsLogsetId : null;
35493552
this.ClsTopicId = 'ClsTopicId' in params ? params.ClsTopicId : null;
35503553
this.CodeSecret = 'CodeSecret' in params ? params.CodeSecret : null;
3554+
this.L5Enable = 'L5Enable' in params ? params.L5Enable : null;
35513555
}
35523556
}
35533557

lib/auth/login.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,30 @@ const LoginRes = {
3939
async function loginWithToken() {
4040
const tcbrc = utils_1.getCredentialConfig();
4141
if (tcbrc.secretId && tcbrc.secretKey) {
42-
return LoginRes.SUCCESS;
42+
try {
43+
const { secretId, secretKey } = tcbrc;
44+
await checkAuth({
45+
tmpSecretId: secretId,
46+
tmpSecretKey: secretKey
47+
});
48+
return LoginRes.SUCCESS;
49+
}
50+
catch (e) {
51+
configstore_1.configStore.delete('secretId');
52+
configstore_1.configStore.delete('secretKey');
53+
}
4354
}
44-
const tmpExpired = Number(tcbrc.tmpExpired) || 0;
45-
let refreshExpired = Number(tcbrc.expired) || 0;
46-
const now = Date.now();
47-
if (now < tmpExpired) {
48-
return LoginRes.SUCCESS;
55+
if (tcbrc.refreshToken) {
56+
try {
57+
await checkAuth(tcbrc);
58+
return LoginRes.SUCCESS;
59+
}
60+
catch (e) {
61+
}
4962
}
5063
let credential;
5164
try {
52-
if (now < refreshExpired) {
53-
credential = await auth_1.refreshTmpToken(tcbrc);
54-
}
55-
else {
56-
credential = await auth_1.getAuthTokenFromWeb();
57-
}
65+
credential = await auth_1.getAuthTokenFromWeb();
5866
}
5967
catch (e) {
6068
return LoginRes.UNKNOWN_ERROR(e.message);
@@ -75,7 +83,16 @@ exports.loginWithToken = loginWithToken;
7583
async function loginWithKey(secretId, secretKey) {
7684
const tcbrc = await utils_1.getCredentialConfig();
7785
if (tcbrc.secretId && tcbrc.secretKey) {
78-
return LoginRes.SUCCESS;
86+
try {
87+
const { secretId, secretKey } = tcbrc;
88+
await checkAuth({
89+
tmpSecretId: secretId,
90+
tmpSecretKey: secretKey
91+
});
92+
return LoginRes.SUCCESS;
93+
}
94+
catch (e) {
95+
}
7996
}
8097
if (!secretId || !secretKey) {
8198
return LoginRes.INVALID_PARAM('SecretID 或 SecretKey 不能为空');

lib/auth/logout.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
const configstore_1 = require("../utils/configstore");
44
const constant_1 = require("../constant");
55
const logger_1 = require("../logger");
6+
const auth_1 = require("./auth");
7+
const utils_1 = require("../utils");
68
async function logout() {
7-
configstore_1.configStore.delete(constant_1.ConfigItems.credentail);
8-
configstore_1.configStore.delete(constant_1.ConfigItems.ssh);
9-
logger_1.successLog('注销登录成功!');
9+
const credentail = utils_1.getCredentialConfig();
10+
try {
11+
await auth_1.refreshTmpToken(Object.assign({}, credentail, { isLogout: true }));
12+
configstore_1.configStore.delete(constant_1.ConfigItems.credentail);
13+
configstore_1.configStore.delete(constant_1.ConfigItems.ssh);
14+
logger_1.successLog('注销登录成功!');
15+
}
16+
catch (e) {
17+
configstore_1.configStore.delete(constant_1.ConfigItems.credentail);
18+
configstore_1.configStore.delete(constant_1.ConfigItems.ssh);
19+
logger_1.successLog('注销登录成功!');
20+
}
1021
}
1122
exports.logout = logout;

lib/commands/login.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ async function checkLogin() {
1717
return true;
1818
}
1919
if (credential.refreshToken) {
20-
if (Date.now() < Number(credential.tmpExpired)) {
20+
try {
21+
await env_1.listEnvs();
2122
return true;
2223
}
23-
else if (Date.now() < Number(credential.expired)) {
24-
return true;
24+
catch (error) {
25+
return false;
2526
}
2627
}
2728
return false;

lib/function/create.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ async function createFunction(options) {
4646
ZipFile: base64
4747
},
4848
CodeSecret: codeSecret,
49-
MemorySize: 256
49+
MemorySize: 256,
50+
L5Enable: func.config && func.config.l5 ? 'TRUE' : null
5051
};
5152
const { config } = func;
5253
envVariables.length && (params.Environment = { Variables: envVariables });

lib/function/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ async function updateFunctionConfig(options) {
152152
Key: key,
153153
Value: config.envVariables[key]
154154
}));
155+
const l5Enable = typeof config.l5 === 'undefined' ? null : (config.l5 ? 'TRUE' : 'FALSE');
155156
const params = {
156157
FunctionName: functionName,
157-
Namespace: envId
158+
Namespace: envId,
159+
L5Enable: l5Enable
158160
};
159161
envVariables.length && (params.Environment = { Variables: envVariables });
160162
config.timeout && (params.Timeout = config.timeout);

src/auth/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function getAuthTokenFromWeb(): Promise<Credential> {
132132

133133
// 临时密钥过期后,进行续期
134134
export async function refreshTmpToken(
135-
metaData: Credential
135+
metaData: Credential & { isLogout?: boolean }
136136
): Promise<Credential> {
137137
const mac = getMacAddress()
138138
const hash = md5(mac)

src/auth/login.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getAuthTokenFromWeb, refreshTmpToken } from './auth'
1+
import { getAuthTokenFromWeb } from './auth'
22
import { getCredentialConfig, CloudService } from '../utils'
33
import { ConfigItems } from '../constant'
44
import { configStore } from '../utils/configstore'
@@ -46,27 +46,34 @@ export async function loginWithToken() {
4646
const tcbrc: Credential = getCredentialConfig()
4747
// 已有永久密钥
4848
if (tcbrc.secretId && tcbrc.secretKey) {
49-
return LoginRes.SUCCESS
49+
try {
50+
const { secretId, secretKey } = tcbrc
51+
await checkAuth({
52+
tmpSecretId: secretId,
53+
tmpSecretKey: secretKey
54+
})
55+
return LoginRes.SUCCESS
56+
} catch (e) {
57+
// 删除无效的 secret
58+
configStore.delete('secretId')
59+
configStore.delete('secretKey')
60+
}
5061
}
51-
// 如果存在临时密钥,校验临时密钥是否有效,是否需要续期
52-
const tmpExpired = Number(tcbrc.tmpExpired) || 0
53-
let refreshExpired = Number(tcbrc.expired) || 0
54-
const now = Date.now()
5562

56-
if (now < tmpExpired) {
57-
return LoginRes.SUCCESS
63+
// 校验临时密钥
64+
if (tcbrc.refreshToken) {
65+
try {
66+
await checkAuth(tcbrc)
67+
return LoginRes.SUCCESS
68+
} catch (e) {
69+
// 忽略错误,继续进行
70+
}
5871
}
5972

6073
let credential
6174

6275
try {
63-
if (now < refreshExpired) {
64-
// 临时 token 过期,自动续期
65-
credential = await refreshTmpToken(tcbrc)
66-
} else {
67-
// 通过腾讯云-云开发控制台获取授权
68-
credential = await getAuthTokenFromWeb()
69-
}
76+
credential = await getAuthTokenFromWeb()
7077
} catch (e) {
7178
return LoginRes.UNKNOWN_ERROR(e.message)
7279
}
@@ -90,7 +97,16 @@ export async function loginWithKey(secretId?: string, secretKey?: string) {
9097
const tcbrc: Credential = await getCredentialConfig()
9198
// 已有永久密钥
9299
if (tcbrc.secretId && tcbrc.secretKey) {
93-
return LoginRes.SUCCESS
100+
try {
101+
const { secretId, secretKey } = tcbrc
102+
await checkAuth({
103+
tmpSecretId: secretId,
104+
tmpSecretKey: secretKey
105+
})
106+
return LoginRes.SUCCESS
107+
} catch (e) {
108+
// 忽略错误
109+
}
94110
}
95111

96112
if (!secretId || !secretKey) {

src/auth/logout.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import { configStore } from '../utils/configstore'
22
import { ConfigItems } from '../constant'
33
import { successLog } from '../logger'
4+
import { refreshTmpToken } from './auth'
5+
import { getCredentialConfig } from '../utils'
6+
// import { CloudBaseError } from '../error'
47

58
// 注销登录信息
69
export async function logout() {
7-
configStore.delete(ConfigItems.credentail)
8-
configStore.delete(ConfigItems.ssh)
9-
successLog('注销登录成功!')
10+
const credentail = getCredentialConfig()
11+
12+
try {
13+
await refreshTmpToken({
14+
...credentail,
15+
isLogout: true
16+
})
17+
configStore.delete(ConfigItems.credentail)
18+
configStore.delete(ConfigItems.ssh)
19+
successLog('注销登录成功!')
20+
} catch (e) {
21+
configStore.delete(ConfigItems.credentail)
22+
configStore.delete(ConfigItems.ssh)
23+
// throw new CloudBaseError('云端设备登录记录删除失败,请手动删除!')
24+
successLog('注销登录成功!')
25+
}
1026
}

src/commands/login.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ async function checkLogin() {
1515
return true
1616
}
1717

18-
// 存在临时密钥信息
18+
// 尝试获取环境列表,判断是否登录
1919
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 有效期内,刷新临时密钥
20+
try {
21+
await listEnvs()
2522
return true
23+
} catch (error) {
24+
return false
2625
}
2726
}
2827

0 commit comments

Comments
 (0)