From 2cd2145a175a874a9e99eedbfb192d13ceb9a3b0 Mon Sep 17 00:00:00 2001 From: Erick Sposito Date: Fri, 15 Jul 2022 15:01:38 -0300 Subject: [PATCH 1/3] Adjustment of sample code without We made an adjustment to the documentation because with the example we couldn't make it work. Following the steps we put in the current example works perfectly. @padupe @monnylly @ericksposito Signed-off-by: Erick Sposito --- docs/auth/identity-resolver.md | 86 ++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/docs/auth/identity-resolver.md b/docs/auth/identity-resolver.md index 06ada040e4..72ac91b288 100644 --- a/docs/auth/identity-resolver.md +++ b/docs/auth/identity-resolver.md @@ -273,46 +273,60 @@ that the user belongs to using the user access token in the provided result obje ```ts // File: packages/backend/src/plugins/auth.ts -import { DEFAULT_NAMESPACE, stringifyEntityRef, } from '@backstage/catalog-model'; +import { + createRouter, + defaultAuthProviderFactories, + providers, +} from '@backstage/plugin-auth-backend'; +import { Router } from 'express'; +import { PluginEnvironment } from '../types'; +import { + stringifyEntityRef, + DEFAULT_NAMESPACE, +} from '@backstage/catalog-model'; export default async function createPlugin( - // ... - return await createRouter({ - // ... + env: PluginEnvironment, +): Promise { + return createRouter({ + ...env, providerFactories: { - // ... - google: async ({ profile }, ctx) => { - if (!profile.email) { - throw new Error( - 'Login failed, user profile does not contain an email', - ); - } - // Split the email into the local part and the domain. - const [localPart, domain] = profile.email.split('@'); - - // Next we verify the email domain. It is recommended to include this - // kind of check if you don't look up the user in an external service. - if (domain !== 'acme.org') { - throw new Error('Login failed, user email domain check failed'); - } - - // By using `stringifyEntityRef` we ensure that the reference is formatted correctly - const userEntityRef = stringifyEntityRef({ - kind: 'User', - name: localPart, - namespace: DEFAULT_NAMESPACE, - }); - - return ctx.issueToken({ - claims: { - sub: userEntityRef, - ent: [userEntityRef], + ...defaultAuthProviderFactories, + google: providers.google.create({ + signIn: { + resolver: async ({ profile }, ctx) => { + if (!profile.email) { + throw new Error('Login Failed'); + } + // Split the email into the local part and the domain. + const [localPart, domain] = profile.email.split('@'); + + // Next we verify the email domain. It is recommended to include this + // kind of check if you don't look up the user in an external service. + if (domain !== 'acme.org') { + throw new Error( + `Login failed, this email ${profile.email} does not belong to the expected domain`, + ); + } + + // By using `stringifyEntityRef` we ensure that the reference is formatted correctly + const userEntity = stringifyEntityRef({ + kind: 'User', + name: localPart, + namespace: DEFAULT_NAMESPACE, + }); + return ctx.issueToken({ + claims: { + sub: userEntity, + ent: [userEntity], + }, + }); }, - }); - }, - } - }) -) + }, + }), + }, + }); +} ``` ## AuthHandler From 360d6487259019cfffb0e8beb98a5484cac515ba Mon Sep 17 00:00:00 2001 From: Erick Sposito Date: Fri, 15 Jul 2022 17:17:44 -0300 Subject: [PATCH 2/3] Update identity-resolver.md Signed-off-by: Erick Sposito --- docs/auth/identity-resolver.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/auth/identity-resolver.md b/docs/auth/identity-resolver.md index 72ac91b288..95f54bb5ea 100644 --- a/docs/auth/identity-resolver.md +++ b/docs/auth/identity-resolver.md @@ -275,7 +275,6 @@ that the user belongs to using the user access token in the provided result obje // File: packages/backend/src/plugins/auth.ts import { createRouter, - defaultAuthProviderFactories, providers, } from '@backstage/plugin-auth-backend'; import { Router } from 'express'; @@ -288,15 +287,14 @@ import { export default async function createPlugin( env: PluginEnvironment, ): Promise { - return createRouter({ + return await createRouter({ ...env, providerFactories: { - ...defaultAuthProviderFactories, google: providers.google.create({ signIn: { resolver: async ({ profile }, ctx) => { if (!profile.email) { - throw new Error('Login Failed'); + throw new Error('Login failed, user profile does not contain an email'); } // Split the email into the local part and the domain. const [localPart, domain] = profile.email.split('@'); From 9c0e4e24fe63f28d125a9651506c76d56c804e61 Mon Sep 17 00:00:00 2001 From: Erick Sposito Date: Mon, 18 Jul 2022 09:14:03 -0300 Subject: [PATCH 3/3] applying prettier after doc change Signed-off-by: Erick Sposito --- docs/auth/identity-resolver.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/auth/identity-resolver.md b/docs/auth/identity-resolver.md index 95f54bb5ea..b9944508c6 100644 --- a/docs/auth/identity-resolver.md +++ b/docs/auth/identity-resolver.md @@ -273,10 +273,7 @@ that the user belongs to using the user access token in the provided result obje ```ts // File: packages/backend/src/plugins/auth.ts -import { - createRouter, - providers, -} from '@backstage/plugin-auth-backend'; +import { createRouter, providers } from '@backstage/plugin-auth-backend'; import { Router } from 'express'; import { PluginEnvironment } from '../types'; import { @@ -294,11 +291,13 @@ export default async function createPlugin( signIn: { resolver: async ({ profile }, ctx) => { if (!profile.email) { - throw new Error('Login failed, user profile does not contain an email'); + throw new Error( + 'Login failed, user profile does not contain an email', + ); } // Split the email into the local part and the domain. const [localPart, domain] = profile.email.split('@'); - + // Next we verify the email domain. It is recommended to include this // kind of check if you don't look up the user in an external service. if (domain !== 'acme.org') { @@ -306,7 +305,7 @@ export default async function createPlugin( `Login failed, this email ${profile.email} does not belong to the expected domain`, ); } - + // By using `stringifyEntityRef` we ensure that the reference is formatted correctly const userEntity = stringifyEntityRef({ kind: 'User',