From 88cf6da18d2f793d891c78df3ae5809548cf8536 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 3 Sep 2020 12:02:58 +0200 Subject: [PATCH] auth-backend: move passport helpers into separate lib --- plugins/auth-backend/src/lib/oauth/types.ts | 3 +-- .../PassportStrategyHelper.test.ts | 0 .../{ => passport}/PassportStrategyHelper.ts | 13 +--------- .../auth-backend/src/lib/passport/index.ts | 24 +++++++++++++++++++ .../src/providers/auth0/provider.ts | 5 ++-- .../src/providers/github/provider.ts | 5 ++-- .../src/providers/gitlab/provider.ts | 5 ++-- .../src/providers/google/provider.ts | 5 ++-- .../src/providers/microsoft/provider.ts | 5 ++-- .../src/providers/oauth2/provider.ts | 5 ++-- .../src/providers/okta/provider.ts | 5 ++-- .../src/providers/saml/provider.ts | 2 +- plugins/auth-backend/src/providers/types.ts | 11 +++++++++ 13 files changed, 52 insertions(+), 36 deletions(-) rename plugins/auth-backend/src/lib/{ => passport}/PassportStrategyHelper.test.ts (100%) rename plugins/auth-backend/src/lib/{ => passport}/PassportStrategyHelper.ts (96%) create mode 100644 plugins/auth-backend/src/lib/passport/index.ts diff --git a/plugins/auth-backend/src/lib/oauth/types.ts b/plugins/auth-backend/src/lib/oauth/types.ts index ecc665831f..440b89ed6f 100644 --- a/plugins/auth-backend/src/lib/oauth/types.ts +++ b/plugins/auth-backend/src/lib/oauth/types.ts @@ -15,8 +15,7 @@ */ import express from 'express'; -import { AuthResponse } from '../../providers/types'; -import { RedirectInfo } from '../PassportStrategyHelper'; +import { AuthResponse, RedirectInfo } from '../../providers/types'; /** * Common options for passport.js-based OAuth providers diff --git a/plugins/auth-backend/src/lib/PassportStrategyHelper.test.ts b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts similarity index 100% rename from plugins/auth-backend/src/lib/PassportStrategyHelper.test.ts rename to plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts diff --git a/plugins/auth-backend/src/lib/PassportStrategyHelper.ts b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.ts similarity index 96% rename from plugins/auth-backend/src/lib/PassportStrategyHelper.ts rename to plugins/auth-backend/src/lib/passport/PassportStrategyHelper.ts index 3b8d0801e9..7bc34186f4 100644 --- a/plugins/auth-backend/src/lib/PassportStrategyHelper.ts +++ b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.ts @@ -17,7 +17,7 @@ import express from 'express'; import passport from 'passport'; import jwtDecoder from 'jwt-decode'; -import { ProfileInfo } from '../providers/types'; +import { ProfileInfo, RedirectInfo } from '../../providers/types'; export type PassportDoneCallback = ( err?: Error, @@ -64,17 +64,6 @@ export const makeProfileInfo = ( }; }; -export type RedirectInfo = { - /** - * URL to redirect to - */ - url: string; - /** - * Status code to use for the redirect - */ - status?: number; -}; - export const executeRedirectStrategy = async ( req: express.Request, providerStrategy: passport.Strategy, diff --git a/plugins/auth-backend/src/lib/passport/index.ts b/plugins/auth-backend/src/lib/passport/index.ts new file mode 100644 index 0000000000..c307e212fa --- /dev/null +++ b/plugins/auth-backend/src/lib/passport/index.ts @@ -0,0 +1,24 @@ +/* + * 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. + */ + +export { + executeFetchUserProfileStrategy, + executeFrameHandlerStrategy, + executeRedirectStrategy, + executeRefreshTokenStrategy, + makeProfileInfo, +} from './PassportStrategyHelper'; +export type { PassportDoneCallback } from './PassportStrategyHelper'; diff --git a/plugins/auth-backend/src/providers/auth0/provider.ts b/plugins/auth-backend/src/providers/auth0/provider.ts index ff55846302..19f8d5d803 100644 --- a/plugins/auth-backend/src/providers/auth0/provider.ts +++ b/plugins/auth-backend/src/providers/auth0/provider.ts @@ -32,9 +32,8 @@ import { executeRefreshTokenStrategy, makeProfileInfo, PassportDoneCallback, - RedirectInfo, -} from '../../lib/PassportStrategyHelper'; -import { AuthProviderConfig } from '../types'; +} from '../../lib/passport'; +import { AuthProviderConfig, RedirectInfo } from '../types'; import { Config } from '@backstage/config'; type PrivateInfo = { diff --git a/plugins/auth-backend/src/providers/github/provider.ts b/plugins/auth-backend/src/providers/github/provider.ts index 22fb908ef9..68810a5b1b 100644 --- a/plugins/auth-backend/src/providers/github/provider.ts +++ b/plugins/auth-backend/src/providers/github/provider.ts @@ -21,9 +21,8 @@ import { executeRedirectStrategy, makeProfileInfo, PassportDoneCallback, - RedirectInfo, -} from '../../lib/PassportStrategyHelper'; -import { AuthProviderConfig } from '../types'; +} from '../../lib/passport'; +import { AuthProviderConfig, RedirectInfo } from '../types'; import { OAuthProvider, OAuthProviderOptions, diff --git a/plugins/auth-backend/src/providers/gitlab/provider.ts b/plugins/auth-backend/src/providers/gitlab/provider.ts index a3bd6deef3..d076644e46 100644 --- a/plugins/auth-backend/src/providers/gitlab/provider.ts +++ b/plugins/auth-backend/src/providers/gitlab/provider.ts @@ -21,9 +21,8 @@ import { executeRedirectStrategy, makeProfileInfo, PassportDoneCallback, - RedirectInfo, -} from '../../lib/PassportStrategyHelper'; -import { AuthProviderConfig } from '../types'; +} from '../../lib/passport'; +import { AuthProviderConfig, RedirectInfo } from '../types'; import { OAuthProvider, OAuthProviderOptions, diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index 0301062474..21cd76b26f 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -23,9 +23,8 @@ import { makeProfileInfo, executeFetchUserProfileStrategy, PassportDoneCallback, - RedirectInfo, -} from '../../lib/PassportStrategyHelper'; -import { AuthProviderConfig } from '../types'; +} from '../../lib/passport'; +import { AuthProviderConfig, RedirectInfo } from '../types'; import { OAuthProvider, OAuthProviderHandlers, diff --git a/plugins/auth-backend/src/providers/microsoft/provider.ts b/plugins/auth-backend/src/providers/microsoft/provider.ts index 5eec047b69..a7edbb134e 100644 --- a/plugins/auth-backend/src/providers/microsoft/provider.ts +++ b/plugins/auth-backend/src/providers/microsoft/provider.ts @@ -25,10 +25,9 @@ import { makeProfileInfo, executeFetchUserProfileStrategy, PassportDoneCallback, - RedirectInfo, -} from '../../lib/PassportStrategyHelper'; +} from '../../lib/passport'; -import { AuthProviderConfig } from '../types'; +import { AuthProviderConfig, RedirectInfo } from '../types'; import { OAuthProvider, diff --git a/plugins/auth-backend/src/providers/oauth2/provider.ts b/plugins/auth-backend/src/providers/oauth2/provider.ts index a1a3cc7d97..bc50408948 100644 --- a/plugins/auth-backend/src/providers/oauth2/provider.ts +++ b/plugins/auth-backend/src/providers/oauth2/provider.ts @@ -32,9 +32,8 @@ import { executeRefreshTokenStrategy, makeProfileInfo, PassportDoneCallback, - RedirectInfo, -} from '../../lib/PassportStrategyHelper'; -import { AuthProviderConfig } from '../types'; +} from '../../lib/passport'; +import { AuthProviderConfig, RedirectInfo } from '../types'; import { Config } from '@backstage/config'; type PrivateInfo = { diff --git a/plugins/auth-backend/src/providers/okta/provider.ts b/plugins/auth-backend/src/providers/okta/provider.ts index e46e396aa5..05a692ba40 100644 --- a/plugins/auth-backend/src/providers/okta/provider.ts +++ b/plugins/auth-backend/src/providers/okta/provider.ts @@ -29,9 +29,8 @@ import { makeProfileInfo, executeFetchUserProfileStrategy, PassportDoneCallback, - RedirectInfo, -} from '../../lib/PassportStrategyHelper'; -import { AuthProviderConfig } from '../types'; +} from '../../lib/passport'; +import { AuthProviderConfig, RedirectInfo } from '../types'; import { Logger } from 'winston'; import { StateStore } from 'passport-oauth2'; import { TokenIssuer } from '../../identity'; diff --git a/plugins/auth-backend/src/providers/saml/provider.ts b/plugins/auth-backend/src/providers/saml/provider.ts index d6ea0520fc..82a1cfa1b7 100644 --- a/plugins/auth-backend/src/providers/saml/provider.ts +++ b/plugins/auth-backend/src/providers/saml/provider.ts @@ -24,7 +24,7 @@ import { executeFrameHandlerStrategy, executeRedirectStrategy, PassportDoneCallback, -} from '../../lib/PassportStrategyHelper'; +} from '../../lib/passport'; import { AuthProviderConfig, AuthProviderRouteHandlers, diff --git a/plugins/auth-backend/src/providers/types.ts b/plugins/auth-backend/src/providers/types.ts index c7b70adbdc..bc80a3d9b5 100644 --- a/plugins/auth-backend/src/providers/types.ts +++ b/plugins/auth-backend/src/providers/types.ts @@ -32,6 +32,17 @@ export type AuthProviderConfig = { appUrl: string; }; +export type RedirectInfo = { + /** + * URL to redirect to + */ + url: string; + /** + * Status code to use for the redirect + */ + status?: number; +}; + /** * Any Auth provider needs to implement this interface which handles the routes in the * auth backend. Any auth API requests from the frontend reaches these methods.