auth-backend: added legacy sign-in resolver transform

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-08-07 19:45:19 +02:00
parent 705ac88dcc
commit 7d29ca8e8c
3 changed files with 119 additions and 0 deletions
@@ -0,0 +1,69 @@
/*
* 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 {
AuthResolverContext,
PassportProfile,
} from '@backstage/plugin-auth-node';
import { adoptLegacyOAuthSignInResolver } from './adoptLegacyOAuthSignInResolver';
describe('adoptLegacyOAuthSignInResolver', () => {
it('should pass through undefined', () => {
expect(adoptLegacyOAuthSignInResolver(undefined)).toBeUndefined();
});
it('should convert a legacy resolver to a new one', () => {
const legacyResolver = jest.fn();
const newResolver = adoptLegacyOAuthSignInResolver(legacyResolver);
newResolver?.(
{
profile: { email: 'em@i.l' },
result: {
fullProfile: { id: 'id' } as PassportProfile,
session: {
accessToken: 'token',
expiresInSeconds: 3,
scope: 'sco pe',
tokenType: 'bear',
idToken: 'id-token',
refreshToken: 'refresh-token',
},
},
},
{ ctx: 'ctx' } as unknown as AuthResolverContext,
);
expect(legacyResolver).toHaveBeenCalledWith(
{
profile: { email: 'em@i.l' },
result: {
fullProfile: { id: 'id' },
accessToken: 'token',
refreshToken: 'refresh-token',
params: {
scope: 'sco pe',
id_token: 'id-token',
expires_in: 3,
token_type: 'bear',
},
},
},
{ ctx: 'ctx' },
);
});
});
@@ -0,0 +1,49 @@
/*
* 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 {
OAuthAuthenticatorResult,
PassportProfile,
SignInResolver,
} from '@backstage/plugin-auth-node';
import { OAuthResult } from '../oauth';
/** @public */
export function adoptLegacyOAuthSignInResolver(
signInResolver?: SignInResolver<OAuthResult>,
): SignInResolver<OAuthAuthenticatorResult<PassportProfile>> | undefined {
return (
signInResolver &&
(async (input, ctx) =>
signInResolver(
{
profile: input.profile,
result: {
fullProfile: input.result.fullProfile,
accessToken: input.result.session.accessToken,
refreshToken: input.result.session.refreshToken,
params: {
scope: input.result.session.scope,
id_token: input.result.session.idToken,
token_type: input.result.session.tokenType,
expires_in: input.result.session.expiresInSeconds,
},
},
},
ctx,
))
);
}
@@ -15,3 +15,4 @@
*/
export { adaptLegacyOAuthHandler } from './adaptLegacyOAuthHandler';
export { adoptLegacyOAuthSignInResolver } from './adoptLegacyOAuthSignInResolver';