diff --git a/plugins/auth-backend/src/providers/factories.ts b/plugins/auth-backend/src/providers/factories.ts index 0c9f99e878..010b86f409 100644 --- a/plugins/auth-backend/src/providers/factories.ts +++ b/plugins/auth-backend/src/providers/factories.ts @@ -14,37 +14,34 @@ * limitations under the License. */ -import { - AuthProviderFactories, - AuthProviderRouteHandlers, - AuthProviderConfig, -} from './types'; -import { GoogleAuthProvider } from './google'; -import { GithubAuthProvider } from './github'; -import { OAuthProvider } from './OAuthProvider'; +import Router from 'express-promise-router'; +import { createGithubProvider } from './github'; +import { createGoogleProvider } from './google'; +import { AuthProviderFactory, AuthProviderConfig } from './types'; -export class ProviderFactories { - private static readonly providerFactories: AuthProviderFactories = { - google: GoogleAuthProvider, - github: GithubAuthProvider, - }; +const factories: { [providerId: string]: AuthProviderFactory } = { + google: createGoogleProvider, + github: createGithubProvider, +}; - public static getProviderFactory( - config: AuthProviderConfig, - ): AuthProviderRouteHandlers { - const providerId = config.provider; - const ProviderImpl = ProviderFactories.providerFactories[providerId]; - if (!ProviderImpl) { - throw Error( - `Provider Implementation missing for : ${providerId} auth provider`, - ); - } - const providerInstance = new ProviderImpl(config); - const oauthProvider = new OAuthProvider( - providerInstance, - providerId, - config.disableRefresh, - ); - return oauthProvider; +export function createAuthProvider(providerId: string, config: any) { + const factory = factories[providerId]; + if (!factory) { + throw Error(`No auth provider available for '${providerId}'`); } + return factory(config); } + +export const createAuthProviderRouter = (config: AuthProviderConfig) => { + const providerId = config.provider; + const provider = createAuthProvider(providerId, config); + + const router = Router(); + router.get('/start', provider.start.bind(provider)); + router.get('/handler/frame', provider.frameHandler.bind(provider)); + router.get('/logout', provider.logout.bind(provider)); + if (provider.refresh) { + router.get('/refresh', provider.refresh.bind(provider)); + } + return router; +}; diff --git a/plugins/auth-backend/src/providers/github/index.ts b/plugins/auth-backend/src/providers/github/index.ts index c3a48d35e0..60ad6998b7 100644 --- a/plugins/auth-backend/src/providers/github/index.ts +++ b/plugins/auth-backend/src/providers/github/index.ts @@ -14,4 +14,4 @@ * limitations under the License. */ -export { GithubAuthProvider } from './provider'; +export { createGithubProvider } from './provider'; diff --git a/plugins/auth-backend/src/providers/github/provider.ts b/plugins/auth-backend/src/providers/github/provider.ts index 4445e98451..09622d9303 100644 --- a/plugins/auth-backend/src/providers/github/provider.ts +++ b/plugins/auth-backend/src/providers/github/provider.ts @@ -27,6 +27,7 @@ import { AuthInfoBase, AuthInfoPrivate, } from '../types'; +import { OAuthProvider } from '../OAuthProvider'; export class GithubAuthProvider implements OAuthProviderHandlers { private readonly providerConfig: AuthProviderConfig; @@ -57,3 +58,9 @@ export class GithubAuthProvider implements OAuthProviderHandlers { return await executeFrameHandlerStrategy(req, this._strategy); } } + +export function createGithubProvider(config: AuthProviderConfig) { + const provider = new GithubAuthProvider(config); + const oauthProvider = new OAuthProvider(provider, config.provider, true); + return oauthProvider; +} diff --git a/plugins/auth-backend/src/providers/google/index.ts b/plugins/auth-backend/src/providers/google/index.ts index 0ec98bef89..b2cd85e6de 100644 --- a/plugins/auth-backend/src/providers/google/index.ts +++ b/plugins/auth-backend/src/providers/google/index.ts @@ -14,4 +14,4 @@ * limitations under the License. */ -export { GoogleAuthProvider } from './provider'; +export { createGoogleProvider } from './provider'; diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index 90d33652a5..e3969b14c8 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -28,6 +28,7 @@ import { RedirectInfo, AuthProviderConfig, } from '../types'; +import { OAuthProvider } from '../OAuthProvider'; export class GoogleAuthProvider implements OAuthProviderHandlers { private readonly providerConfig: AuthProviderConfig; @@ -87,3 +88,9 @@ export class GoogleAuthProvider implements OAuthProviderHandlers { }; } } + +export function createGoogleProvider(config: AuthProviderConfig) { + const provider = new GoogleAuthProvider(config); + const oauthProvider = new OAuthProvider(provider, config.provider); + return oauthProvider; +} diff --git a/plugins/auth-backend/src/providers/index.test.ts b/plugins/auth-backend/src/providers/index.test.ts deleted file mode 100644 index 7f39d9de57..0000000000 --- a/plugins/auth-backend/src/providers/index.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 { defaultRouter } from '.'; - -describe('test', () => { - it('unbreaks the test runner', () => { - expect(defaultRouter).toBeDefined(); - }); -}); diff --git a/plugins/auth-backend/src/providers/index.ts b/plugins/auth-backend/src/providers/index.ts index cfbabbbad1..d210bfd1bb 100644 --- a/plugins/auth-backend/src/providers/index.ts +++ b/plugins/auth-backend/src/providers/index.ts @@ -14,24 +14,4 @@ * limitations under the License. */ -import Router from 'express-promise-router'; -import { AuthProviderRouteHandlers, AuthProviderConfig } from './types'; -import { ProviderFactories } from './factories'; - -export const defaultRouter = (provider: AuthProviderRouteHandlers) => { - const router = Router(); - router.get('/start', provider.start.bind(provider)); - router.get('/handler/frame', provider.frameHandler.bind(provider)); - router.get('/logout', provider.logout.bind(provider)); - if (provider.refresh) { - router.get('/refresh', provider.refresh.bind(provider)); - } - return router; -}; - -export const makeProvider = (config: AuthProviderConfig) => { - const providerId = config.provider; - const oauthProvider = ProviderFactories.getProviderFactory(config); - const providerRouter = defaultRouter(oauthProvider); - return { providerId, providerRouter }; -}; +export { createAuthProviderRouter } from './factories'; diff --git a/plugins/auth-backend/src/providers/types.ts b/plugins/auth-backend/src/providers/types.ts index dc83c19dd0..661435e74b 100644 --- a/plugins/auth-backend/src/providers/types.ts +++ b/plugins/auth-backend/src/providers/types.ts @@ -37,13 +37,9 @@ export interface AuthProviderRouteHandlers { logout(req: express.Request, res: express.Response): Promise; } -export type AuthProviderFactories = { - [key: string]: AuthProviderFactory; -}; - -export type AuthProviderFactory = { - new (providerConfig: any): OAuthProviderHandlers; -}; +export type AuthProviderFactory = ( + config: AuthProviderConfig, +) => AuthProviderRouteHandlers; export type AuthInfoBase = { accessToken: string; diff --git a/plugins/auth-backend/src/service/router.ts b/plugins/auth-backend/src/service/router.ts index 49487d551a..aeb72b3a3b 100644 --- a/plugins/auth-backend/src/service/router.ts +++ b/plugins/auth-backend/src/service/router.ts @@ -19,7 +19,7 @@ import Router from 'express-promise-router'; import cookieParser from 'cookie-parser'; import { Logger } from 'winston'; import { providers } from './../providers/config'; -import { makeProvider } from '../providers'; +import { createAuthProviderRouter } from '../providers'; export interface RouterOptions { logger: Logger; @@ -35,9 +35,10 @@ export async function createRouter( // configure all the providers for (const providerConfig of providers) { - const { providerId, providerRouter } = makeProvider(providerConfig); - logger.info(`Configuring provider, ${providerId}`); - router.use(`/${providerId}`, providerRouter); + const { provider } = providerConfig; + const providerRouter = createAuthProviderRouter(providerConfig); + logger.info(`Configuring provider, ${provider}`); + router.use(`/${provider}`, providerRouter); } return router;