|
1 | 1 | "use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 4 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 5 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 6 | + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 7 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 8 | + }); |
| 9 | +}; |
2 | 10 | Object.defineProperty(exports, "__esModule", { value: true }); |
3 | | -const auth_1 = require("./auth"); |
4 | 11 | const utils_1 = require("../utils"); |
5 | 12 | const constant_1 = require("../constant"); |
6 | | -const configstore_1 = require("../utils/configstore"); |
7 | 13 | const tcbService = new utils_1.CloudService('tcb', '2018-06-08'); |
8 | | -async function checkAuth(credential) { |
9 | | - const { tmpSecretId, tmpSecretKey, tmpToken } = credential; |
10 | | - tcbService.setCredential(tmpSecretId, tmpSecretKey, tmpToken); |
11 | | - return await tcbService.request('DescribeEnvs'); |
| 14 | +function checkAuth(credential) { |
| 15 | + return __awaiter(this, void 0, void 0, function* () { |
| 16 | + const { tmpSecretId, tmpSecretKey, tmpToken } = credential; |
| 17 | + tcbService.setCredential(tmpSecretId, tmpSecretKey, tmpToken); |
| 18 | + return yield tcbService.request('DescribeEnvs'); |
| 19 | + }); |
12 | 20 | } |
13 | 21 | const LoginRes = { |
14 | 22 | SUCCESS: { |
@@ -36,84 +44,62 @@ const LoginRes = { |
36 | 44 | }; |
37 | 45 | } |
38 | 46 | }; |
39 | | -async function loginWithToken() { |
40 | | - const tcbrc = utils_1.getCredentialConfig(); |
41 | | - if (tcbrc.secretId && tcbrc.secretKey) { |
42 | | - try { |
43 | | - const { secretId, secretKey } = tcbrc; |
44 | | - await checkAuth({ |
45 | | - tmpSecretId: secretId, |
46 | | - tmpSecretKey: secretKey |
47 | | - }); |
| 47 | +function loginWithToken() { |
| 48 | + return __awaiter(this, void 0, void 0, function* () { |
| 49 | + const isLogin = yield utils_1.checkAndGetCredential(); |
| 50 | + if (isLogin) { |
48 | 51 | return LoginRes.SUCCESS; |
49 | 52 | } |
| 53 | + let credential; |
| 54 | + try { |
| 55 | + credential = yield utils_1.getAuthTokenFromWeb(); |
| 56 | + } |
50 | 57 | catch (e) { |
51 | | - configstore_1.configStore.delete('secretId'); |
52 | | - configstore_1.configStore.delete('secretKey'); |
| 58 | + return LoginRes.UNKNOWN_ERROR(e.message); |
| 59 | + } |
| 60 | + if (!credential.refreshToken || !credential.uin) { |
| 61 | + return LoginRes.INVALID_TOKEN; |
53 | 62 | } |
54 | | - } |
55 | | - if (tcbrc.refreshToken) { |
56 | 63 | try { |
57 | | - await checkAuth(tcbrc); |
58 | | - return LoginRes.SUCCESS; |
| 64 | + yield checkAuth(credential); |
59 | 65 | } |
60 | 66 | catch (e) { |
| 67 | + return LoginRes.UNKNOWN_ERROR(e.message); |
61 | 68 | } |
62 | | - } |
63 | | - let credential; |
64 | | - try { |
65 | | - credential = await auth_1.getAuthTokenFromWeb(); |
66 | | - } |
67 | | - catch (e) { |
68 | | - return LoginRes.UNKNOWN_ERROR(e.message); |
69 | | - } |
70 | | - if (!credential.refreshToken || !credential.uin) { |
71 | | - return LoginRes.INVALID_TOKEN; |
72 | | - } |
73 | | - try { |
74 | | - await checkAuth(credential); |
75 | | - } |
76 | | - catch (e) { |
77 | | - return LoginRes.UNKNOWN_ERROR(e.message); |
78 | | - } |
79 | | - configstore_1.configStore.set(constant_1.ConfigItems.credentail, credential); |
80 | | - return LoginRes.SUCCESS; |
| 69 | + utils_1.authStore.set(constant_1.ConfigItems.credentail, credential); |
| 70 | + return LoginRes.SUCCESS; |
| 71 | + }); |
81 | 72 | } |
82 | 73 | exports.loginWithToken = loginWithToken; |
83 | | -async function loginWithKey(secretId, secretKey) { |
84 | | - const tcbrc = await utils_1.getCredentialConfig(); |
85 | | - if (tcbrc.secretId && tcbrc.secretKey) { |
86 | | - try { |
87 | | - const { secretId, secretKey } = tcbrc; |
88 | | - await checkAuth({ |
89 | | - tmpSecretId: secretId, |
90 | | - tmpSecretKey: secretKey |
91 | | - }); |
| 74 | +function loginWithKey(secretId, secretKey) { |
| 75 | + return __awaiter(this, void 0, void 0, function* () { |
| 76 | + const hasLogin = yield utils_1.checkAndGetCredential(); |
| 77 | + if (hasLogin) { |
92 | 78 | return LoginRes.SUCCESS; |
93 | 79 | } |
| 80 | + if (!secretId || !secretKey) { |
| 81 | + return LoginRes.INVALID_PARAM('SecretID 或 SecretKey 不能为空'); |
| 82 | + } |
| 83 | + try { |
| 84 | + yield checkAuth({ tmpSecretId: secretId, tmpSecretKey: secretKey }); |
| 85 | + } |
94 | 86 | catch (e) { |
| 87 | + return LoginRes.CHECK_LOGIN_FAILED; |
95 | 88 | } |
96 | | - } |
97 | | - if (!secretId || !secretKey) { |
98 | | - return LoginRes.INVALID_PARAM('SecretID 或 SecretKey 不能为空'); |
99 | | - } |
100 | | - try { |
101 | | - await checkAuth({ tmpSecretId: secretId, tmpSecretKey: secretKey }); |
102 | | - } |
103 | | - catch (e) { |
104 | | - return LoginRes.CHECK_LOGIN_FAILED; |
105 | | - } |
106 | | - configstore_1.configStore.set(constant_1.ConfigItems.credentail, { secretId, secretKey }); |
107 | | - return LoginRes.SUCCESS; |
| 89 | + utils_1.authStore.set(constant_1.ConfigItems.credentail, { secretId, secretKey }); |
| 90 | + return LoginRes.SUCCESS; |
| 91 | + }); |
108 | 92 | } |
109 | 93 | exports.loginWithKey = loginWithKey; |
110 | | -async function login(options) { |
111 | | - if (options && options.key) { |
112 | | - const { secretId, secretKey } = options; |
113 | | - return await loginWithKey(secretId, secretKey); |
114 | | - } |
115 | | - else { |
116 | | - return await loginWithToken(); |
117 | | - } |
| 94 | +function login(options) { |
| 95 | + return __awaiter(this, void 0, void 0, function* () { |
| 96 | + if (options && options.key) { |
| 97 | + const { secretId, secretKey } = options; |
| 98 | + return yield loginWithKey(secretId, secretKey); |
| 99 | + } |
| 100 | + else { |
| 101 | + return yield loginWithToken(); |
| 102 | + } |
| 103 | + }); |
118 | 104 | } |
119 | 105 | exports.login = login; |
0 commit comments