Skip to main content

Azure EasyAuth 提供程序

Backstagecore-plugin-api软件包自带 Microsoft 身份验证提供程序,可使用 Microsoft Entra ID(前身为 Azure Active Directory)为 Azure 托管的支持 Easy Auth 的 PaaS 服务(如 Azure App Services)验证用户身份。

Backstage Changes

在您的app-config.yamlapp-config.production.yaml文件

auth:
environment: development
providers:
azure-easyauth:
development: {}

添加一个providerFactories中的路由器条目。packages/backend/src/plugins/auth.ts.

import { providers } from '@backstage/plugin-auth-backend';

export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
const authProviderFactories = {
'azure-easyauth': providers.easyAuth.create({
signIn: {
resolver: async (info, ctx) => {
const {
fullProfile: { id },
} = info.result;

if (!id) {
throw new Error('User profile contained no id');
}

return await ctx.signInWithCatalogUser({
annotations: {
'graph.microsoft.com/user-id': id,
},
});
},
},
}),
};

return await createRouter({
logger: env.logger,
config: env.config,
database: env.database,
discovery: env.discovery,
tokenManager: env.tokenManager,
providerFactories: authProviderFactories,
});
}

现在后端已经准备就绪,可以在/api/auth/azure-easyauth/refresh剩下的工作就是更新前端登录机制,以用户的名义通过 IAP 轮询该端点。

前端更改

要使用该组件,您需要配置应用程序的SignInPage建议使用ProxiedSignInPage但对于本地开发(或在 Azure 之外运行的任何其他场景),您需要设置不同的内容。 为了获得最接近 Easy Auth 的体验,您可以设置microsoft但这需要设置应用程序注册和Secret,而这可能会被贵组织锁定,在这种情况下,在本地使用访客登录可能会更方便。 请参见使用代理提供商登录了解更多详情。

packages/app/src/App.tsx
import { ProxiedSignInPage } from '@backstage/core-components';

const app = createApp({
components: {
SignInPage: props => {
const configApi = useApi(configApiRef);
if (configApi.getString('auth.environment') !== 'development') {
return <ProxiedSignInPage {...props} provider="azure-easyauth" />;
}
return (
<SignInPage
{...props}
providers={['guest', 'custom']}
title="Select a sign-in method"
align="center"
/>
);
},
},
// ..
});

Azure 配置

如何配置 azure 取决于您用来托管 Backstage 的 Azure 服务。

Azure 应用服务

要将 EasyAuth 与 App 服务一起使用,请打开 Entra ID(以前的 Azure Active Directory)身份验证 您还必须启用令牌存储。

下面的示例展示了如何通过二头肌模板来实现这一功能:

resource webApp 'Microsoft.Web/sites@2022-03-01' existing = {
name: 'MY-WEBAPP-NAME'

resource authConfig 'config' = {
name: 'authsettingsV2'
properties: {
globalValidation: {
redirectToProvider: 'AzureActiveDirectory'
requireAuthentication: true
unauthenticatedClientAction: 'RedirectToLoginPage'
}
login: {
tokenStore: {
enabled: true
}
}
platform: {
enabled: true
}
identityProviders: {
azureActiveDirectory: {
enabled: true
login: {
loginParameters: [ 'domain_hint=MYCOMPANY.COM' ]
}
registration: {
clientId: 'CLIENT-ID'
clientSecretSettingName: 'CLIENT-SECRET-NAME'
openIdIssuer: 'https://sts.windows.net/${tenant().tenantId}/v2.0'
}
}
}
}
}
}