Skip to content

Commit

Permalink
feat: abstract direct model queries, create signature service
Browse files Browse the repository at this point in the history
  • Loading branch information
njlie committed Aug 24, 2022
1 parent 8b6a1b5 commit 6516c7c
Show file tree
Hide file tree
Showing 10 changed files with 742 additions and 511 deletions.
28 changes: 28 additions & 0 deletions packages/auth/src/accessToken/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ describe('Access Token Service', (): void => {
})
})

describe('Get', (): void => {
let accessToken: AccessToken
beforeEach(async (): Promise<void> => {
accessToken = await AccessToken.query(trx).insert({
value: 'test-access-token',
managementId: v4(),
grantId: grant.id,
expiresIn: 1234
})
})

test('Can get an access token by its value', async (): Promise<void> => {
const fetchedToken = await accessTokenService.get(accessToken.value)
expect(fetchedToken.value).toEqual(accessToken.value)
expect(fetchedToken.managementId).toEqual(accessToken.managementId)
expect(fetchedToken.grantId).toEqual(accessToken.grantId)
})

test('Can get an access token by its managementId', async (): Promise<void> => {
const fetchedToken = await accessTokenService.getByManagementId(
accessToken.managementId
)
expect(fetchedToken.value).toEqual(accessToken.value)
expect(fetchedToken.managementId).toEqual(accessToken.managementId)
expect(fetchedToken.grantId).toEqual(accessToken.grantId)
})
})

describe('Introspect', (): void => {
test('Can introspect active token', async (): Promise<void> => {
const clientId = crypto
Expand Down
13 changes: 13 additions & 0 deletions packages/auth/src/accessToken/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { IAppConfig } from '../config/app'
import { Access } from '../access/model'

export interface AccessTokenService {
get(token: string): Promise<AccessToken>
getByManagementId(managementId: string): Promise<AccessToken>
introspect(token: string): Promise<Introspection | undefined>
revoke(id: string): Promise<void>
create(grantId: string, opts?: AccessTokenOpts): Promise<AccessToken>
Expand Down Expand Up @@ -64,6 +66,9 @@ export async function createAccessTokenService({
}

return {
get: (token: string) => get(token),
getByManagementId: (managementId: string) =>
getByManagementId(managementId),
introspect: (token: string) => introspect(deps, token),
revoke: (id: string) => revoke(deps, id),
create: (grantId: string, opts?: AccessTokenOpts) =>
Expand All @@ -78,6 +83,14 @@ function isTokenExpired(token: AccessToken): boolean {
return expiresAt < now.getTime()
}

async function get(token: string): Promise<AccessToken> {
return AccessToken.query().findOne('value', token)
}

async function getByManagementId(managementId: string): Promise<AccessToken> {
return AccessToken.query().findOne('managementId', managementId)
}

async function introspect(
deps: ServiceDependencies,
value: string
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class App {

const accessTokenRoutes = await this.container.use('accessTokenRoutes')
const grantRoutes = await this.container.use('grantRoutes')
const clientService = await this.container.use('clientService')
const signatureService = await this.container.use('signatureService')

const openApi = await this.container.use('openApi')
const toRouterPath = (path: string): string =>
Expand Down Expand Up @@ -225,7 +225,7 @@ export class App {
path,
method
}),
clientService.tokenHttpsigMiddleware,
signatureService.tokenHttpsigMiddleware,
route
)
// TODO: remove once all endpoints are implemented
Expand Down

0 comments on commit 6516c7c

Please sign in to comment.