feat(docs): add multiton docs and fix the custom handler service

Signed-off-by: Juan Pablo Garcia Ripa <sarabadu@gmail.com>
This commit is contained in:
Juan Pablo Garcia Ripa
2025-03-24 10:22:43 +01:00
parent 7d96f52c41
commit 9377fbfb0f
12 changed files with 93 additions and 48 deletions
+12 -10
View File
@@ -414,7 +414,7 @@ Each entry has one or more of the following fields:
## Adding custom or logic for validation and issuing of tokens
The `pluginTokenHandlerDecoratorServiceRef` and `externalTokenHandlerDecoratorServiceRef` can be used to decorate the existing token handler without having to re-implement the entire `AuthService` implementation.
The `pluginTokenHandlerDecoratorServiceRef` and `externalTokenHandlersServiceRef` can be used to extend the existing token handler without having to re-implement the entire `AuthService` implementation.
This is particularly useful when you want to add additional logic to the handler, such as logging or metrics or custom token validation.
### PluginTokenHandler decoration
@@ -446,25 +446,27 @@ const decoratedPluginTokenHandler = createServiceFactory({
### ExternalTokenHandler decoration
The `externalTokenHandlerDecoratorServiceRef` can be used to decorate the default ExternalTokenHandler used for verify tokens from external callers.
The `externalTokenHandlersServiceRef` can be used to add custom external token handlers to the default implementation.
The `ExternalTokenHandler` interface has one methods:
The returned object should be a map of token custom types and their handler factories. The object keys will be matched to the configured `externalAccess` type in the app-config, calling the factory function with the config object for that type. Custom token handlers should implement the `TokenHandler` interface, which provides methods for verifying tokens.
- `verifyToken`: This method is used to verify a token. It takes in the token as an argument and returns a promise that resolves to an object containing the subject of the token and an optional limited user token.
For example, to add a custom token handler for a type called 'custom':
```ts
import {
ExternalTokenHandler,
externalTokenHandlerDecoratorServiceRef,
TokenHandler,
externalTokenHandlersServiceRef,
} from '@backstage/backend-defaults/auth';
import { Config } from '@backstage/config';
import { createServiceFactory } from '@backstage/backend-plugin-api';
const decoratedPluginTokenHandler = createServiceFactory({
service: externalTokenHandlerDecoratorServiceRef,
const customExternalTokenHandlers = createServiceFactory({
service: externalTokenHandlersServiceRef,
deps: {},
async factory() {
return (defaultImplementation: ExternalTokenHandler) =>
new CustomTokenHandler(defaultImplementation);
return {
custom: (config: Config) => new CustomTokenHandler(config);
};
},
});
```