Skip to content

Commit

Permalink
WIP #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Snir Shechter committed Feb 10, 2021
1 parent 272784c commit e658d28
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import fastifyConstructor from 'fastify'
import { config } from './config/index.js'
import { routes } from './routes/index.js'
import { Storage } from './services/storage/index.js'
import { Auth } from './services/auth/index.js'
declare module 'fastify' {
interface FastifyInstance {
config: typeof config
storage: Storage
auth: Auth
}
}

Expand All @@ -18,6 +20,12 @@ const fastify = fastifyConstructor({
// Config
fastify.decorate('config', config)

// Auth
const auth = new Auth('BearerToken', {
token: '123',
})
fastify.decorate('auth', auth)

// Storage
const storage = new Storage({
appName: config.appName,
Expand Down
8 changes: 4 additions & 4 deletions src/services/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export class Auth implements AuthDriver {
return this._driver
}

public async isAuthorized(request: FastifyRequest, scopes: Scope[]): Promise<boolean> {
return this._driver.isAuthorized(request, scopes)
public async isAuthorized(request: FastifyRequest): Promise<boolean> {
return this._driver.isAuthorized(request)
}
public async allowedScopes(request: FastifyRequest): Promise<Scope[]> {
return this._driver.allowedScopes(request)
}
public authorize(request: FastifyRequest, scopes: Scope[]): Promise<void> {
return this._driver.authorize(request, scopes)
public authorize(request: FastifyRequest): Promise<void> {
return this._driver.authorize(request)
}
}
4 changes: 2 additions & 2 deletions src/services/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export enum Scope {
}

export interface AuthDriver {
isAuthorized(request: FastifyRequest, scopes: Scope[]): Promise<boolean>
isAuthorized(request: FastifyRequest): Promise<boolean>
allowedScopes(request: FastifyRequest): Promise<Scope[]>
authorize(request: FastifyRequest, scope: Scope[]): Promise<void> // Throws an error if fails
authorize(request: FastifyRequest): Promise<void> // Throws an error if fails
}

export type Driver = 'BearerToken'

0 comments on commit e658d28

Please sign in to comment.