-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rbac to more api routes (#1009)
* feat: add rbac to more api routes * docs: wip * docs: rbac wip * Remove the lock icon for consistent look --------- Co-authored-by: James Perkins <[email protected]>
- Loading branch information
Showing
28 changed files
with
2,263 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { IntegrationHarness } from "@/pkg/testutil/integration-harness"; | ||
import { type Flatten, Unkey, or } from "@unkey/api/src/index"; // use unbundled raw esm typescript | ||
import { schema } from "@unkey/db"; | ||
import { newId } from "@unkey/id"; | ||
import { expect, test } from "vitest"; | ||
|
||
test("create with roles and permissions", async () => { | ||
using h = new IntegrationHarness(); | ||
await h.seed(); | ||
|
||
type Resources = { | ||
domain: "create" | "delete" | "read"; | ||
dns: { | ||
record: "create" | "read" | "delete"; | ||
}; | ||
}; | ||
type Permissions = Flatten<Resources, ".">; | ||
|
||
const roleId = newId("role"); | ||
await h.db.insert(schema.roles).values({ | ||
id: roleId, | ||
name: "domain.manager", | ||
workspaceId: h.resources.userWorkspace.id, | ||
}); | ||
|
||
for (const name of ["domain.create", "dns.record.create", "domain.delete"]) { | ||
const permissionId = newId("permission"); | ||
await h.db.insert(schema.permissions).values({ | ||
id: permissionId, | ||
name, | ||
workspaceId: h.resources.userWorkspace.id, | ||
}); | ||
|
||
await h.db.insert(schema.rolesPermissions).values({ | ||
roleId, | ||
permissionId, | ||
workspaceId: h.resources.userWorkspace.id, | ||
}); | ||
} | ||
|
||
const { key: rootKey } = await h.createRootKey([`api.${h.resources.userApi.id}.create_key`]); | ||
|
||
const sdk = new Unkey({ | ||
baseUrl: h.baseUrl, | ||
rootKey: rootKey, | ||
}); | ||
|
||
const create = await sdk.keys.create({ | ||
apiId: h.resources.userApi.id, | ||
roles: ["domain.manager"], | ||
}); | ||
expect(create.error).toBeUndefined(); | ||
const key = create.result!.key; | ||
|
||
const verify = await sdk.keys.verify<Permissions>({ | ||
apiId: h.resources.userApi.id, | ||
key, | ||
authorization: { | ||
permissions: or("domain.create", "dns.record.create"), | ||
}, | ||
}); | ||
|
||
expect(verify.error).toBeUndefined(); | ||
expect(verify.result).toBeDefined(); | ||
console.log("res", verify.result); | ||
|
||
expect(verify.result!.valid).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.