auth-node: add sign-in resolver factory

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-08-02 10:07:22 +02:00
parent 12b4d8a3f8
commit 39e19858b8
4 changed files with 98 additions and 6 deletions
+3 -1
View File
@@ -41,7 +41,9 @@
"lodash": "^4.17.21",
"node-fetch": "^2.6.7",
"passport": "^0.6.0",
"winston": "^3.2.1"
"winston": "^3.2.1",
"zod": "^3.21.4",
"zod-to-json-schema": "^3.21.4"
},
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
@@ -0,0 +1,71 @@
/*
* 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 { ZodSchema, ZodTypeDef } from 'zod';
import { SignInResolver } from '../types';
import zodToJsonSchema from 'zod-to-json-schema';
import { JsonObject } from '@backstage/types';
export interface SignInResolverFactory<TAuthResult, TOptions> {
(
...options: undefined extends TOptions
? [options?: TOptions]
: [options: TOptions]
): SignInResolver<TAuthResult>;
optionsJsonSchema?: JsonObject;
}
export interface SignInResolverFactoryOptions<
TAuthResult,
TOptionsOutput,
TOptionsInput,
> {
optionsSchema?: ZodSchema<TOptionsOutput, ZodTypeDef, TOptionsInput>;
create(options: TOptionsOutput): SignInResolver<TAuthResult>;
}
export function createSignInResolverFactory<
TAuthResult,
TOptionsOutput,
TOptionsInput,
>(
options: SignInResolverFactoryOptions<
TAuthResult,
TOptionsOutput,
TOptionsInput
>,
): SignInResolverFactory<TAuthResult, TOptionsInput> {
const { optionsSchema } = options;
if (!optionsSchema) {
return (resolverOptions?: TOptionsInput) => {
if (resolverOptions) {
throw new Error('sign-in resolver does not accept options');
}
return options.create(undefined as TOptionsOutput);
};
}
const factory = (
...[resolverOptions]: undefined extends TOptionsInput
? [options?: TOptionsInput]
: [options: TOptionsInput]
) => {
const parsedOptions = optionsSchema.parse(resolverOptions);
return options.create(parsedOptions);
};
factory.optionsJsonSchema = zodToJsonSchema(optionsSchema) as JsonObject;
return factory;
}
+17
View File
@@ -0,0 +1,17 @@
/*
* 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.
*/
export { createSignInResolverFactory } from './createSignInResolverFactory';