Skip to content

Commit

Permalink
ChannelAccessToken client doesn't have to require channel access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang-33 committed Nov 17, 2023
1 parent 03b1580 commit 2172d20
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{# @pebvariable name="imports" type="java.util.List<java.util.Map<String, String>>" #}
{# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" #}
{# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
{% include "./licenseInfo.pebble" %}

/* tslint:disable:no-unused-locals */
Expand All @@ -19,7 +20,9 @@ import {AxiosResponse} from "axios";

interface httpClientConfig {
baseURL?: string;
{% if authMethods != null -%}
channelAccessToken: string;
{% endif -%}
// TODO support defaultHeaders?
}

Expand All @@ -33,7 +36,9 @@ export class {{operations.classname}} {
}
this.httpClient = new HTTPClient({
defaultHeaders: {
{% if authMethods != null -%}
Authorization: "Bearer " + config.channelAccessToken,
{% endif -%}
},
responseParser: this.parseHTTPResponse.bind(this),
baseURL: config.baseURL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
{% if op.hasBodyParam %}const params = {{op.bodyParam.paramName}};
{% elseif op.hasFormParams %}const formParams = {
{% for param in op.formParams -%}
"{{param.paramName}}": {{param.paramName}},
"{{param.baseName}}": {{param.paramName}},
{% endfor %}
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === '') {
delete formParams[key];
}
});
{% elseif op.hasQueryParams %}const queryParams = {
{% for param in op.queryParams -%}
"{{param.paramName}}": {{param.paramName}},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{# @pebvariable name="imports" type="java.util.List<java.util.Map<String, String>>" #}
{# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" #}
{# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
import { {{operations.classname}} } from "../../api";

{% for import in imports -%}
Expand All @@ -21,7 +22,9 @@ describe("{{operations.classname}}", () => {
afterEach(() => { server.resetHandlers() })

const client = new {{operations.classname}}({
{% if authMethods != null -%}
channelAccessToken: channel_access_token,
{% endif -%}
});

{% for op in operations.operation %}
Expand All @@ -44,10 +47,12 @@ describe("{{operations.classname}}", () => {
({ request, params, cookies }) => {
requestCount++;

{% if authMethods != null -%}
equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
{% endif -%}
equal(
request.headers.get("User-Agent"),
`${pkg.name}/${pkg.version}`,
Expand Down
73 changes: 53 additions & 20 deletions lib/channel-access-token/api/channelAccessTokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { AxiosResponse } from "axios";

interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
}

Expand All @@ -43,9 +42,7 @@ export class ChannelAccessTokenClient {
config.baseURL = "https://api.line.me";
}
this.httpClient = new HTTPClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: {},
responseParser: this.parseHTTPResponse.bind(this),
baseURL: config.baseURL,
});
Expand Down Expand Up @@ -99,10 +96,16 @@ export class ChannelAccessTokenClient {
clientSecret?: string,
): Promise<IssueShortLivedChannelAccessTokenResponse> {
const formParams = {
grantType: grantType,
clientId: clientId,
clientSecret: clientSecret,
grant_type: grantType,
client_id: clientId,
client_secret: clientSecret,
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === "") {
delete formParams[key];
}
});

const res =
this.httpClient.postForm<IssueShortLivedChannelAccessTokenResponse>(
Expand All @@ -125,10 +128,16 @@ export class ChannelAccessTokenClient {
clientAssertion?: string,
): Promise<IssueChannelAccessTokenResponse> {
const formParams = {
grantType: grantType,
clientAssertionType: clientAssertionType,
clientAssertion: clientAssertion,
grant_type: grantType,
client_assertion_type: clientAssertionType,
client_assertion: clientAssertion,
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === "") {
delete formParams[key];
}
});

const res = this.httpClient.postForm<IssueChannelAccessTokenResponse>(
"/oauth2/v2.1/token",
Expand All @@ -154,12 +163,18 @@ export class ChannelAccessTokenClient {
clientSecret?: string,
): Promise<IssueStatelessChannelAccessTokenResponse> {
const formParams = {
grantType: grantType,
clientAssertionType: clientAssertionType,
clientAssertion: clientAssertion,
clientId: clientId,
clientSecret: clientSecret,
grant_type: grantType,
client_assertion_type: clientAssertionType,
client_assertion: clientAssertion,
client_id: clientId,
client_secret: clientSecret,
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === "") {
delete formParams[key];
}
});

const res =
this.httpClient.postForm<IssueStatelessChannelAccessTokenResponse>(
Expand All @@ -178,8 +193,14 @@ export class ChannelAccessTokenClient {
accessToken?: string,
): Promise<Types.MessageAPIResponseBase> {
const formParams = {
accessToken: accessToken,
access_token: accessToken,
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === "") {
delete formParams[key];
}
});

const res = this.httpClient.postForm("/v2/oauth/revoke", formParams);
return ensureJSON(res);
Expand All @@ -198,10 +219,16 @@ export class ChannelAccessTokenClient {
accessToken?: string,
): Promise<Types.MessageAPIResponseBase> {
const formParams = {
clientId: clientId,
clientSecret: clientSecret,
accessToken: accessToken,
client_id: clientId,
client_secret: clientSecret,
access_token: accessToken,
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === "") {
delete formParams[key];
}
});

const res = this.httpClient.postForm("/oauth2/v2.1/revoke", formParams);
return ensureJSON(res);
Expand All @@ -216,8 +243,14 @@ export class ChannelAccessTokenClient {
accessToken?: string,
): Promise<VerifyChannelAccessTokenResponse> {
const formParams = {
accessToken: accessToken,
access_token: accessToken,
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === "") {
delete formParams[key];
}
});

const res = this.httpClient.postForm<VerifyChannelAccessTokenResponse>(
"/v2/oauth/verify",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ describe("ChannelAccessTokenClient", () => {
server.resetHandlers();
});

const client = new ChannelAccessTokenClient({
channelAccessToken: channel_access_token,
});
const client = new ChannelAccessTokenClient({});

it("getsAllValidChannelAccessTokenKeyIds", async () => {
let requestCount = 0;
Expand All @@ -42,10 +40,6 @@ describe("ChannelAccessTokenClient", () => {
http.get(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand Down Expand Up @@ -74,10 +68,6 @@ describe("ChannelAccessTokenClient", () => {
http.post(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand Down Expand Up @@ -108,10 +98,6 @@ describe("ChannelAccessTokenClient", () => {
http.post(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand Down Expand Up @@ -144,10 +130,6 @@ describe("ChannelAccessTokenClient", () => {
http.post(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand Down Expand Up @@ -182,10 +164,6 @@ describe("ChannelAccessTokenClient", () => {
http.post(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand All @@ -212,10 +190,6 @@ describe("ChannelAccessTokenClient", () => {
http.post(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand Down Expand Up @@ -246,10 +220,6 @@ describe("ChannelAccessTokenClient", () => {
http.post(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand All @@ -276,10 +246,6 @@ describe("ChannelAccessTokenClient", () => {
http.get(endpoint, ({ request, params, cookies }) => {
requestCount++;

equal(
request.headers.get("Authorization"),
`Bearer ${channel_access_token}`,
);
equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`);

return HttpResponse.json({});
Expand Down
20 changes: 13 additions & 7 deletions lib/module-attach/api/lineModuleAttachClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,23 @@ export class LineModuleAttachClient {
brandType?: string,
): Promise<AttachModuleResponse> {
const formParams = {
grantType: grantType,
grant_type: grantType,
code: code,
redirectUri: redirectUri,
codeVerifier: codeVerifier,
clientId: clientId,
clientSecret: clientSecret,
redirect_uri: redirectUri,
code_verifier: codeVerifier,
client_id: clientId,
client_secret: clientSecret,
region: region,
basicSearchId: basicSearchId,
basic_search_id: basicSearchId,
scope: scope,
brandType: brandType,
brand_type: brandType,
};
// Remove properties with undefined or empty values
Object.keys(formParams).forEach((key: keyof typeof formParams) => {
if (formParams[key] === undefined || formParams[key] === "") {
delete formParams[key];
}
});

const res = this.httpClient.postForm<AttachModuleResponse>(
"/module/auth/v1/token",
Expand Down

0 comments on commit 2172d20

Please sign in to comment.