backend-app-api: added backend.auth.dangerouslyDisableDefaultAuthPolicy config + tests

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-19 18:46:33 +01:00
parent cb993f9dc8
commit a2b90a82d4
10 changed files with 302 additions and 13 deletions
+4 -1
View File
@@ -128,7 +128,10 @@ export interface MockDirectoryOptions {
// @public (undocumented)
export namespace mockServices {
// (undocumented)
export function auth(options?: { pluginId?: string }): AuthService;
export function auth(options?: {
pluginId?: string;
disableDefaultAuthPolicy?: boolean;
}): AuthService;
// (undocumented)
export namespace auth {
const // (undocumented)
@@ -24,7 +24,10 @@ import {
} from './mockCredentials';
describe('MockAuthService', () => {
const auth = new MockAuthService('test');
const auth = new MockAuthService({
pluginId: 'test',
disableDefaultAuthPolicy: false,
});
it('should reject invalid tokens', async () => {
await expect(auth.authenticate('')).rejects.toThrow('Token is empty');
@@ -37,7 +37,16 @@ import {
/** @internal */
export class MockAuthService implements AuthService {
constructor(private readonly pluginId: string) {}
readonly pluginId: string;
readonly disableDefaultAuthPolicy: boolean;
constructor(options: {
pluginId: string;
disableDefaultAuthPolicy: boolean;
}) {
this.pluginId = options.pluginId;
this.disableDefaultAuthPolicy = options.disableDefaultAuthPolicy;
}
async authenticate(token: string): Promise<BackstageCredentials> {
switch (token) {
@@ -117,6 +126,10 @@ export class MockAuthService implements AuthService {
| BackstageServicePrincipal
| BackstageNonePrincipal;
if (principal.type === 'none' && this.disableDefaultAuthPolicy) {
return { token: '' };
}
if (principal.type !== 'user' && principal.type !== 'service') {
throw new AuthenticationError(
`Refused to issue service token for credential type '${principal.type}'`,
@@ -35,7 +35,10 @@ export class MockHttpAuthService implements HttpAuthService {
#defaultCredentials: BackstageCredentials;
constructor(pluginId: string, defaultCredentials: BackstageCredentials) {
this.#auth = new MockAuthService(pluginId);
this.#auth = new MockAuthService({
pluginId,
disableDefaultAuthPolicy: false,
});
this.#defaultCredentials = defaultCredentials;
}
@@ -169,14 +169,33 @@ export namespace mockServices {
}));
}
export function auth(options?: { pluginId?: string }): AuthService {
return new MockAuthService(options?.pluginId ?? 'test');
export function auth(options?: {
pluginId?: string;
disableDefaultAuthPolicy?: boolean;
}): AuthService {
return new MockAuthService({
pluginId: options?.pluginId ?? 'test',
disableDefaultAuthPolicy: Boolean(options?.disableDefaultAuthPolicy),
});
}
export namespace auth {
export const factory = createServiceFactory({
service: coreServices.auth,
deps: { plugin: coreServices.pluginMetadata },
factory: ({ plugin }) => new MockAuthService(plugin.getId()),
deps: {
plugin: coreServices.pluginMetadata,
config: coreServices.rootConfig,
},
factory({ plugin, config }) {
const disableDefaultAuthPolicy = Boolean(
config.getOptionalBoolean(
'backend.auth.dangerouslyDisableDefaultAuthPolicy',
),
);
return new MockAuthService({
pluginId: plugin.getId(),
disableDefaultAuthPolicy,
});
},
});
export const mock = simpleMock(coreServices.auth, () => ({
authenticate: jest.fn(),