auth-node: refactor to use plain ProfileTransform

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-08-07 17:43:17 +02:00
parent 2b8781affd
commit d3265deba8
6 changed files with 38 additions and 30 deletions
+1
View File
@@ -38,6 +38,7 @@ export type {
ClientAuthResponse,
CookieConfigurer,
ProfileInfo,
ProfileTransform,
SignInInfo,
SignInResolver,
TokenParams,
@@ -20,12 +20,12 @@ import {
PassportHelpers,
PassportProfile,
} from '../passport';
import { ProfileTransform } from '../types';
import {
OAuthAuthenticatorAuthenticateInput,
OAuthAuthenticatorRefreshInput,
OAuthAuthenticatorResult,
OAuthAuthenticatorStartInput,
OAuthProfileTransform,
} from './types';
/** @public */
@@ -53,13 +53,14 @@ export type PassportOAuthDoneCallback = PassportDoneCallback<
/** @public */
export class PassportOAuthAuthenticatorHelper {
static defaultProfileTransform: OAuthProfileTransform<PassportProfile> =
async input => ({
profile: PassportHelpers.transformProfile(
input.fullProfile,
input.session.idToken,
),
});
static defaultProfileTransform: ProfileTransform<
OAuthAuthenticatorResult<PassportProfile>
> = async input => ({
profile: PassportHelpers.transformProfile(
input.fullProfile,
input.session.idToken,
),
});
static from(strategy: Strategy) {
return new PassportOAuthAuthenticatorHelper(strategy);
@@ -15,22 +15,22 @@
*/
import { readDeclarativeSignInResolver } from '../sign-in';
import { AuthProviderFactory, SignInResolver } from '../types';
import {
AuthProviderFactory,
ProfileTransform,
SignInResolver,
} from '../types';
import { OAuthEnvironmentHandler } from './OAuthEnvironmentHandler';
import { createOAuthRouteHandlers } from './createOAuthRouteHandlers';
import { OAuthStateTransform } from './state';
import {
OAuthAuthenticator,
OAuthAuthenticatorResult,
OAuthProfileTransform,
} from './types';
import { OAuthAuthenticator, OAuthAuthenticatorResult } from './types';
import { SignInResolverFactory } from '../sign-in/createSignInResolverFactory';
/** @public */
export function createOAuthProviderFactory<TProfile>(options: {
authenticator: OAuthAuthenticator<unknown, TProfile>;
stateTransform?: OAuthStateTransform;
profileTransform?: OAuthProfileTransform<TProfile>;
profileTransform?: ProfileTransform<OAuthAuthenticatorResult<TProfile>>;
signInResolver?: SignInResolver<OAuthAuthenticatorResult<TProfile>>;
signInResolverFactories?: {
[name in string]: SignInResolverFactory<
@@ -43,8 +43,9 @@ export function createOAuthProviderFactory<TProfile>(options: {
return OAuthEnvironmentHandler.mapConfig(ctx.config, envConfig => {
const signInResolver =
options.signInResolver ??
readDeclarativeSignInResolver(envConfig, {
signInResolverFactories: options.signInResolverFactories,
readDeclarativeSignInResolver({
config: envConfig,
signInResolverFactories: options.signInResolverFactories ?? {},
});
return createOAuthRouteHandlers<TProfile>({
@@ -37,13 +37,10 @@ import {
AuthResolverContext,
ClientAuthResponse,
CookieConfigurer,
ProfileTransform,
SignInResolver,
} from '../types';
import {
OAuthAuthenticator,
OAuthAuthenticatorResult,
OAuthProfileTransform,
} from './types';
import { OAuthAuthenticator, OAuthAuthenticatorResult } from './types';
import { Config } from '@backstage/config';
/** @public */
@@ -56,7 +53,7 @@ export interface OAuthRouteHandlersOptions<TProfile> {
config: Config;
resolverContext: AuthResolverContext;
stateTransform?: OAuthStateTransform;
profileTransform?: OAuthProfileTransform<TProfile>;
profileTransform?: ProfileTransform<OAuthAuthenticatorResult<TProfile>>;
cookieConfigurer?: CookieConfigurer;
signInResolver?: SignInResolver<OAuthAuthenticatorResult<TProfile>>;
}
+2 -7
View File
@@ -16,7 +16,7 @@
import { Config } from '@backstage/config';
import { Request } from 'express';
import { AuthResolverContext, ProfileInfo } from '../types';
import { ProfileTransform } from '../types';
export interface OAuthSession {
accessToken: string;
@@ -27,11 +27,6 @@ export interface OAuthSession {
refreshToken?: string;
}
export type OAuthProfileTransform<TProfile> = (
result: OAuthAuthenticatorResult<TProfile>,
context: AuthResolverContext,
) => Promise<{ profile: ProfileInfo }>;
export interface OAuthAuthenticatorStartInput {
scope: string;
state: string;
@@ -60,7 +55,7 @@ export interface OAuthAuthenticatorResult<TProfile> {
}
export interface OAuthAuthenticator<TContext, TProfile> {
defaultProfileTransform: OAuthProfileTransform<TProfile>;
defaultProfileTransform: ProfileTransform<OAuthAuthenticatorResult<TProfile>>;
shouldPersistScopes?: boolean;
initialize(ctx: { callbackUrl: string; config: Config }): TContext;
start(
+13
View File
@@ -309,6 +309,19 @@ export type SignInResolver<TAuthResult> = (
context: AuthResolverContext,
) => Promise<BackstageSignInResult>;
/**
* Describes the function that transforms the result of a successful
* authentication into a {@link ProfileInfo} object.
*
* This function may optionally throw an error in order to reject authentication.
*
* @public
*/
export type ProfileTransform<TResult> = (
result: TResult,
context: AuthResolverContext,
) => Promise<{ profile: ProfileInfo }>;
/**
* Used to display login information to user, i.e. sidebar popup.
*