From 987637d75a322e5bec2d745cfc21a306f51338a8 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 29 Jul 2023 11:31:00 +0200 Subject: [PATCH] auth-node: added createOAuthProviderFactory Signed-off-by: Patrik Oldsberg --- .../src/oauth/createOAuthProviderFactory.ts | 51 +++++++++++++++++++ plugins/auth-node/src/oauth/index.ts | 1 + 2 files changed, 52 insertions(+) create mode 100644 plugins/auth-node/src/oauth/createOAuthProviderFactory.ts diff --git a/plugins/auth-node/src/oauth/createOAuthProviderFactory.ts b/plugins/auth-node/src/oauth/createOAuthProviderFactory.ts new file mode 100644 index 0000000000..6120bb528a --- /dev/null +++ b/plugins/auth-node/src/oauth/createOAuthProviderFactory.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2023 The Backstage Authors + * + * 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 { AuthProviderFactory, SignInResolver } from '../types'; +import { OAuthEnvironmentHandler } from './OAuthEnvironmentHandler'; +import { createOAuthRouteHandlers } from './createOAuthRouteHandlers'; +import { OAuthStateTransform } from './state'; +import { + OAuthAuthenticator, + OAuthAuthenticatorResult, + OAuthProfileTransform, +} from './types'; + +/** @public */ +export function createOAuthProviderFactory(options: { + authenticator: OAuthAuthenticator; + stateTransform?: OAuthStateTransform; + profileTransform?: OAuthProfileTransform; + signInResolver?: SignInResolver>; +}): AuthProviderFactory { + return ctx => { + return OAuthEnvironmentHandler.mapConfig(ctx.config, envConfig => { + return createOAuthRouteHandlers({ + authenticator: options.authenticator, + appUrl: ctx.globalConfig.appUrl, + baseUrl: ctx.globalConfig.baseUrl, + config: envConfig, + isOriginAllowed: ctx.globalConfig.isOriginAllowed, + cookieConfigurer: ctx.globalConfig.cookieConfigurer, + providerId: ctx.providerId, + resolverContext: ctx.resolverContext, + stateTransform: options.stateTransform, + profileTransform: options.profileTransform, + signInResolver: options.signInResolver, + }); + }); + }; +} diff --git a/plugins/auth-node/src/oauth/index.ts b/plugins/auth-node/src/oauth/index.ts index 046d7a47f7..99f05b0d0c 100644 --- a/plugins/auth-node/src/oauth/index.ts +++ b/plugins/auth-node/src/oauth/index.ts @@ -25,6 +25,7 @@ export { type PassportOAuthResult, } from './PassportOAuthAuthenticatorHelper'; export { OAuthEnvironmentHandler } from './OAuthEnvironmentHandler'; +export { createOAuthProviderFactory } from './createOAuthProviderFactory'; export { createOAuthAuthenticator, type OAuthAuthenticator,