diff --git a/plugins/auth-backend/src/providers/factories.ts b/plugins/auth-backend/src/providers/factories.ts new file mode 100644 index 0000000000..218f7995e1 --- /dev/null +++ b/plugins/auth-backend/src/providers/factories.ts @@ -0,0 +1,22 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AuthProviderFactories } from './types'; +import { GoogleAuthProvider } from './google/provider'; + +export const providerFactories: AuthProviderFactories = { + google: GoogleAuthProvider, +}; diff --git a/plugins/auth-backend/src/providers/index.test.ts b/plugins/auth-backend/src/providers/index.test.ts new file mode 100644 index 0000000000..a6d6c221a9 --- /dev/null +++ b/plugins/auth-backend/src/providers/index.test.ts @@ -0,0 +1,80 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import passport from 'passport'; +import express from 'express'; +import { makeProvider } from '.'; +import { AuthProvider, AuthProviderRouteHandlers } from './types'; + +class MyAuthProvider implements AuthProvider, AuthProviderRouteHandlers { + strategy(): passport.Strategy { + return new passport.Strategy(); + } + start( + req: express.Request, + res: express.Response, + next: express.NextFunction, + ): Promise { + return new Promise((res, rej) => res()); + } + frameHandler( + req: express.Request, + res: express.Response, + next: express.NextFunction, + ): express.Response { + return res.send('frameHandler'); + } + logout( + req: express.Request, + res: express.Response, + next: express.NextFunction, + ): express.Response { + return res.send('logout'); + } +} + +const providerFactories = { + a: MyAuthProvider, +}; + +const providerConfig = { + provider: 'a', + options: { + somekey: 'somevalue', + }, +}; + +const providerConfigInvalid = { + provider: 'b', + options: { + somekey: 'somevalue', + }, +}; + +describe('makeProvider', () => { + it('makes a provider for Myauthprovider', () => { + const provider = makeProvider(providerFactories, providerConfig); + expect(provider.providerId).toEqual('a'); + expect(provider.strategy).toBeDefined(); + expect(provider.providerRouter).toBeDefined(); + }); + + it('throws an error when provider implementation does not exist', () => { + expect(() => { + makeProvider(providerFactories, providerConfigInvalid); + }).toThrow('Provider Implementation missing for : b auth provider'); + }); +}); diff --git a/plugins/auth-backend/src/providers/index.ts b/plugins/auth-backend/src/providers/index.ts index a26176381a..433c7483b3 100644 --- a/plugins/auth-backend/src/providers/index.ts +++ b/plugins/auth-backend/src/providers/index.ts @@ -21,12 +21,6 @@ import { AuthProviderConfig, } from './types'; -import { GoogleAuthProvider } from './google/provider'; - -const providerFactories: AuthProviderFactories = { - google: GoogleAuthProvider, -}; - export const defaultRouter = (provider: AuthProviderRouteHandlers) => { const router = Router(); router.get('/start', provider.start); @@ -38,11 +32,16 @@ export const defaultRouter = (provider: AuthProviderRouteHandlers) => { return router; }; -export const makeProvider = (config: AuthProviderConfig) => { +export const makeProvider = ( + providerFactories: AuthProviderFactories, + config: any, +) => { const providerId = config.provider; const ProviderImpl = providerFactories[providerId]; if (!ProviderImpl) { - throw Error(`Provider Implementation missing for provider: ${providerId}`); + throw Error( + `Provider Implementation missing for : ${providerId} auth provider`, + ); } const providerInstance = new ProviderImpl(config); const strategy = providerInstance.strategy(); diff --git a/plugins/auth-backend/src/service/router.ts b/plugins/auth-backend/src/service/router.ts index cdde95d9d0..18ec5ad81d 100644 --- a/plugins/auth-backend/src/service/router.ts +++ b/plugins/auth-backend/src/service/router.ts @@ -19,6 +19,7 @@ import Router from 'express-promise-router'; import passport from 'passport'; import { Logger } from 'winston'; import { providers } from './../providers/config'; +import { providerFactories } from './../providers/factories'; import { makeProvider } from '../providers'; export interface RouterOptions { @@ -35,6 +36,7 @@ export async function createRouter( // configure all the providers for (const providerConfig of providers) { const { providerId, strategy, providerRouter } = makeProvider( + providerFactories, providerConfig, ); logger.info(`Configuring provider: ${providerId}`);