feat: update the public api instead of profileTransform it is AuthHandler

Co-authored-by: Fredrik Adelöw <freben@users.noreply.github.com>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2021-06-16 10:56:42 +02:00
parent 551c050e23
commit f5f290f1c2
3 changed files with 24 additions and 22 deletions
@@ -42,10 +42,12 @@ describe('createGoogleProvider', () => {
logger: getVoidLogger(),
catalogIdentityClient: (catalogIdentityClient as unknown) as CatalogIdentityClient,
tokenIssuer: (tokenIssuer as unknown) as TokenIssuer,
profileTransform: async ({ fullProfile }) => ({
email: fullProfile.emails![0]!.value,
displayName: fullProfile.displayName,
picture: 'http://google.com/lols',
authHandler: async ({ fullProfile }) => ({
profile: {
email: fullProfile.emails![0]!.value,
displayName: fullProfile.displayName,
picture: 'http://google.com/lols',
},
}),
clientId: 'mock',
clientSecret: 'mock',
@@ -40,7 +40,7 @@ import {
} from '../../lib/passport';
import {
AuthProviderFactory,
ProfileTransform,
AuthHandler,
RedirectInfo,
SignInResolver,
} from '../types';
@@ -52,7 +52,7 @@ type PrivateInfo = {
type Options = OAuthProviderOptions & {
signInResolver?: SignInResolver<OAuthResult>;
profileTransform: ProfileTransform<OAuthResult>;
authHandler: AuthHandler<OAuthResult>;
tokenIssuer: TokenIssuer;
catalogIdentityClient: CatalogIdentityClient;
logger: Logger;
@@ -61,14 +61,14 @@ type Options = OAuthProviderOptions & {
export class GoogleAuthProvider implements OAuthHandlers {
private readonly _strategy: GoogleStrategy;
private readonly signInResolver?: SignInResolver<OAuthResult>;
private readonly profileTransform: ProfileTransform<OAuthResult>;
private readonly authHandler: AuthHandler<OAuthResult>;
private readonly tokenIssuer: TokenIssuer;
private readonly catalogIdentityClient: CatalogIdentityClient;
private readonly logger: Logger;
constructor(options: Options) {
this.signInResolver = options.signInResolver;
this.profileTransform = options.profileTransform;
this.authHandler = options.authHandler;
this.tokenIssuer = options.tokenIssuer;
this.catalogIdentityClient = options.catalogIdentityClient;
this.logger = options.logger;
@@ -146,7 +146,7 @@ export class GoogleAuthProvider implements OAuthHandlers {
}
private async handleResult(result: OAuthResult) {
const profile = await this.profileTransform(result);
const { profile } = await this.authHandler(result);
const response: OAuthResponse = {
providerInfo: {
@@ -235,7 +235,7 @@ export type GoogleProviderOptions = {
* The profile transformation function used to verify and convert the auth response
* into the profile that will be presented to the user.
*/
profileTransform?: ProfileTransform<OAuthResult>;
authHandler?: AuthHandler<OAuthResult>;
/**
* Configure sign-in for this provider, without it the provider can not be used to sign users in.
@@ -272,13 +272,11 @@ export const createGoogleProvider = (
tokenIssuer,
});
let profileTransform: ProfileTransform<OAuthResult> = async ({
fullProfile,
params,
}) => makeProfileInfo(fullProfile, params.id_token);
if (options?.profileTransform) {
profileTransform = options.profileTransform;
}
const authHandler: AuthHandler<OAuthResult> = options?.authHandler
? options.authHandler
: async ({ fullProfile, params }) => ({
profile: makeProfileInfo(fullProfile, params.id_token),
});
const signInResolverFn =
options?.signIn?.resolver ?? googleDefaultSignInResolver;
@@ -295,7 +293,7 @@ export const createGoogleProvider = (
clientSecret,
callbackUrl,
signInResolver,
profileTransform,
authHandler,
tokenIssuer,
catalogIdentityClient,
logger,
+6 -4
View File
@@ -219,14 +219,16 @@ export type SignInResolver<AuthResult> = (
},
) => Promise<BackstageIdentity>;
export type AuthHandlerResult = { profile: ProfileInfo };
/**
* A transformation function called every time the user authenticates using the provider.
* The AuthHandler function is called every time the user authenticates using the provider.
*
* The transform should return a profile that represents the session for the user in the frontend.
* The handler should return a profile that represents the session for the user in the frontend.
*
* Throwing an error in the function will cause the authentication to fail, making it
* possible to use this function as a way to limit access to a certain group of users.
*/
export type ProfileTransform<AuthResult> = (
export type AuthHandler<AuthResult> = (
input: AuthResult,
) => Promise<ProfileInfo>;
) => Promise<AuthHandlerResult>;