AzureAD passport-azure-ad 使用教程

AzureAD passport-azure-ad 使用教程

passport-azure-ad The code for Passport Azure AD has been moved to the MSAL.js repo. Please open any issues or PRs at the link below. passport-azure-ad 项目地址: https://gitcode.com/gh_mirrors/pa/passport-azure-ad

1. 项目介绍

passport-azure-ad 是一个开源项目,提供了一套用于集成 Azure Active Directory (Azure AD) 认证和授权的 Passport.js 策略。该项目支持 OpenID Connect、WS-Federation 和 SAML-P 协议,使得 Node.js 应用能够利用 Azure AD 的功能,例如 Web 单点登录(WebSSO)、OAuth 端点保护和 JWT 令牌的签发与验证。

2. 项目快速启动

首先,确保您的 Node.js 环境已经安装并配置正确。

安装

通过 npm 安装 passport-azure-ad

npm install passport-azure-ad

配置

创建一个新的文件(例如 config.js),并添加以下配置信息:

module.exports = {
  creds: {
    identityMetadata: 'https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration',
    clientID: 'YOUR_CLIENT_ID',
    audience: 'YOUR_TENANT_ID',
    responseType: 'code',
    responseMode: 'query',
    redirectUrl: 'YOUR_REDIRECT_URL',
    clientSecret: 'YOUR_CLIENT_SECRET',
    validateIssuer: false,
    isB2C: false,
    issuer: 'https://login.microsoftonline.com/YOUR_TENANT_ID/v2.0',
    scope: ['openid', 'profile', 'email'],
    loggingLevel: 'info',
    nonceLifetime: 3600,
    nonceMaxAmount: 10,
    useCookieInsteadOfSession: false,
    cookieSameSite: 'Lax',
    cookieEncryptionKeys: undefined,
    clockSkew: 300,
  }
};

确保替换 YOUR_CLIENT_IDYOUR_TENANT_IDYOUR_REDIRECT_URLYOUR_CLIENT_SECRET 为您的 Azure AD 应用程序的实际值。

使用

在你的 Node.js 应用程序的 server.js 或主文件中,引入 passport-azure-ad 并配置策略:

const passport = require('passport');
const OIDCStrategy = require('passport-azure-ad').OIDCStrategy;
const config = require('./config');

const strategy = new OIDCStrategy(config.creds, (iss, sub, profile, cb) => {
  // 在这里,你可以根据 profile 来查找或创建用户
  return cb(null, profile);
});

passport.use(strategy);

// 其余的 Express 应用程序设置...

在 Express 应用程序中,使用 passport 中间件来处理认证:

app.get('/auth/openid', passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }));
app.post('/auth/openid/callback', passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }), (req, res) => {
  // 成功认证后的逻辑
});

3. 应用案例和最佳实践

案例一:Web 应用程序的单一登录

使用 OIDCStrategy 实现 Azure AD 的单一登录功能,用户可以在登录后访问受保护的应用程序资源。

最佳实践

  • 确保 validateIssuer 设置正确以验证令牌的颁发者。
  • 使用 HTTPS 以保护认证过程中的数据传输。
  • 不要在客户端存储敏感信息,如客户端密钥。

4. 典型生态项目

  • passport-azure-ad: 用于 Azure AD 认证的 Passport.js 策略。
  • microsoft-authentication-library-for-js: 提供了用于 JavaScript 的 Microsoft 身份验证库。

以上是 passport-azure-ad 的基本使用教程。在实际应用中,您可能需要根据具体的业务需求进行调整和扩展。

passport-azure-ad The code for Passport Azure AD has been moved to the MSAL.js repo. Please open any issues or PRs at the link below. passport-azure-ad 项目地址: https://gitcode.com/gh_mirrors/pa/passport-azure-ad

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡寒侃Joe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值