使用 Passport-saml 注销:req.logout() 或 Strategy.logout(),或两者?

2023-11-22

我有一个关于使用 Passport-saml 进行身份验证时注销用户的正确方法的问题。

使用 Passport-saml 的示例脚本显示注销如下:

app.get('/logout', function(req, res){
  req.logout();
  res.redirect('/');
});

据我所知,这将结束本地护照会话,但它似乎不会向 SAML IdP 发送注销请求。当用户再次登录时,它会重定向到 IdP,但会立即重定向回经过身份验证的用户。有没有办法通过 IdP 注销,以便用户在登录我的网站时必须再次输入密码?我见过其他使用我们的 IdP 的网站也这样做,所以我认为这是可能的。

我确实注意到在passport-saml代码中有一个logout()Passport-saml 策略对象上的方法,该方法似乎没有被调用req.logout()。所以我尝试将代码切换为:

app.get('/logout', function(req, res) {
    //strategy is a ref to passport-saml Strategy instance 
    strategy.logout(req, function(){
        req.logout();
        res.redirect('/');
    });
});

但我在 XMLNode.js 深处遇到了这个错误

Error: Could not create any elements with: [object Object]
   at XMLElement.module.exports.XMLNode.element (/.../node_modules/passport-saml/node_modules/xmlbuilder/lib/XMLNode.js:74:15)
   at XMLElement.module.exports.XMLNode.element (/.../node_modules/passport-saml/node_modules/xmlbuilder/lib/XMLNode.js:54:25)
   at XMLElement.module.exports.XMLNode.element (/.../node_modules/passport-saml/node_modules/xmlbuilder/lib/XMLNode.js:54:25)
   at new XMLBuilder (/.../node_modules/passport-saml/node_modules/xmlbuilder/lib/XMLBuilder.js:27:19)
   at Object.module.exports.create (/.../node_modules/passport-saml/node_modules/xmlbuilder/lib/index.js:11:12)
   at SAML.generateLogoutRequest (/.../node_modules/passport-saml/lib/passport-saml/saml.js:169:21)

我没有正确调用这个方法吗?或者我不应该直接调用这个方法并调用其他方法?

我看到在generateLogoutRequest()它指的是两个属性req.user我不确定是否存在:

  'saml:NameID' : {
    '@Format': req.user.nameIDFormat,
    '#text': req.user.nameID
  }

如果这些属性不存在,会导致此错误吗?如果是这样,我认为也许我需要确保将这些属性添加到从验证回调函数返回的用户对象中?

感谢任何人都可以为此提供的任何帮助。


是的,将 nameIDFormat 和 nameID 添加到用户将解决该问题。

  1. 要启用注销,您应该在策略中配置 logoutURL 选项

logoutUrl: 'http://example.org/simplesaml/saml2/idp/SingleLogoutService.php',

策略中的logout方法实际上并不发送任何请求。以请求作为参数调用回调函数。

要启动注销过程:

passport.logoutSaml = function(req, res) {
    //Here add the nameID and nameIDFormat to the user if you stored it someplace.
    req.user.nameID = req.user.saml.nameID;
    req.user.nameIDFormat = req.user.saml.nameIDFormat;


    samlStrategy.logout(req, function(err, request){
        if(!err){
            //redirect to the IdP Logout URL
            res.redirect(request);
        }
    });
};

编辑:nameId 和 nameId 格式必须在成功登录后保存在某处

var samlStrategy = new SamlStrategy(
  {
    callbackUrl: 'https://mydomain/auth/saml/callback',
    entryPoint: 'https://authprovider/endpoint',
    logoutUrl: 'https://authprovider/logoutEndPoint',
    issuer: 'passport-saml'
  },
  function(profile, done) {

      //Here save the nameId and nameIDFormat somewhere
      user.saml = {};
      user.saml.nameID = profile.nameID;
      user.saml.nameIDFormat = profile.nameIDFormat;

      //Do save

      });
  });
  1. 您还必须为注销回调创建一个端点:

应在 IdP 配置的 SP 元数据中配置此 URL。注销完成后,IdP 将重定向到该 URL。

在你的路线中:

app.post('/auth/saml/logout/callback', passport.logoutSamlCallback);

在您的护照配置中:

passport.logoutSamlCallback = function(req, res){
    req.logout();
    res.redirect('/');
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Passport-saml 注销:req.logout() 或 Strategy.logout(),或两者? 的相关文章

随机推荐