首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Passport-SAML:读取用户信息

Passport-SAML:读取用户信息
EN

Stack Overflow用户
提问于 2017-08-09 03:18:52
回答 3查看 3.1K关注 0票数 3

还是个菜鸟!

我正在构建一个Node应用程序,并且已经设置了各种所需的端点。我的项目的需求之一是使用SAML机制进行身份验证。我在应用程序中使用passport-SAML进行身份验证。

到目前为止,我已经能够设置和使用SAML策略,并且我的应用程序能够调用Idp入口点,并从idp接收返回的响应。

我无法理解如何访问idp返回的用户信息,以便使用SAML返回的用户信息来创建和维护会话。

代码语言:javascript
复制
const saml = require('passport-saml');

module.exports = function (passport, config) {

  passport.serializeUser(function (user, done) {
    done(null, user);
  });

  passport.deserializeUser(function (user, done) {
    done(null, user);
  });

  var samlStrategyOptions = new saml.Strategy(
    {
      // URL that goes from the Identity Provider -> Service Provider
      callbackUrl: config.passport.saml.callback_url,
      // path: config.passport.saml.path,
      // URL that goes from the Service Provider -> Identity Provider
      entryPoint: config.passport.saml.entryPoint,
      issuer: config.passport.saml.issuer,
      identifierFormat: null,
      // Service Provider private key
      decryptionPvk: config.passport.saml.decryptionPvk,
      // Service Provider Certificate
      privateCert: config.passport.saml.privateCert,
      // Identity Provider's public key
      cert: config.passport.saml.cert,
      validateInResponseTo: false,
      disableRequestedAuthnContext: true
    },
    function (profile, done) {
      return done(null,
        {
          id: profile.uid,
          email: profile.email,
          displayName: profile.cn,
          firstName: profile.givenName,
          lastName: profile.sn
        });
    })


  // module.exports.samlStrategyOptions = samlStrategyOptions  ;
  passport.use(samlStrategyOptions);

};

以下是我的快车路线控制器

代码语言:javascript
复制
router.route('/login')

.get(
    passport.authenticate(config.passport.strategy,
      {
        successRedirect: '/',
        failureRedirect: '/login'
      })
);

router.route('/login/callback/')

.post(
    passport.authenticate(config.passport.strategy,
      {
        failureRedirect: '/',
        failureFlash: true
      }),
    function (req, res) {

      res.redirect('/');
    }
);

这是我从Idp收到的响应属性的SAML片段。

代码语言:javascript
复制
<saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">Shubham123</saml:NameID>
EN

回答 3

Stack Overflow用户

发布于 2017-12-18 20:58:11

我也是这么想的。所以我使用body-parser作为中间件。

代码语言:javascript
复制
 // middleware to parse HTTP POST's JSON, buffer, string,zipped or raw and URL encoded data and exposes it on req.body
app.use(bodyParser.json());
// use querystring library to parse x-www-form-urlencoded data for flat data structure (not nested data)
app.use(bodyParser.urlencoded({ extended: false }));

然后你会得到这样的配置文件

代码语言:javascript
复制
{ issuer: '',
   sessionIndex: '_x0P5ZeWx-ACSQAulKgVTxSquNsVdac_H',
   nameID: 'auth0|5a266569083226773d5d43a9',
   nameIDFormat: 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified',
   nameQualifier: undefined,
   spNameQualifier: undefined,
   'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': 'auth0|s9ds',
   'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress': 'myuser@q.com',
   'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'myuser@q.com',
   'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn': 'myuser@q.com',
   'http://schemas.auth0.com/identities/default/provider': 'auth0',
   'http://schemas.auth0.com/identities/default/connection': 'Username-Password-Authentication',
   'http://schemas.auth0.com/identities/default/isSocial': 'false',
   'http://schemas.auth0.com/email_verified': 'false',
   'http://schemas.auth0.com/clientID': 'bZVOM5KQmhyir5xEYhLHGRAQglks2AIp',
   'http://schemas.auth0.com/picture': 'https://s.gravatar.com/avatar/e85e57405a82225ff36b5af793ed287c?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fsu.png',
   'http://schemas.auth0.com/nickname': 'myuser',
   'http://schemas.auth0.com/identities': '[object Object]',
   'http://schemas.auth0.com/updated_at': 'Mon Dec 18 2017 12:14:28 GMT+0000 (UTC)',
   'http://schemas.auth0.com/created_at': 'Tue Dec 05 2017 09:22:49 GMT+0000 (UTC)',
   getAssertionXml: [Function] }

并通过提取如下数据来创建用户

代码语言:javascript
复制
{ id: profile["nameID"], userName: profile["http://schemas.auth0.com/nickname"] }
票数 0
EN

Stack Overflow用户

发布于 2020-10-15 12:34:51

为了获取用户详细信息,在IDP的控制台中,您必须在SP设置中设置您希望IDP返回的参数,并在断言中获取这些参数。这是我在onelogin中所做的:

票数 0
EN

Stack Overflow用户

发布于 2021-04-13 00:21:00

我正在使用node-saml passport module,我发现this example非常有用。总而言之,一旦解析了SAML流程(您的IdP正在对您的处理程序进行POST回调),用户数据就存储在request对象中。现在,如果您想要获取用户数据,例如在任何get请求中,您可以执行以下操作:

代码语言:javascript
复制
    app.get('/logout', function(req, res) {
    console.log('logout');
    console.log(req.user);
    req.logout();
    res.redirect(config.passport.saml.logoutCallback);
});

其中req.user包含您所有的用户数据。在该示例中,req.user包含以下内容:

代码语言:javascript
复制
{
firstName: 'local givenName',
lastName: 'local lastname',
email: 'testUser@sample.com'
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45576447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档