Skip to content

Commit

Permalink
feat: separate plugin from index
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkaron committed Feb 22, 2022
1 parent c212168 commit bf9d76d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 77 deletions.
84 changes: 7 additions & 77 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,7 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import { FastifyPluginAsync } from 'fastify';
import fp from 'fastify-plugin';
import { Client } from 'openid-client';
import {
openIDAuthLoginFactory,
OpenIDLoginHandlerFactory,
OpenIDLoginOptions,
} from './login';
import {
openIDAuthLogoutFactory,
OpenIDLogoutHandlerFactory,
OpenIDLogoutOptions,
} from './logout';
import {
openIDAuthRefreshFactory,
OpenIDRefreshHandlerFactory,
OpenIDRefreshOptions,
} from './refresh';
import {
openIDAuthVerifyFactory,
OpenIDVerifyHandlerFactory,
OpenIDVerifyOptions,
} from './verify';

declare module 'fastify' {
interface FastifyRequest {
session: {
get(key: string): any;
set(key: string, value: any): void;
};
}
}

export interface FastifyOpenIDAuthPluginOptions {
name: string;
client: Client;
login?: OpenIDLoginOptions;
verify: OpenIDVerifyOptions;
refresh: OpenIDRefreshOptions;
logout: OpenIDLogoutOptions;
}

export interface OpenIDAuthNamespace {
login: OpenIDLoginHandlerFactory;
verify: OpenIDVerifyHandlerFactory;
refresh: OpenIDRefreshHandlerFactory;
logout: OpenIDLogoutHandlerFactory;
}

export const openIDAuthPlugin: FastifyPluginAsync<FastifyOpenIDAuthPluginOptions> =
fp(
async (fastify, options) => {
const { name, client, login, refresh, verify, logout } = options;

const openIDAuthNamespace: OpenIDAuthNamespace = {
login: openIDAuthLoginFactory(client, login),
refresh: openIDAuthRefreshFactory(client, refresh),
verify: openIDAuthVerifyFactory(verify),
logout: openIDAuthLogoutFactory(client, logout),
};

fastify.log.trace(
`decorating \`fastify[${name}]\` with OpenIDAuthNamespace`
);
fastify.decorate(name, openIDAuthNamespace);
},
{
fastify: '3.x',
name: 'fastify-openid-auth',
decorators: {
request: ['session'],
},
}
);

export default openIDAuthPlugin;
export * from './login';
export * from './logout';
export * from './plugin';
export { openIDAuthPlugin as default } from './plugin';
export * from './refresh';
export * from './types';
export * from './verify';
9 changes: 9 additions & 0 deletions src/login.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import { RouteHandlerMethod } from 'fastify';
import {
AuthorizationParameters,
Expand All @@ -10,6 +11,14 @@ import {
import { format } from 'util';
import { OpenIDWriteTokens } from './types';

declare module 'fastify' {
interface FastifyRequest {
session: {
get(key: string): any;
set(key: string, value: any): void;
};
}
}
export interface OpenIDLoginOptions {
params?: AuthorizationParameters;
extras?: CallbackExtras;
Expand Down
65 changes: 65 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { FastifyPluginAsync } from 'fastify';
import fp from 'fastify-plugin';
import { Client } from 'openid-client';
import {
openIDAuthLoginFactory,
OpenIDLoginHandlerFactory,
OpenIDLoginOptions,
} from './login';
import {
openIDAuthLogoutFactory,
OpenIDLogoutHandlerFactory,
OpenIDLogoutOptions,
} from './logout';
import {
openIDAuthRefreshFactory,
OpenIDRefreshHandlerFactory,
OpenIDRefreshOptions,
} from './refresh';
import {
openIDAuthVerifyFactory,
OpenIDVerifyHandlerFactory,
OpenIDVerifyOptions,
} from './verify';

export interface FastifyOpenIDAuthPluginOptions {
name: string;
client: Client;
login?: OpenIDLoginOptions;
verify: OpenIDVerifyOptions;
refresh: OpenIDRefreshOptions;
logout: OpenIDLogoutOptions;
}

export interface OpenIDAuthNamespace {
login: OpenIDLoginHandlerFactory;
verify: OpenIDVerifyHandlerFactory;
refresh: OpenIDRefreshHandlerFactory;
logout: OpenIDLogoutHandlerFactory;
}

export const openIDAuthPlugin: FastifyPluginAsync<FastifyOpenIDAuthPluginOptions> =
fp(
async (fastify, options) => {
const { name, client, login, refresh, verify, logout } = options;

const openIDAuthNamespace: OpenIDAuthNamespace = {
login: openIDAuthLoginFactory(client, login),
refresh: openIDAuthRefreshFactory(client, refresh),
verify: openIDAuthVerifyFactory(verify),
logout: openIDAuthLogoutFactory(client, logout),
};

fastify.log.trace(
`decorating \`fastify[${name}]\` with OpenIDAuthNamespace`
);
fastify.decorate(name, openIDAuthNamespace);
},
{
fastify: '3.x',
name: 'fastify-openid-auth',
decorators: {
request: ['session'],
},
}
);

0 comments on commit bf9d76d

Please sign in to comment.