Skip to content

Commit

Permalink
Fix circular dep in models for authorization (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor-K authored Jul 9, 2024
1 parent ffb8d4e commit b92c8b5
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
ClientReadModel,
KeyReadModel,
} from "../read-models/authorizationReadModel.js";
import { Client } from "./client.js";
import { Key } from "./key.js";
import { Client, Key } from "./client.js";

export const toReadModelKey = (key: Key): KeyReadModel => ({
...key,
Expand Down
72 changes: 71 additions & 1 deletion packages/models/src/authorization/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import { z } from "zod";
import { ClientId, PurposeId, TenantId, UserId } from "../brandedIds.js";
import { Key } from "./key.js";

export const keyUse = {
sig: "Sig",
enc: "Enc",
} as const;
export const KeyUse = z.enum([
Object.values(keyUse)[0],
...Object.values(keyUse).slice(1),
]);
export type KeyUse = z.infer<typeof KeyUse>;

export const Key = z.object({
clientId: ClientId,
userId: UserId,
kid: z.string(),
name: z.string(),
encodedPem: z.string(),
algorithm: z.string(),
use: KeyUse,
createdAt: z.coerce.date(),
});

export type Key = z.infer<typeof Key>;

export const clientKind = {
consumer: "Consumer",
Expand All @@ -25,3 +47,51 @@ export const Client = z.object({
});

export type Client = z.infer<typeof Client>;

export const KeyWithClient = z.object({
JWKKey: z.object({
kty: z.string(),
keyOps: z.array(z.string()).optional(),
use: z.string().optional(),
alg: z.string().optional(),
kid: z.string(),
x5u: z.string().optional(),
x5t: z.string().optional(),
x5tS256: z.string().optional(),
x5c: z.array(z.string()).optional(),
crv: z.string().optional(),
x: z.string().optional(),
y: z.string().optional(),
d: z.string().optional(),
k: z.string().optional(),
n: z.string().optional(),
e: z.string().optional(),
p: z.string().optional(),
q: z.string().optional(),
dp: z.string().optional(),
dq: z.string().optional(),
qi: z.string().optional(),
oth: z
.array(
z.object({
r: z.string(),
d: z.string(),
t: z.string(),
})
)
.optional(),
}),
client: z.object({
id: ClientId,
consumerId: TenantId,
name: z.string(),
purposes: z.array(PurposeId),
description: z.string().optional(),
users: z.array(UserId),
kind: ClientKind,
createdAt: z.coerce.date(),
keys: z.array(Key),
}),
});

export type KeyWithClient = z.infer<typeof KeyWithClient>;
74 changes: 0 additions & 74 deletions packages/models/src/authorization/key.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ClientKindV1, ClientV1 } from "../gen/v1/authorization/client.js";
import { KeyUseV1, KeyV1 } from "../gen/v1/authorization/key.js";
import { bigIntToDate } from "../utils.js";
import { Client, ClientKind, clientKind } from "./client.js";
import { Key, KeyUse, keyUse } from "./key.js";
import { Key, KeyUse, keyUse } from "./client.js";

const fromKeyUseV1 = (input: KeyUseV1): KeyUse => {
switch (input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ClientKindV2, ClientV2 } from "../gen/v2/authorization/client.js";
import { KeyUseV2, KeyV2 } from "../gen/v2/authorization/key.js";
import { bigIntToDate } from "../utils.js";
import { Client, ClientKind, clientKind } from "./client.js";
import { Key, KeyUse, keyUse } from "./key.js";
import { Key, KeyUse, keyUse } from "./client.js";

const fromKeyUseV2 = (input: KeyUseV2): KeyUse => {
switch (input) {
Expand Down
2 changes: 1 addition & 1 deletion packages/models/src/authorization/protobufConverterToV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../gen/v1/authorization/client.js";
import { dateToBigInt } from "../utils.js";
import { PurposeId, generateId } from "../brandedIds.js";
import { Key, KeyUse, keyUse } from "./key.js";
import { Key, KeyUse, keyUse } from "./client.js";
import { Client, ClientKind, clientKind } from "./client.js";

const clientComponentState = {
Expand Down
2 changes: 1 addition & 1 deletion packages/models/src/authorization/protobufConverterToV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { match } from "ts-pattern";
import { dateToBigInt } from "../utils.js";
import { ClientKindV2, ClientV2 } from "../gen/v2/authorization/client.js";
import { KeyUseV2, KeyV2 } from "../gen/v2/authorization/key.js";
import { Key, KeyUse, keyUse } from "./key.js";
import { Key, KeyUse, keyUse } from "./client.js";
import { Client, ClientKind, clientKind } from "./client.js";

const toKeyUseV2 = (input: KeyUse): KeyUseV2 =>
Expand Down
1 change: 0 additions & 1 deletion packages/models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export * from "./purpose/protobufConverterFromV2.js";
export * from "./purpose/protobufConverterToV2.js";

export * from "./authorization/client.js";
export * from "./authorization/key.js";
export * from "./authorization/authorizationEvents.js";
export * from "./authorization/protobufConverterFromV1.js";
export * from "./authorization/protobufConverterToV1.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/models/src/read-models/authorizationReadModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";
import { Client } from "../authorization/client.js";
import { Key } from "../authorization/key.js";
import { Key } from "../authorization/client.js";

export const KeyReadModel = Key.extend({
createdAt: z.string().datetime(),
Expand Down

0 comments on commit b92c8b5

Please sign in to comment.