permission-backend: pass in identity client
Signed-off-by: Mike Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
```ts
|
||||
import express from 'express';
|
||||
import { IdentityClient } from '@backstage/plugin-auth-backend';
|
||||
import { Logger as Logger_2 } from 'winston';
|
||||
import { PermissionPolicy } from '@backstage/plugin-permission-node';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
@@ -16,6 +17,8 @@ export interface RouterOptions {
|
||||
// (undocumented)
|
||||
discovery: PluginEndpointDiscovery;
|
||||
// (undocumented)
|
||||
identity: IdentityClient;
|
||||
// (undocumented)
|
||||
logger: Logger_2;
|
||||
// (undocumented)
|
||||
policy: PermissionPolicy;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
import express from 'express';
|
||||
import request from 'supertest';
|
||||
import { getVoidLogger } from '@backstage/backend-common';
|
||||
import { IdentityClient } from '@backstage/plugin-auth-backend';
|
||||
import {
|
||||
AuthorizeResult,
|
||||
Permission,
|
||||
@@ -25,27 +26,6 @@ import { PermissionPolicy } from '@backstage/plugin-permission-node';
|
||||
|
||||
import { createRouter } from './router';
|
||||
|
||||
jest.mock('@backstage/plugin-auth-backend', () => {
|
||||
class MockIdentityClient {
|
||||
authenticate = jest.fn(token =>
|
||||
Promise.resolve(
|
||||
token
|
||||
? {
|
||||
id: 'test-user',
|
||||
token,
|
||||
}
|
||||
: undefined,
|
||||
),
|
||||
);
|
||||
|
||||
static getBearerToken = jest.fn(authHeader =>
|
||||
authHeader ? `<token for "${authHeader}">` : undefined,
|
||||
);
|
||||
}
|
||||
|
||||
return { IdentityClient: MockIdentityClient };
|
||||
});
|
||||
|
||||
const policy: PermissionPolicy = {
|
||||
handle: jest.fn().mockImplementation((_req, identity) => {
|
||||
if (identity) {
|
||||
@@ -70,6 +50,18 @@ describe('createRouter', () => {
|
||||
getBaseUrl: jest.fn(),
|
||||
getExternalBaseUrl: jest.fn(),
|
||||
},
|
||||
identity: {
|
||||
authenticate: jest.fn(token => {
|
||||
if (!token) {
|
||||
throw new Error('No token supplied!');
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
id: 'test-user',
|
||||
token,
|
||||
});
|
||||
}),
|
||||
} as unknown as IdentityClient,
|
||||
policy,
|
||||
});
|
||||
|
||||
@@ -108,7 +100,7 @@ describe('createRouter', () => {
|
||||
expect(response.status).toEqual(200);
|
||||
expect(policy.handle).toHaveBeenCalledWith(
|
||||
{ permission },
|
||||
{ id: 'test-user', token: '<token for "Bearer test-token">' },
|
||||
{ id: 'test-user', token: 'test-token' },
|
||||
);
|
||||
expect(response.body).toEqual([
|
||||
{ id: 123, result: AuthorizeResult.ALLOW },
|
||||
|
||||
@@ -44,6 +44,7 @@ export interface RouterOptions {
|
||||
logger: Logger;
|
||||
discovery: PluginEndpointDiscovery;
|
||||
policy: PermissionPolicy;
|
||||
identity: IdentityClient;
|
||||
}
|
||||
|
||||
const handleRequest = async (
|
||||
@@ -98,12 +99,7 @@ const handleRequest = async (
|
||||
export async function createRouter(
|
||||
options: RouterOptions,
|
||||
): Promise<express.Router> {
|
||||
const { policy, discovery } = options;
|
||||
|
||||
const identity = new IdentityClient({
|
||||
discovery,
|
||||
issuer: await discovery.getExternalBaseUrl('auth'),
|
||||
});
|
||||
const { policy, discovery, identity } = options;
|
||||
|
||||
const permissionIntegrationClient = new PermissionIntegrationClient({
|
||||
discovery,
|
||||
|
||||
Reference in New Issue
Block a user