Skip to content

Commit

Permalink
fix(bazaar-backend): use the new auth system
Browse files Browse the repository at this point in the history
Signed-off-by: Niklas Aronsson <[email protected]>
  • Loading branch information
Niklas Aronsson committed Sep 12, 2024
1 parent 4b9cb6e commit 74ea94e
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 123 deletions.
24 changes: 24 additions & 0 deletions workspaces/bazaar/.changeset/light-dots-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@backstage-community/plugin-bazaar-backend': minor
---

**BREAKING**: The `discovery` service is now required if using the old backend
system. Migrated to support new auth services.

```diff
import { PluginEnvironment } from '../types';
import { createRouter } from '@backstage-community/plugin-bazaar-backend';
import { Router } from 'express';

export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
return await createRouter({
logger: env.logger,
config: env.config,
database: env.database,
+ discovery: env.discovery,
identity: env.identity,
});
}
```
1 change: 1 addition & 0 deletions workspaces/bazaar/plugins/bazaar-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default async function createPlugin(
logger: env.logger,
config: env.config,
database: env.database,
discovery: env.discovery,
identity: env.identity,
});
}
Expand Down
5 changes: 5 additions & 0 deletions workspaces/bazaar/plugins/bazaar-backend/dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createBackend } from '@backstage/backend-defaults';

const backend = createBackend();
backend.add(import('../src/alpha'));
backend.start();
6 changes: 2 additions & 4 deletions workspaces/bazaar/plugins/bazaar-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@
},
"dependencies": {
"@backstage/backend-common": "^0.24.0",
"@backstage/backend-defaults": "^0.4.2",
"@backstage/backend-plugin-api": "^0.8.0",
"@backstage/config": "^1.2.0",
"@backstage/plugin-auth-node": "^0.5.0",
"@types/express": "^4.17.6",
"express": "^4.17.1",
"express-promise-router": "^4.1.0",
"knex": "^3.0.0",
"yn": "^4.0.0"
"knex": "^3.0.0"
},
"devDependencies": {
"@backstage/backend-test-utils": "^0.5.0",
Expand Down
19 changes: 14 additions & 5 deletions workspaces/bazaar/plugins/bazaar-backend/src/alpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@ export default createBackendPlugin({
deps: {
config: coreServices.rootConfig,
database: coreServices.database,
identity: coreServices.identity,
logger: coreServices.logger,
discovery: coreServices.discovery,
httpAuth: coreServices.httpAuth,
httpRouter: coreServices.httpRouter,
logger: coreServices.logger,
},
async init({ database, config, identity, logger, httpRouter }) {
async init({
config,
database,
discovery,
httpAuth,
httpRouter,
logger,
}) {
httpRouter.use(
await createRouter({
database,
config,
identity,
database,
discovery,
httpAuth,
logger,
}),
);
Expand Down
33 changes: 0 additions & 33 deletions workspaces/bazaar/plugins/bazaar-backend/src/run.ts

This file was deleted.

31 changes: 22 additions & 9 deletions workspaces/bazaar/plugins/bazaar-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,38 @@
* limitations under the License.
*/

import { errorHandler, PluginDatabaseManager } from '@backstage/backend-common';
import {
errorHandler,
PluginDatabaseManager,
createLegacyAuthAdapters,
} from '@backstage/backend-common';
import express from 'express';
import Router from 'express-promise-router';
import { Config } from '@backstage/config';
import { IdentityApi } from '@backstage/plugin-auth-node';
import { DatabaseHandler } from './DatabaseHandler';
import { LoggerService } from '@backstage/backend-plugin-api';
import {
DiscoveryService,
HttpAuthService,
IdentityService,
LoggerService,
RootConfigService,
} from '@backstage/backend-plugin-api';

/** @public */
export interface RouterOptions {
logger: LoggerService;
database: PluginDatabaseManager;
config: Config;
identity: IdentityApi;
discovery: DiscoveryService;
config: RootConfigService;
identity?: IdentityService;
httpAuth?: HttpAuthService;
}

/** @public */
export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger, database, identity } = options;
const { logger, database } = options;
const { httpAuth } = createLegacyAuthAdapters(options);

const dbHandler = await DatabaseHandler.create({ database });

Expand All @@ -55,12 +66,14 @@ export async function createRouter(

router.put('/projects/:id/member/:userId', async (request, response) => {
const { id, userId } = request.params;
const user = await identity.getIdentity({ request: request });
const credentials = await httpAuth.credentials(request, {
allow: ['user'],
});

await dbHandler.addMember(
parseInt(id, 10),
userId,
user?.identity.userEntityRef,
credentials.principal.userEntityRef,
request.body?.picture,
);

Expand Down

This file was deleted.

4 changes: 1 addition & 3 deletions workspaces/bazaar/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2517,16 +2517,14 @@ __metadata:
resolution: "@backstage-community/plugin-bazaar-backend@workspace:plugins/bazaar-backend"
dependencies:
"@backstage/backend-common": ^0.24.0
"@backstage/backend-defaults": ^0.4.2
"@backstage/backend-plugin-api": ^0.8.0
"@backstage/backend-test-utils": ^0.5.0
"@backstage/cli": ^0.27.0
"@backstage/config": ^1.2.0
"@backstage/plugin-auth-node": ^0.5.0
"@types/express": ^4.17.6
express: ^4.17.1
express-promise-router: ^4.1.0
knex: ^3.0.0
yn: ^4.0.0
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 74ea94e

Please sign in to comment.