core-components: update ProxiedSignInPage for IdentityApi deprecation removals

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-01-13 13:00:46 +01:00
parent f58092cfeb
commit b00ea117cb
4 changed files with 35 additions and 10 deletions
@@ -79,7 +79,6 @@ describe('ProxiedSignInIdentity', () => {
picture: 'p',
},
backstageIdentity: {
id: 'i',
token: [
'eyJhbGciOiJFUzI1NiIsImtpZCI6ImMxNTMzNDRiLWZjYzktNGIwOS1iN2ZhLTU3ZmM5MDhjMjBiNiJ9',
btoa(
@@ -96,8 +95,8 @@ describe('ProxiedSignInIdentity', () => {
].join('.'),
identity: {
type: 'user',
userEntityRef: 'ue',
ownershipEntityRefs: ['oe'],
userEntityRef: 'k:ns/ue',
ownershipEntityRefs: ['k:ns/oe'],
},
},
};
@@ -126,6 +125,28 @@ describe('ProxiedSignInIdentity', () => {
expect(getBaseUrl).lastCalledWith('auth');
expect(serverCalled).toBeCalledTimes(1);
// All information should now be available
await expect(identity.getBackstageIdentity()).resolves.toEqual({
type: 'user',
userEntityRef: 'k:ns/ue',
ownershipEntityRefs: ['k:ns/oe'],
});
await expect(identity.getIdToken()).resolves.toEqual(expect.any(String));
await expect(identity.getCredentials()).resolves.toEqual({
token: expect.any(String),
});
await expect(identity.getProfileInfo()).resolves.toEqual({
email: 'e',
displayName: 'd',
picture: 'p',
});
expect(identity.getUserId()).toBe('ue');
expect(identity.getProfile()).toEqual({
email: 'e',
displayName: 'd',
picture: 'p',
});
await identity.getSessionAsync(); // no need to fetch again just yet
expect(serverCalled).toBeCalledTimes(1);
@@ -94,8 +94,14 @@ export class ProxiedSignInIdentity implements IdentityApi {
/** {@inheritdoc @backstage/core-plugin-api#IdentityApi.getUserId} */
getUserId(): string {
const session = this.getSessionSync();
return session.backstageIdentity.id;
const { backstageIdentity } = this.getSessionSync();
const ref = backstageIdentity.identity.userEntityRef;
const match = /^([^:/]+:)?([^:/]+\/)?([^:/]+)$/.exec(ref);
if (!match) {
throw new TypeError(`Invalid user entity reference "${ref}"`);
}
return match[3];
}
/** {@inheritdoc @backstage/core-plugin-api#IdentityApi.getIdToken} */
@@ -28,12 +28,11 @@ describe('types', () => {
picture: 'p',
},
backstageIdentity: {
id: 'i',
token: 't',
identity: {
type: 'user',
userEntityRef: 'ue',
ownershipEntityRefs: ['oe'],
userEntityRef: 'k:ns/ue',
ownershipEntityRefs: ['k:ns/oe'],
},
},
};
@@ -28,7 +28,6 @@ export const proxiedSessionSchema = z.object({
picture: z.string().optional(),
}),
backstageIdentity: z.object({
id: z.string(),
token: z.string(),
identity: z.object({
type: z.literal('user'),
@@ -47,5 +46,5 @@ export const proxiedSessionSchema = z.object({
export type ProxiedSession = {
providerInfo?: { [key: string]: unknown };
profile: ProfileInfo;
backstageIdentity: BackstageIdentityResponse;
backstageIdentity: Omit<BackstageIdentityResponse, 'id'>;
};