backend-{plugin,app}-api: add HttpAuthService + initial implementation

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Carl-Erik Bergström <cbergstrom@spotify.com>
Co-authored-by: blam <ben@blam.sh>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-09 13:02:13 +01:00
parent c5a7cf4a4f
commit 9e45e2af6d
11 changed files with 322 additions and 3 deletions
@@ -0,0 +1,56 @@
/*
* Copyright 2024 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 { Request, Response, Handler } from 'express';
import {
BackstageUserCredentials,
BackstageServiceCredentials,
BackstageCredentials,
} from './AuthService';
/** @public */
export type BackstageUnauthorizedCredentials = {
$$type: '@backstage/BackstageCredentials';
type: 'unauthorized';
};
/** @public */
export type BackstageCredentialTypes = {
user: BackstageUserCredentials;
'user-cookie': BackstageUserCredentials;
service: BackstageServiceCredentials;
unauthorized: BackstageUnauthorizedCredentials;
};
/** @public */
export interface HttpAuthService {
createHttpPluginRouterMiddleware(): Handler;
credentials<TAllowed extends keyof BackstageCredentialTypes>(
req: Request,
options: {
allow: Array<TAllowed>;
},
): Promise<BackstageCredentialTypes[TAllowed]>;
// TODO: Keep an eye on this, might not be needed
requestHeaders(options?: {
forward?: BackstageCredentials;
}): Promise<Record<string, string>>;
issueUserCookie(res: Response): Promise<void>;
}
@@ -67,6 +67,15 @@ export namespace coreServices {
import('./DiscoveryService').DiscoveryService
>({ id: 'core.discovery' });
/**
* The service reference for the plugin scoped {@link HttpAuthService}.
*
* @public
*/
export const httpAuth = createServiceRef<
import('./HttpAuthService').HttpAuthService
>({ id: 'core.httpAuth' });
/**
* The service reference for the plugin scoped {@link HttpRouterService}.
*
@@ -30,6 +30,11 @@ export type { RootConfigService } from './RootConfigService';
export type { DatabaseService } from './DatabaseService';
export type { DiscoveryService } from './DiscoveryService';
export type { HttpRouterService } from './HttpRouterService';
export type {
HttpAuthService,
BackstageCredentialTypes,
BackstageUnauthorizedCredentials,
} from './HttpAuthService';
export type {
LifecycleService,
LifecycleServiceStartupHook,