auth-backend: move prepareBackstageIdentityResponse to auth-node

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-07-26 10:32:50 +02:00
parent b62b47a6dd
commit 48793dfc95
5 changed files with 74 additions and 32 deletions
@@ -1,41 +0,0 @@
/*
* Copyright 2020 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 { prepareBackstageIdentityResponse } from './prepareBackstageIdentityResponse';
function mkToken(payload: unknown) {
return `a.${Buffer.from(JSON.stringify(payload), 'utf8').toString(
'base64',
)}.z`;
}
describe('prepareBackstageIdentityResponse', () => {
it('parses a complete token to determine the identity', () => {
const token = mkToken({ sub: 'k:ns/n', ent: ['k:ns/o'] });
expect(
prepareBackstageIdentityResponse({
token,
}),
).toEqual({
token,
identity: {
type: 'user',
userEntityRef: 'k:ns/n',
ownershipEntityRefs: ['k:ns/o'],
},
});
});
});
@@ -14,39 +14,11 @@
* limitations under the License.
*/
import { InputError } from '@backstage/errors';
import {
BackstageIdentityResponse,
BackstageSignInResult,
} from '@backstage/plugin-auth-node';
function parseJwtPayload(token: string) {
const [_header, payload, _signature] = token.split('.');
return JSON.parse(Buffer.from(payload, 'base64').toString());
}
import { prepareBackstageIdentityResponse as _prepareBackstageIdentityResponse } from '@backstage/plugin-auth-node';
/**
* Parses a Backstage-issued token and decorates the
* {@link @backstage/plugin-auth-node#BackstageIdentityResponse} with identity information sourced from the
* token.
*
* @public
* @deprecated import from `@backstage/plugin-auth-node` instead
*/
export function prepareBackstageIdentityResponse(
result: BackstageSignInResult,
): BackstageIdentityResponse {
if (!result.token) {
throw new InputError(`Identity response must return a token`);
}
const { sub, ent } = parseJwtPayload(result.token);
return {
...result,
identity: {
type: 'user',
userEntityRef: sub,
ownershipEntityRefs: ent ?? [],
},
};
}
export const prepareBackstageIdentityResponse =
_prepareBackstageIdentityResponse;