Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a default client as a temporary solution #9

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/demo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @authhero/demo

## 0.4.0

### Minor Changes

- add a fallback client as a temporary solution

### Patch Changes

- Updated dependencies
- [email protected]

## 0.3.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@authhero/demo",
"private": true,
"version": "0.3.0",
"version": "0.4.0",
"scripts": {
"dev": "bun --watch src/bun.ts"
},
Expand All @@ -10,7 +10,7 @@
"@hono/swagger-ui": "^0.4.1",
"@hono/zod-openapi": "^0.18.0",
"@peculiar/x509": "^1.12.3",
"authhero": "^0.17.0",
"authhero": "^0.19.0",
"hono": "^4.6.11",
"hono-openapi-middlewares": "^1.0.11",
"kysely-bun-sqlite": "^0.3.2",
Expand Down
6 changes: 6 additions & 0 deletions packages/authhero/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# authhero

## 0.19.0

### Minor Changes

- add a fallback client as a temporary solution

## 0.18.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/authhero/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "authhero",
"version": "0.18.0",
"version": "0.19.0",
"files": [
"dist"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Bindings, Variables } from "../types";
import { computeCodeChallenge } from "../utils/crypto";
import { SILENT_AUTH_MAX_AGE, SILENT_COOKIE_NAME } from "../constants";
import { serializeCookie } from "oslo/cookie";
import { getClient } from "../helpers/client";

export const authorizationCodeGrantParamsSchema = z
.object({
Expand Down Expand Up @@ -39,11 +40,7 @@ export async function authorizationCodeGrant(
ctx: Context<{ Bindings: Bindings; Variables: Variables }>,
params: AuthorizationCodeGrantTypeParams,
) {
const client = await ctx.env.data.clients.get(params.client_id);

if (!client) {
throw new HTTPException(403, { message: "Invalid client credentials" });
}
const client = await getClient(ctx.env, params.client_id);

const code = await ctx.env.data.codes.get(
client.tenant.id,
Expand Down
62 changes: 62 additions & 0 deletions packages/authhero/src/helpers/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { HTTPException } from "hono/http-exception";
import { Client, connectionSchema } from "authhero";
import { Bindings } from "../types";

export async function getClient(
env: Bindings,
clientId: string,
): Promise<Client> {
const client = await env.data.clients.get(clientId);
if (!client) {
throw new HTTPException(403, { message: "Client not found" });
}

// Temporarily assume that this is the default client.
const defaultClient = await env.data.clients.get("DEFAULT_CLIENT");
markusahlstrand marked this conversation as resolved.
Show resolved Hide resolved

const connections = defaultClient
? client.connections
.map((connection) => {
const defaultConnection = defaultClient?.connections?.find(
(c) => c.name === connection.name,
);

const mergedConnection = connectionSchema.parse({
...(defaultConnection || {}),
...connection,
options: {
...(defaultConnection?.options || {}),
...connection.options,
},
});

return mergedConnection;
})
.filter((c) => c)
: client.connections;

return {
...client,
web_origins: [
...(defaultClient?.web_origins || []),
...(client.web_origins || []),
`${env.ISSUER}u/login`,
markusahlstrand marked this conversation as resolved.
Show resolved Hide resolved
],
allowed_logout_urls: [
...(defaultClient?.allowed_logout_urls || []),
...(client.allowed_logout_urls || []),
env.ISSUER,
],
callbacks: [
...(defaultClient?.callbacks || []),
...(client.callbacks || []),
`${env.ISSUER}u/info`,
],
connections,
domains: [...(client.domains || []), ...(defaultClient?.domains || [])],
tenant: {
...(defaultClient?.tenant || {}),
...client.tenant,
},
};
}
2 changes: 1 addition & 1 deletion packages/authhero/test/routes/oauth2/token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe("token", () => {
expect(response.status).toBe(403);
const body = await response.text();

expect(body).toBe("Invalid client credentials");
expect(body).toBe("Client not found");
});

it("should return a 403 if the code is expired", async () => {
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.