@lark-project/meegle 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/meegle.js +56 -27
- package/package.json +1 -1
package/dist/bin/meegle.js
CHANGED
|
@@ -738,45 +738,57 @@ async function startDeviceCodeInit(host, customHeaders) {
|
|
|
738
738
|
client_id: client.client_id
|
|
739
739
|
};
|
|
740
740
|
}
|
|
741
|
-
async function
|
|
741
|
+
async function pollDeviceCodeOnce(options) {
|
|
742
742
|
const metadata = await fetchOAuthMetadata(options.host, options.customHeaders);
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
if (data.access_token) {
|
|
759
|
-
return {
|
|
743
|
+
const tokenRes = await fetch(metadata.token_endpoint, {
|
|
744
|
+
method: "POST",
|
|
745
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded", ...options.customHeaders },
|
|
746
|
+
body: new URLSearchParams({
|
|
747
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
748
|
+
device_code: options.device_code,
|
|
749
|
+
client_id: options.client_id
|
|
750
|
+
})
|
|
751
|
+
});
|
|
752
|
+
const tokenRaw = await tokenRes.json();
|
|
753
|
+
const data = tokenRaw.access_token || tokenRaw.error ? tokenRaw : tokenRaw.data ?? tokenRaw;
|
|
754
|
+
if (data.access_token) {
|
|
755
|
+
return {
|
|
756
|
+
status: "ok",
|
|
757
|
+
token_data: {
|
|
760
758
|
accessToken: data.access_token,
|
|
761
759
|
refreshToken: data.refresh_token,
|
|
762
760
|
clientId: options.client_id,
|
|
763
761
|
expiresAt: data.expires_in ? Date.now() + data.expires_in * 1e3 : void 0
|
|
764
|
-
}
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
if (data.error === "slow_down") return { status: "slow_down" };
|
|
766
|
+
if (data.error === "authorization_pending") return { status: "authorization_pending" };
|
|
767
|
+
if (data.error === "expired_token") return { status: "expired_token" };
|
|
768
|
+
throw new ServerError(
|
|
769
|
+
`Device Code \u6388\u6743\u5931\u8D25: ${data.error ?? "\u672A\u77E5\u9519\u8BEF"}`,
|
|
770
|
+
"DEVICE_CODE_FAILED"
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
async function startDeviceCodePoll(options) {
|
|
774
|
+
let interval = options.interval * 1e3;
|
|
775
|
+
const deadline = Date.now() + options.expires_in * 1e3;
|
|
776
|
+
while (Date.now() < deadline) {
|
|
777
|
+
await sleep(interval);
|
|
778
|
+
const result = await pollDeviceCodeOnce(options);
|
|
779
|
+
if (result.status === "ok" && result.token_data) {
|
|
780
|
+
return result.token_data;
|
|
765
781
|
}
|
|
766
|
-
if (
|
|
782
|
+
if (result.status === "slow_down") {
|
|
767
783
|
interval += 5e3;
|
|
768
784
|
continue;
|
|
769
785
|
}
|
|
770
|
-
if (
|
|
786
|
+
if (result.status === "authorization_pending") {
|
|
771
787
|
continue;
|
|
772
788
|
}
|
|
773
|
-
if (
|
|
789
|
+
if (result.status === "expired_token") {
|
|
774
790
|
throw new ClientError("\u6388\u6743\u5DF2\u8D85\u65F6\uFF0C\u8BF7\u91CD\u65B0\u6267\u884C meegle auth login --device-code", "DEVICE_CODE_EXPIRED");
|
|
775
791
|
}
|
|
776
|
-
throw new ServerError(
|
|
777
|
-
`Device Code \u6388\u6743\u5931\u8D25: ${data.error ?? "\u672A\u77E5\u9519\u8BEF"}`,
|
|
778
|
-
"DEVICE_CODE_FAILED"
|
|
779
|
-
);
|
|
780
792
|
}
|
|
781
793
|
throw new ClientError("\u6388\u6743\u5DF2\u8D85\u65F6\uFF0C\u8BF7\u91CD\u65B0\u6267\u884C meegle auth login --device-code", "DEVICE_CODE_EXPIRED");
|
|
782
794
|
}
|
|
@@ -957,7 +969,7 @@ async function resolveHost(profile, hostFlag) {
|
|
|
957
969
|
return host;
|
|
958
970
|
}
|
|
959
971
|
function registerLogin(auth) {
|
|
960
|
-
auth.command("login").description("\u767B\u5F55\u98DE\u4E66\u9879\u76EE").option("--device-code", "\u4F7F\u7528 Device Code \u6D41\u7A0B\uFF08\u65E0\u6D4F\u89C8\u5668\u73AF\u5883\uFF09").option("--phase <phase>", "Device Code \u9636\u6BB5: init \u6216 poll").option("--host <host>", "\u6307\u5B9A\u7AD9\u70B9\u57DF\u540D\uFF08\u8DF3\u8FC7\u4EA4\u4E92\u5F0F\u9009\u62E9\uFF09").option("--device-code-value <code>", "poll \u9636\u6BB5\u7684 device_code").option("--client-id <id>", "poll \u9636\u6BB5\u7684 client_id").option("--interval <seconds>", "poll \u8F6E\u8BE2\u95F4\u9694\uFF08\u79D2\uFF09", "5").option("--expires-in <seconds>", "poll \u8D85\u65F6\u65F6\u95F4\uFF08\u79D2\uFF09", "600").option("--format <format>", "\u8F93\u51FA\u683C\u5F0F: json, table, ndjson", "table").action(async (options) => {
|
|
972
|
+
auth.command("login").description("\u767B\u5F55\u98DE\u4E66\u9879\u76EE").option("--device-code", "\u4F7F\u7528 Device Code \u6D41\u7A0B\uFF08\u65E0\u6D4F\u89C8\u5668\u73AF\u5883\uFF09").option("--phase <phase>", "Device Code \u9636\u6BB5: init \u6216 poll").option("--host <host>", "\u6307\u5B9A\u7AD9\u70B9\u57DF\u540D\uFF08\u8DF3\u8FC7\u4EA4\u4E92\u5F0F\u9009\u62E9\uFF09").option("--device-code-value <code>", "poll \u9636\u6BB5\u7684 device_code").option("--client-id <id>", "poll \u9636\u6BB5\u7684 client_id").option("--interval <seconds>", "poll \u8F6E\u8BE2\u95F4\u9694\uFF08\u79D2\uFF09", "5").option("--expires-in <seconds>", "poll \u8D85\u65F6\u65F6\u95F4\uFF08\u79D2\uFF09", "600").option("--once", "poll \u9636\u6BB5\u5355\u6B21\u5C1D\u8BD5\uFF0C\u7ACB\u5373\u8FD4\u56DE\u7ED3\u679C\uFF08\u4E0D\u963B\u585E\uFF09").option("--format <format>", "\u8F93\u51FA\u683C\u5F0F: json, table, ndjson", "table").action(async (options) => {
|
|
961
973
|
const profileName = auth.parent?.opts().profile ?? await getCurrentProfileName();
|
|
962
974
|
const format = validateFormat(options.format);
|
|
963
975
|
const config = await loadConfig(profileName);
|
|
@@ -978,6 +990,23 @@ function registerLogin(auth) {
|
|
|
978
990
|
throw new ClientError("--phase poll \u9700\u8981 --device-code-value \u548C --client-id", "MISSING_OPTION");
|
|
979
991
|
}
|
|
980
992
|
const host2 = await resolveHost(profileName, options.host);
|
|
993
|
+
if (options.once) {
|
|
994
|
+
const result = await pollDeviceCodeOnce({
|
|
995
|
+
host: host2,
|
|
996
|
+
device_code: options.deviceCodeValue,
|
|
997
|
+
client_id: options.clientId,
|
|
998
|
+
customHeaders: config.headers
|
|
999
|
+
});
|
|
1000
|
+
if (result.status === "ok" && result.token_data) {
|
|
1001
|
+
const store3 = await createTokenStore(profileName);
|
|
1002
|
+
const tokenManager3 = new TokenManager(store3, host2);
|
|
1003
|
+
await tokenManager3.saveToken(result.token_data);
|
|
1004
|
+
console.log(formatOutput({ status: "ok", message: "\u767B\u5F55\u6210\u529F" }, format));
|
|
1005
|
+
} else {
|
|
1006
|
+
console.log(formatOutput({ status: result.status }, format));
|
|
1007
|
+
}
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
981
1010
|
const tokenData2 = await startDeviceCodePoll({
|
|
982
1011
|
host: host2,
|
|
983
1012
|
device_code: options.deviceCodeValue,
|