Skip to content

Commit 7624784

Browse files
authored
Merge pull request #1 from macropygia/feature/mixed
Rename getPlugin to getEndpoints
2 parents b695e3e + a03efdf commit 7624784

26 files changed

+127
-94
lines changed

README.ja.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ const rp = await OidcClient.create({
4040
client_secret: "client-secret",
4141
},
4242
});
43-
const plugin = rp.getPlugin();
43+
const endpoints = rp.getEndpoints();
4444
const hook = rp.getAuthHook();
4545

4646
console.log(rp.issuerMetadata);
4747

4848
new Elysia()
49-
.use(plugin)
49+
.use(endpoints)
5050
.guard((app) =>
5151
app
5252
.use(hook)
@@ -200,7 +200,7 @@ const client = await OidcClient.create({
200200

201201
```typescript
202202
const rp = await OidcClient.create({ ... });
203-
const plugin = rp.getPlugin();
203+
const endpoints = rp.getEndpoints();
204204
const hook = rp.getAuthHook({
205205
scope: "scoped",
206206
loginRedirectUrl: "/auth/login",
@@ -209,7 +209,7 @@ const hook = rp.getAuthHook({
209209
});
210210

211211
new Elysia()
212-
.use(plugin)
212+
.use(endpoints)
213213
.guard((app) =>
214214
app
215215
.use(hook)

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ const rp = await OidcClient.create({
4040
client_secret: "client-secret",
4141
},
4242
});
43-
const plugin = rp.getPlugin();
43+
const endpoints = rp.getEndpoints();
4444
const hook = rp.getAuthHook();
4545

4646
console.log(rp.issuerMetadata);
4747

4848
new Elysia()
49-
.use(plugin)
49+
.use(endpoints)
5050
.guard((app) =>
5151
app
5252
.use(hook)
@@ -200,7 +200,7 @@ Determine the validity of the session in `onBeforeHook`, and return `sessionStat
200200

201201
```typescript
202202
const rp = await OidcClient.create({ ... });
203-
const plugin = rp.getPlugin();
203+
const endpoints = rp.getEndpoints();
204204
const hook = rp.getAuthHook({
205205
scope: "scoped",
206206
loginRedirectUrl: "/auth/login",
@@ -209,7 +209,7 @@ const hook = rp.getAuthHook({
209209
});
210210

211211
new Elysia()
212-
.use(plugin)
212+
.use(endpoints)
213213
.guard((app) =>
214214
app
215215
.use(hook)

__test__/const.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ export const baseMockClient = {
117117
getSessionIdCookieType: mock(),
118118
getCookieDefinition: mock(),
119119
getAuthHook: mock(),
120-
getPlugin: mock(),
121-
getClaims: mock().mockReturnValue(mockClaims),
120+
getEndpoints: mock(),
121+
getClaimsFromIdToken: mock().mockReturnValue(mockClaims),
122122
sessionToStatus: mock().mockReturnValue(mockStatus),
123123
logger,
124124
} as DeepPartial<OidcClient> as OidcClient;

__test__/general.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ describe("Integration/general", async () => {
3131
},
3232
},
3333
});
34-
const plugin = oidcClient.getPlugin();
34+
const endpoints = oidcClient.getEndpoints();
3535
const app = new Elysia()
3636
.get("/", () => "home")
37-
.use(plugin)
37+
.use(endpoints)
3838
.listen(rpPort);
3939
const {
4040
// clientMetadata,

core/OidcClient.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { createSession } from "@/functions/createSession";
22
import { deleteSession } from "@/functions/deleteSession";
33
import { fetchSession } from "@/functions/fetchSession";
44
import { getAuthHook } from "@/functions/getAuthHook";
5-
import { getClaims } from "@/functions/getClaims";
6-
import { getPlugin } from "@/functions/getPlugin";
5+
import { getClaimsFromIdToken } from "@/functions/getClaimsFromIdToken";
6+
import { getEndpoints } from "@/functions/getEndpoints";
77
import { sessionToStatus } from "@/functions/sessionToStatus";
88
import { updateSession } from "@/functions/updateSession";
99
import type {
@@ -19,7 +19,7 @@ import { BaseOidcClient } from "./BaseOidcClient";
1919
* OpenID Connect client plugin for ElysiaJS
2020
* - Usage:
2121
* - `const client = await BaseOidcClient.create(options);`
22-
* - `const plugin = client.getPlugin();`
22+
* - `const endpoints = client.getEndpoints();`
2323
* - `const hook = client.getAuthHook();`
2424
*/
2525
export class OidcClient extends BaseOidcClient {
@@ -101,14 +101,15 @@ export class OidcClient extends BaseOidcClient {
101101
* @public
102102
* @returns ElysiaJS Plugin
103103
*/
104-
public getPlugin = () => getPlugin.call(this);
104+
public getEndpoints = () => getEndpoints.call(this);
105105

106106
/**
107107
* Get claims by id_token
108108
* @param idToken ID Token
109109
* @public
110110
*/
111-
public getClaims = (idToken: string) => getClaims.call(this, idToken);
111+
public getClaimsFromIdToken = (idToken: string) =>
112+
getClaimsFromIdToken.call(this, idToken);
112113

113114
/**
114115
* Convert session data to session status

core/deleteCookie.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, mock, test } from "bun:test";
2+
import { type DeepPartial, baseMockClient, mockCookie } from "@/__test__/const";
3+
import type { OidcClient } from "@/core/OidcClient";
4+
import { defaultCookieSettings } from "./const";
5+
import { deleteCookie } from "./deleteCookie";
6+
7+
describe("Unit/core/deleteCookie", () => {
8+
test("Default", () => {
9+
const ms = {
10+
...baseMockClient,
11+
logger: {
12+
trace: mock(),
13+
debug: mock(),
14+
},
15+
} as DeepPartial<OidcClient> as OidcClient;
16+
17+
deleteCookie(ms, mockCookie);
18+
19+
expect(ms.logger?.trace).toHaveBeenCalled();
20+
expect(ms.logger?.debug).toHaveBeenCalled();
21+
expect(
22+
mockCookie[defaultCookieSettings.sessionIdName].update,
23+
).toHaveBeenCalled();
24+
});
25+
});

endpoints/callback.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Elysia from "elysia";
1313
import { callback } from "./callback";
1414

1515
describe("Unit/endpoints/callback", () => {
16-
const plugin = callback;
16+
const endpoints = callback;
1717
const responseBody = {
1818
expired: mock().mockReturnValue(false),
1919
};
@@ -35,7 +35,7 @@ describe("Unit/endpoints/callback", () => {
3535
);
3636

3737
test("Succeeded", async () => {
38-
const app = new Elysia().use(plugin.call(mockClient(mockAuthSession)));
38+
const app = new Elysia().use(endpoints.call(mockClient(mockAuthSession)));
3939

4040
const response = await app.handle(new Request(`http://localhost${path}`));
4141
logger?.info(response);
@@ -46,7 +46,7 @@ describe("Unit/endpoints/callback", () => {
4646
});
4747

4848
test("Session data missing", async () => {
49-
const app = new Elysia().use(plugin.call(mockClient(null)));
49+
const app = new Elysia().use(endpoints.call(mockClient(null)));
5050

5151
const response = await app.handle(new Request(`http://localhost${path}`));
5252
expect(response.status).toBe(401);
@@ -69,7 +69,7 @@ describe("Unit/endpoints/callback", () => {
6969
state: "mock-state",
7070
},
7171
])("`codeVerifier` hash missing", async (session) => {
72-
const app = new Elysia().use(plugin.call(mockClient(session)));
72+
const app = new Elysia().use(endpoints.call(mockClient(session)));
7373

7474
const response = await app.handle(new Request(`http://localhost${path}`));
7575
expect(response.status).toBe(401);
@@ -78,7 +78,7 @@ describe("Unit/endpoints/callback", () => {
7878
test("Update failed", async () => {
7979
const mc = mockClient(mockAuthSession);
8080
mc.updateSession = mock().mockReturnValue(null);
81-
const app = new Elysia().use(plugin.call(mc));
81+
const app = new Elysia().use(endpoints.call(mc));
8282

8383
const response = await app.handle(new Request(`http://localhost${path}`));
8484
logger?.info(response);

endpoints/claims.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ import type { IdTokenClaims } from "openid-client";
1515
import { claims } from "./claims";
1616

1717
describe("Unit/endpoints/claims", () => {
18-
const plugin = claims;
18+
const endpoints = claims;
1919
const path = defaultSettings.claimsPath;
2020
const mockClient = mock(
2121
(session: OIDCClientActiveSession | null, claims: IdTokenClaims | null) =>
2222
({
2323
...baseMockClient,
2424
fetchSession: mock().mockReturnValue(session),
25-
getClaims: mock().mockReturnValue(claims),
25+
getClaimsFromIdToken: mock().mockReturnValue(claims),
2626
logger,
2727
}) as DeepPartial<OidcClient> as OidcClient,
2828
);
2929

3030
it("Succeeded", async () => {
3131
const app = new Elysia().use(
32-
plugin.call(mockClient(mockActiveSession, mockIdTokenClaims)),
32+
endpoints.call(mockClient(mockActiveSession, mockIdTokenClaims)),
3333
);
3434

3535
const response = await app
@@ -40,7 +40,7 @@ describe("Unit/endpoints/claims", () => {
4040
});
4141

4242
it("Session does not exist", async () => {
43-
const app = new Elysia().use(plugin.call(mockClient(null, null)));
43+
const app = new Elysia().use(endpoints.call(mockClient(null, null)));
4444

4545
const response = await app
4646
.handle(new Request(`http://localhost${path}`, postInit))

endpoints/claims.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function claims(this: OidcClient) {
2929
}
3030

3131
const { idToken } = currentSession;
32-
const claims = this.getClaims(idToken);
32+
const claims = this.getClaimsFromIdToken(idToken);
3333

3434
set.headers["Content-Type"] = "application/json";
3535
return claims;

endpoints/introspect.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Elysia from "elysia";
1212
import { introspect } from "./introspect";
1313

1414
describe("Unit/endpoints/introspect", () => {
15-
const plugin = introspect;
15+
const endpoints = introspect;
1616
const responseBody = { type: "introspect" };
1717
const path = defaultSettings.introspectPath;
1818
const mockClient = mock(
@@ -29,7 +29,7 @@ describe("Unit/endpoints/introspect", () => {
2929

3030
test("Succeeded", async () => {
3131
const app = new Elysia().use(
32-
plugin.call(mockClient({ sessionId: "mock-sid" })),
32+
endpoints.call(mockClient({ sessionId: "mock-sid" })),
3333
);
3434

3535
const response = await app.handle(
@@ -41,7 +41,7 @@ describe("Unit/endpoints/introspect", () => {
4141
});
4242

4343
test("Failed", async () => {
44-
const app = new Elysia().use(plugin.call(mockClient(null)));
44+
const app = new Elysia().use(endpoints.call(mockClient(null)));
4545

4646
const response = await app.handle(
4747
new Request(`http://localhost${path}`, getInit),

endpoints/login.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ describe("Unit/endpoints/login", () => {
1212
} as DeepPartial<OidcClient> as OidcClient;
1313

1414
test("Succeeded", async () => {
15-
const plugin = login.call(mc);
16-
const app = new Elysia().use(plugin);
15+
const endpoints = login.call(mc);
16+
const app = new Elysia().use(endpoints);
1717

1818
const response = await app.handle(new Request("http://localhost/login"));
1919

@@ -40,8 +40,8 @@ describe("Unit/endpoints/login", () => {
4040
}),
4141
} as DeepPartial<OidcClient> as OidcClient;
4242

43-
const plugin = login.call(mc);
44-
const app = new Elysia().use(plugin);
43+
const endpoints = login.call(mc);
44+
const app = new Elysia().use(endpoints);
4545

4646
const response = await app.handle(new Request("http://localhost/login"));
4747

endpoints/logout.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ describe("Unit/endpoints/logout", () => {
2424
);
2525

2626
test("Succeeded", async () => {
27-
const plugin = logout.call(mockClient({ sessionId: "mock-sid" }));
28-
const app = new Elysia().use(plugin);
27+
const endpoints = logout.call(mockClient({ sessionId: "mock-sid" }));
28+
const app = new Elysia().use(endpoints);
2929

3030
const response = await app.handle(new Request("http://localhost/logout"));
3131
expect(response.status).toBe(303);
3232
expect(response.headers.get("location")).toBe(redirectPath);
3333
});
3434

3535
test("Failed", async () => {
36-
const plugin = logout.call(mockClient(null));
37-
const app = new Elysia().use(plugin);
36+
const endpoints = logout.call(mockClient(null));
37+
const app = new Elysia().use(endpoints);
3838

3939
const response = await app.handle(new Request("http://localhost/logout"));
4040
expect(response.status).toBe(401);

endpoints/refresh.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Elysia from "elysia";
1212
import { refresh } from "./refresh";
1313

1414
describe("Unit/endpoints/refresh", () => {
15-
const plugin = refresh;
15+
const endpoints = refresh;
1616
const responseBody = {
1717
id_token: "mock-id-token",
1818
access_token: "mock-access-token",
@@ -38,7 +38,7 @@ describe("Unit/endpoints/refresh", () => {
3838
sessionId: "mock-sid",
3939
refreshToken: "mock-refresh-token",
4040
};
41-
const app = new Elysia().use(plugin.call(mockClient(session, true)));
41+
const app = new Elysia().use(endpoints.call(mockClient(session, true)));
4242

4343
const response = await app.handle(
4444
new Request(`http://localhost${path}`, getInit),
@@ -49,7 +49,7 @@ describe("Unit/endpoints/refresh", () => {
4949
});
5050

5151
test("Session missing", async () => {
52-
const app = new Elysia().use(plugin.call(mockClient(null, true)));
52+
const app = new Elysia().use(endpoints.call(mockClient(null, true)));
5353

5454
const response = await app.handle(
5555
new Request(`http://localhost${path}`, getInit),
@@ -59,7 +59,7 @@ describe("Unit/endpoints/refresh", () => {
5959

6060
test("Refresh token missing", async () => {
6161
const session = { sessionId: "mock-sid" };
62-
const app = new Elysia().use(plugin.call(mockClient(session, true)));
62+
const app = new Elysia().use(endpoints.call(mockClient(session, true)));
6363

6464
const response = await app.handle(
6565
new Request(`http://localhost${path}`, getInit),
@@ -72,7 +72,7 @@ describe("Unit/endpoints/refresh", () => {
7272
sessionId: "mock-sid",
7373
refreshToken: "mock-refresh-token",
7474
};
75-
const app = new Elysia().use(plugin.call(mockClient(session, false)));
75+
const app = new Elysia().use(endpoints.call(mockClient(session, false)));
7676

7777
const response = await app.handle(
7878
new Request(`http://localhost${path}`, getInit),

endpoints/revoke.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Elysia from "elysia";
1212
import { revoke } from "./revoke";
1313

1414
describe("Unit/endpoints/revoke", () => {
15-
const plugin = revoke;
15+
const endpoints = revoke;
1616
const responseBody = { type: "revoke" };
1717
const path = defaultSettings.revokePath;
1818
const mockClient = mock(
@@ -29,7 +29,7 @@ describe("Unit/endpoints/revoke", () => {
2929

3030
test("Succeeded", async () => {
3131
const app = new Elysia().use(
32-
plugin.call(mockClient({ sessionId: "mock-sid" })),
32+
endpoints.call(mockClient({ sessionId: "mock-sid" })),
3333
);
3434

3535
const response = await app.handle(
@@ -39,7 +39,7 @@ describe("Unit/endpoints/revoke", () => {
3939
});
4040

4141
test("Failed", async () => {
42-
const app = new Elysia().use(plugin.call(mockClient(null)));
42+
const app = new Elysia().use(endpoints.call(mockClient(null)));
4343

4444
const response = await app.handle(
4545
new Request(`http://localhost${path}`, getInit),

0 commit comments

Comments
 (0)