Skip to content

Commit

Permalink
fix: resolve the usernames if possible otherwise just log in. part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
quiiver committed Oct 29, 2024
1 parent 5e3004d commit 54cc360
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
DEFAULT_NAMESPACE,
} from '@backstage/catalog-model';

export const gcpIapCustomAuth = createBackendModule({
export const gcpIapCustomAuthProvider = createBackendModule({
pluginId: 'auth',
moduleId: 'custom-gcp-iap-auth-provider',
register(reg) {
Expand All @@ -36,26 +36,24 @@ export const gcpIapCustomAuth = createBackendModule({
);
}

const catalogUser = await ctx.findCatalogUser({
entityRef: { name },
});

if (catalogUser && catalogUser.entity) {
// try to resolve an existing gh username to the name part of the email
// otherwise, issue a log in token.
try {
return ctx.signInWithCatalogUser({ entityRef: { name } });
}
} catch (_) {
const userEntity = stringifyEntityRef({
kind: 'User',
name,
namespace: DEFAULT_NAMESPACE,
});

const userEntity = stringifyEntityRef({
kind: 'User',
name,
namespace: DEFAULT_NAMESPACE,
});

return ctx.issueToken({
claims: {
sub: userEntity,
ent: [userEntity],
},
});
return ctx.issueToken({
claims: {
sub: userEntity,
ent: [userEntity],
},
});
}
},
}),
});
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { createBackend } from '@backstage/backend-defaults';
import { gcpIapCustomAuth } from './gcp-iap-auth-provider';
import { gcpIapCustomAuthProvider } from './auth';

const backend = createBackend();

Expand All @@ -20,7 +20,7 @@ backend.add(import('@backstage/plugin-techdocs-backend/alpha'));
backend.add(import('@backstage/plugin-auth-backend'));
// See https://backstage.io/docs/backend-system/building-backends/migrating#the-auth-plugin
backend.add(import('@backstage/plugin-auth-backend-module-guest-provider'));
backend.add(gcpIapCustomAuth);
backend.add(gcpIapCustomAuthProvider);
// See https://backstage.io/docs/auth/guest/provider

// catalog plugin
Expand Down

0 comments on commit 54cc360

Please sign in to comment.