护照-SAML:读取用户信息

3

我还是个新手!

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

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

我不知道如何访问idp返回的用户信息,以便我可以使用SAML返回的用户信息来创建和维护会话。

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);

};

以下是我的Express路由控制器

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代码片段。

<saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">Shubham123</saml:NameID>

你好,能否分享一下你是如何从响应中提取SAML XML的? - Sumit
1
@Sumit,回应您的问题,这里有三个可用的函数:getAssertion、getAssertionXml和getSamlResponseXml。 - Ankit
3个回答

0

我也遇到了同样的问题。所以我使用了body-parser作为中间件。

 // 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 }));

然后您将获得类似于以下的配置文件

{ 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] }

通过提取数据创建用户

{ id: profile["nameID"], userName: profile["http://schemas.auth0.com/nickname"] }

0
为了获取用户详细信息,在您的IDP控制台中,您必须设置SP设置中您想要IDP返回的参数,并在assertion中获取它们。 这就是我在onelogin中所做的: {{link1:onelogin}}

0

我正在使用node-saml passport模块,并且我发现这个示例非常有用。 总结一下,一旦SAML过程解决(您的IdP正在向处理程序进行POST回调),用户数据将存储在请求对象中。现在,如果您想获取该用户数据,例如在任何GET请求中,您可以执行以下操作:

    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包含以下内容:

{
firstName: 'local givenName',
lastName: 'local lastname',
email: 'testUser@sample.com'
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接