chore: initial test with demo.backstage.io authentication

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2023-01-12 12:15:46 +01:00
parent 9d456891f9
commit fcecd81709
2 changed files with 77 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
/*
* 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 { DiscoveryApi } from '@backstage/core-plugin-api';
const ERROR_PREFIX = 'Invalid discovery URL pattern,';
/**
* UrlPatternDiscovery is a lightweight DiscoveryApi implementation.
* It uses a single template string to construct URLs for each plugin.
*
* @public
*/
export class UrlPatternDiscovery implements DiscoveryApi {
/**
* Creates a new UrlPatternDiscovery given a template. The the only
* interpolation done for the template is to replace instances of `{{pluginId}}`
* with the ID of the plugin being requested.
*
* Example pattern: `http://localhost:7007/api/{{ pluginId }}`
*/
static compile(pattern: string): UrlPatternDiscovery {
const parts = pattern.split(/\{\{\s*pluginId\s*\}\}/);
const urlStr = parts.join('pluginId');
let url;
try {
url = new URL(urlStr);
} catch {
throw new Error(`${ERROR_PREFIX} URL '${urlStr}' is invalid`);
}
if (url.hash) {
throw new Error(`${ERROR_PREFIX} URL must not have a hash`);
}
if (url.search) {
throw new Error(`${ERROR_PREFIX} URL must not have a query`);
}
if (urlStr.endsWith('/')) {
throw new Error(`${ERROR_PREFIX} URL must not end with a slash`);
}
return new UrlPatternDiscovery(parts);
}
private constructor(private readonly parts: string[]) {}
async getBaseUrl(pluginId: string): Promise<string> {
if (pluginId === 'auth') {
return 'https://demo.backstage.io/api/auth';
}
return this.parts.join(pluginId);
}
}
+10
View File
@@ -31,11 +31,21 @@ import {
AnyApiFactory,
configApiRef,
createApiFactory,
discoveryApiRef,
errorApiRef,
githubAuthApiRef,
} from '@backstage/core-plugin-api';
import { UrlPatternDiscovery } from './UrlPatternDiscovery';
export const apis: AnyApiFactory[] = [
createApiFactory({
api: discoveryApiRef,
deps: { configApi: configApiRef },
factory: ({ configApi }) =>
UrlPatternDiscovery.compile(
`${configApi.getString('backend.baseUrl')}/api/{{ pluginId }}`,
),
}),
createApiFactory({
api: scmIntegrationsApiRef,
deps: { configApi: configApiRef },