Skip to content

Commit

Permalink
support client
Browse files Browse the repository at this point in the history
  • Loading branch information
EdamAme-x committed Oct 14, 2024
1 parent 71e4843 commit 5ecefa2
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions packages/linejs/client/clients/internal/liff-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// For Liff (liff, etc)
import { JSDOM } from "jsdom";
import type { NestedArray, ProtocolKey } from "../../libs/thrift/declares.ts";
import type { LooseType } from "../../entities/common.ts";
import { BaseClient } from "../base-client.ts";
Expand Down Expand Up @@ -94,8 +93,8 @@ export class LiffClient extends BaseClient {
if (error instanceof InternalError) {
this.log("liff-error", { ...error.data });
if (error.data.code === 3 && tryConsent) {
const data: LINETypes.LiffException =
error.data as LINETypes.LiffException;
const data: LINETypes.LiffException = error
.data as LINETypes.LiffException;
const payload = data.payload;
const consentRequired = payload.consentRequired;
const channelId = consentRequired.channelId;
Expand All @@ -104,8 +103,12 @@ export class LiffClient extends BaseClient {
let hasConsent = false;

if (channelId && consentUrl) {
if (toType === 4 || this.system?.device === "DESKTOPWIN") {
hasConsent = await this.tryConsentAuthorize(consentUrl);
if (
toType === 4 || this.system?.device === "DESKTOPWIN"
) {
hasConsent = await this.tryConsentAuthorize(
consentUrl,
);
} else {
hasConsent = await this.tryConsentLiff(channelId);
}
Expand All @@ -117,7 +120,9 @@ export class LiffClient extends BaseClient {
}
}
throw new Error(
`Failed to get LiffToken: ${liffId}${chatMid ? "@" + chatMid : ""}`,
`Failed to get LiffToken: ${liffId}${
chatMid ? "@" + chatMid : ""
}`,
);
}
}
Expand All @@ -143,7 +148,10 @@ export class LiffClient extends BaseClient {
...options,
};
if (!this.liff_token_cache[to] || forceIssue) {
token = await this.getLiffToken({ chatMid: to, liffId: this.liffId });
token = await this.getLiffToken({
chatMid: to,
liffId: this.liffId,
});
} else {
token = this.liff_token_cache[to];
}
Expand All @@ -165,7 +173,9 @@ export class LiffClient extends BaseClient {
});

if (!response.ok) {
throw new Error(`Failed to send Liff message: ${response.statusText}`);
throw new Error(
`Failed to send Liff message: ${response.statusText}`,
);
}

const responseBody = await response.json();
Expand Down Expand Up @@ -203,25 +213,27 @@ export class LiffClient extends BaseClient {
): Promise<boolean> {
const headers: Record<string, string> = {
"X-Line-Access": this.metadata?.authToken as string,
"User-Agent":
this.system?.device === "IOS"
? `Mozilla/5.0 (iPhone; CPU iPhone OS ${this.system.systemVersion} like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Safari Line/${this.system.appVersion}`
: "Mozilla/5.0 (Linux; Android 8.0.1; SAMSUNG Realise/LineJS; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/56.0.2924.87 Mobile Safari/537.36",
"User-Agent": this.system?.device === "IOS"
? `Mozilla/5.0 (iPhone; CPU iPhone OS ${this.system.systemVersion} like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Safari Line/${this.system.appVersion}`
: "Mozilla/5.0 (Linux; Android 8.0.1; SAMSUNG Realise/LineJS; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/56.0.2924.87 Mobile Safari/537.36",
"X-Line-Application": this.system?.type as string,
};

const response = await fetch(consentUrl, { method: "GET", headers });
if (response.ok) {
const text = await response.text();
const consentResponse = new JSDOM(text).dom.window.document;
const channelId =
consentResponse
.querySelector('meta[name="channelId"]')
?.getAttribute("content") ?? null;
const csrfToken =
consentResponse
.querySelector('meta[name="csrfToken"]')
?.getAttribute("content") ?? null;
const consentResponse = "DOMParser" in window
? new ((window as LooseType).DOMParser)().parseFromString(
text,
"text/html",
)
: new (await import("jsdom"))(text).dom.window.document;
const channelId = consentResponse
.querySelector('meta[name="channelId"]')
?.getAttribute("content") ?? null;
const csrfToken = consentResponse
.querySelector('meta[name="csrfToken"]')
?.getAttribute("content") ?? null;

if (channelId && csrfToken) {
const payload = new URLSearchParams({
Expand Down

0 comments on commit 5ecefa2

Please sign in to comment.