-
Notifications
You must be signed in to change notification settings - Fork 0
Start work on API endpoints and API access used by frontend adapters #79
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
Open
jjnp
wants to merge
15
commits into
main
Choose a base branch
from
feature/adapter-api-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e052008
add basic api routes
jjnp c599f65
modify hooks to allow regex use for specifying public routes
jjnp 84ae992
add schemas
jjnp 86f6077
start rudimentary api key validation
jjnp be590f9
add migration
jjnp b63121f
add apikey repository and services
jjnp b0530cf
add repository for project
jjnp b339317
add project repo and tests
jjnp b0b9d90
add name to apiKey
jjnp 1c72b19
continue
jjnp 1a8ed13
rename apiKey to ApiAccess
jjnp 6643d8b
fix schema issues
jjnp 68c77cf
update server endpoints to conform to new names and types
jjnp 6a45b80
add uuid dependency to root workspace
jjnp c5f7ec8
fix linting issues
jjnp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| pnpm exec lint-staged | ||
| npm test | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,9 +7,11 @@ | |
| "kysely": "^0.27.3", | ||
| "pino": "^9.0.0", | ||
| "pino-pretty": "^11.0.0", | ||
| "uuid": "^9.0.1", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since node v20 the std. crypto library has builtin uuid v4 generator. Let's not add the dependency here. |
||
| "zod": "^3.23.5" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/uuid": "^9.0.8", | ||
| "vite": "^5.2.11", | ||
| "vitest": "^1.5.3" | ||
| }, | ||
|
|
||
134 changes: 134 additions & 0 deletions
134
services/src/auth/api-access-repository.integration.test.ts
This file contains hidden or 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,134 @@ | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { db } from '../db/database' | ||
| import { runMigration } from '../db/database-migration-util' | ||
| import { createProject } from '../project/project.repository' | ||
| import { | ||
| createApiAccess, | ||
| deleteApiAccess, | ||
| getApiAccessForProject, | ||
| projectHasKey, | ||
| setApiAccessName | ||
| } from './api-access.repository' | ||
| import type { ProjectId } from '../project/project' | ||
|
|
||
| beforeEach(async () => { | ||
| db.reset() | ||
| await runMigration() | ||
| }) | ||
|
|
||
| describe('ApiKey Repository', () => { | ||
| let projectId: ProjectId | ||
| beforeEach(async () => { | ||
| projectId = (await createProject({ name: 'demo project name' })).id as ProjectId | ||
| }) | ||
|
|
||
| describe('createApiAccess', () => { | ||
| it('should create an api key given an existing project', async () => { | ||
| const result = await createApiAccess(projectId) | ||
| expect(result).toBeTruthy() | ||
| expect(false).toBeFalsy() | ||
| }) | ||
| }) | ||
|
|
||
| describe('getApiAccessForProject', () => { | ||
| it('should fetch all apiKeys for a given project', async () => { | ||
| const access1 = await createApiAccess(projectId) | ||
| const access2 = await createApiAccess(projectId) | ||
|
|
||
| const keys = (await getApiAccessForProject(projectId)).map((it) => it.apikey) | ||
| expect(keys).toHaveLength(2) | ||
| expect(keys).includes(access1.apikey) | ||
| expect(keys).includes(access2.apikey) | ||
| }) | ||
|
|
||
| it('should retrieve the name of the apiKey when it is created with one', async () => { | ||
| const name = 'some key name' | ||
| const key = await createApiAccess(projectId, name) | ||
| const result = await getApiAccessForProject(projectId) | ||
|
|
||
| expect(result.find((it) => it.apikey === key.apikey)?.name).toBe(name) | ||
| }) | ||
|
|
||
| it('should no longer find deleted keys', async () => { | ||
| const key = await createApiAccess(projectId) | ||
|
|
||
| const keysBeforeDelete = await getApiAccessForProject(projectId) | ||
| expect(keysBeforeDelete.map((it) => it.apikey)).toContain(key.apikey) | ||
|
|
||
| await deleteApiAccess(projectId, key.apikey) | ||
| const keysAfterDelete = await getApiAccessForProject(projectId) | ||
| expect(keysAfterDelete.map((it) => it.apikey).includes(key.apikey)).toBe(false) | ||
| }) | ||
|
|
||
| it('should return an empty list in case the project does not exist', async () => { | ||
| const keys = await getApiAccessForProject(projectId) | ||
| expect(keys).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('should return an empty list if there are no keys', async () => { | ||
| const result = await getApiAccessForProject(projectId) | ||
| expect(result).toHaveLength(0) | ||
| }) | ||
| }) | ||
|
|
||
| describe('setApiAccessName', () => { | ||
| it('should be able to set a name when previously there was none', async () => { | ||
| const key = await createApiAccess(projectId) | ||
| const initialRetrieval = await getApiAccessForProject(projectId).then((it) => | ||
| it.find((apiKey) => apiKey.apikey === key.apikey) | ||
| ) | ||
|
|
||
| expect(initialRetrieval?.name).toBeFalsy() | ||
| const updatedName = 'some new apiKeyName' | ||
| await setApiAccessName(key.id, updatedName) | ||
|
|
||
| const secondRetrieval = await getApiAccessForProject(projectId).then((it) => | ||
| it.find((apiKey) => apiKey.apikey === key.apikey) | ||
| ) | ||
| expect(secondRetrieval?.name).toBe(updatedName) | ||
| }) | ||
|
|
||
| it('should be able to set the name', async () => { | ||
| const initialName = 'my personal api key' | ||
| const key = await createApiAccess(projectId, initialName) | ||
| const initialRetrieval = await getApiAccessForProject(projectId).then((it) => | ||
| it.find((apiKey) => apiKey.apikey === key.apikey) | ||
| ) | ||
|
|
||
| expect(initialRetrieval?.name).toBe(initialName) | ||
| const updatedName = 'some new apiKeyName' | ||
| await setApiAccessName(key.id, updatedName) | ||
|
|
||
| const secondRetrieval = await getApiAccessForProject(projectId).then((it) => | ||
| it.find((apiKey) => apiKey.apikey === key.apikey) | ||
| ) | ||
| expect(secondRetrieval?.name).toBe(updatedName) | ||
| }) | ||
| }) | ||
|
|
||
| describe('projectHasKey', () => { | ||
| it('should return true if there is a key for the project', async () => { | ||
| const key = await createApiAccess(projectId) | ||
| const result = await projectHasKey(projectId, key.apikey) | ||
| expect(result).toBe(true) | ||
| }) | ||
|
|
||
| it('should return false if the project does not have the corresponding key', async () => { | ||
| const result = await projectHasKey(projectId, 'nonexiststant-id') | ||
| expect(result).toBe(false) | ||
| }) | ||
|
|
||
| it('should return false if the project does not exist', async () => { | ||
| const result = await projectHasKey(4242, 'nonexiststant-id') | ||
| expect(result).toBe(false) | ||
| }) | ||
|
|
||
| it('should return false if key and project exist, but do not match', async () => { | ||
| const otherProjectId = (await createProject({ name: 'another Project' })).id as ProjectId | ||
| const key = await createApiAccess(projectId) | ||
|
|
||
| const result = await projectHasKey(otherProjectId, key.apikey) | ||
| expect(result).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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,53 @@ | ||
| import { db } from '../db/database' | ||
| import type { ApiKeyCreationParams, SelectableApiKey } from './api-access' | ||
| import { v4 as uuid } from 'uuid' | ||
|
|
||
| export function createApiAccess( | ||
| projectId: number, | ||
| name: string | null = null | ||
| ): Promise<SelectableApiKey> { | ||
| const insertKey: ApiKeyCreationParams = { | ||
| apikey: uuid(), | ||
| name, | ||
| project_id: projectId | ||
| } | ||
|
|
||
| return db | ||
| .insertInto('apiaccess') | ||
| .values(insertKey) | ||
| .returningAll() | ||
| .executeTakeFirstOrThrow(() => new Error('Error creating Api Access Key')) | ||
| } | ||
|
|
||
| export function getApiAccessForProject(projectId: number): Promise<SelectableApiKey[]> { | ||
| return db.selectFrom('apiaccess').selectAll().where('project_id', '==', projectId).execute() | ||
| } | ||
|
|
||
| export async function setApiAccessName(keyId: number, name: string): Promise<void> { | ||
| await db | ||
| .updateTable('apiaccess') | ||
| .set({ | ||
| name | ||
| }) | ||
| .where('id', '==', keyId) | ||
| .execute() | ||
| } | ||
|
|
||
| export async function projectHasKey(projectId: number, apikey: string): Promise<boolean> { | ||
| const result = await db | ||
| .selectFrom('apiaccess') | ||
| .selectAll() | ||
| .where('project_id', '==', projectId) | ||
| .where('apikey', '==', apikey) | ||
| .execute() | ||
|
|
||
| return !!result.length | ||
| } | ||
|
|
||
| export async function deleteApiAccess(projectId: number, apikey: string): Promise<void> { | ||
| await db | ||
| .deleteFrom('apiaccess') | ||
| .where('project_id', '==', projectId) | ||
| .where('apikey', '==', apikey) | ||
| .execute() | ||
| } |
This file contains hidden or 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,27 @@ | ||
| import type { ProjectId } from '../project/project' | ||
| import { type ApiAccess, type ApiAccessId, type ApiKey, apiAccessSchema } from './api-access' | ||
| import { | ||
| createApiAccess, | ||
| getApiAccessForProject, | ||
| projectHasKey, | ||
| setApiAccessName | ||
| } from './api-access.repository' | ||
|
|
||
| export const checkApiKeyAccess = async (apiKey: ApiKey, projectId: ProjectId): Promise<boolean> => | ||
| projectHasKey(projectId, apiKey) | ||
|
|
||
| export const addApiAccess = async (projectId: ProjectId): Promise<ApiAccess> => { | ||
| const key = await createApiAccess(projectId) | ||
|
|
||
| return apiAccessSchema.parse(key) | ||
| } | ||
|
|
||
| export const changeApiAccessName = async (apiAccessId: ApiAccessId, name: string) => { | ||
| await setApiAccessName(apiAccessId, name) | ||
| } | ||
|
|
||
| export const listApiAccessForProject = async (projectId: ProjectId): Promise<ApiAccess[]> => { | ||
| const queryResult = await getApiAccessForProject(projectId) | ||
|
|
||
| return apiAccessSchema.array().parse(queryResult) | ||
| } |
This file contains hidden or 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,22 @@ | ||
| import type { Insertable, Selectable } from 'kysely' | ||
| import type { Apiaccess } from 'kysely-codegen' | ||
| import { z } from 'zod' | ||
|
|
||
| export type ApiKeyCreationParams = Insertable<Omit<Apiaccess, 'id' | 'created_at'>> | ||
| export type SelectableApiKey = Selectable<Apiaccess> | ||
|
|
||
| const apiAccessIdSchema = z.number().brand('api-access') | ||
| export type ApiAccessId = z.infer<typeof apiAccessIdSchema> | ||
|
|
||
| export const apiKeySchema = z.string().uuid().brand('api-key') | ||
| export type ApiKey = z.infer<typeof apiKeySchema> | ||
|
|
||
| export const apiAccessSchema = z.object({ | ||
| id: apiAccessIdSchema, | ||
| apikey: apiKeySchema, | ||
| name: z.string(), | ||
| project_id: z.number(), | ||
| created_at: z.date(), | ||
| updated_at: z.date() | ||
| }) | ||
| export type ApiAccess = z.infer<typeof apiAccessSchema> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also want to run lint-staged no?