代码之家  ›  专栏  ›  技术社区  ›  lexma

使用Passport SAML检索RelayState值

  •  0
  • lexma  · 技术社区  · 4 年前

    我无法检索RelayState值。有人能告诉我或提供相关文件的链接吗。

    这就是我目前的设置和路线。

    安装程序

    const saml_strategy = new saml.Strategy(
        {
            'callbackUrl': 'https://callback-url/adfs/postResponse',
            'entryPoint': 'https://entry-url/adfs/ls/',
            'issuer': 'issuer-name',
            'decryptionPvk': fs.readFileSync('./private.key', 'utf-8'),
            'cert': [idp_cert],
            'identifierFormat': null,
       'disableRequestedAuthnContext': true,
       'authnContext': ["urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"],
       'additionalParams':{'RelayState':'test'}
        }, 
    

    路线

    app.get(
        '/', 
        function (req, res) {
            if (req.isAuthenticated()) 
            {
               res.send( '{ "authenticated": true }' );
            } 
            else 
            {
                res.send( '{ "authenticated":  false }' );
            }
        }
    );
    
    app.get(
        '/login', 
        passport.authenticate('saml', { 'successRedirect': '/', 'failureRedirect': '/login' }),
        function(req, res) 
          {
            res.redirect('/');
          }
    );
    
    ///login/callback
    app.post(
        '/adfs/postResponse',
        passport.authenticate('saml', { 'failureRedirect': '/', 'failureFlash': true }),
        function(req, res) 
        {
            res.redirect('/');
        }
    );
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   James    4 年前

    RelayState 从初始登录请求页面的查询字符串中设置,然后(如果IdP尊重它),它在回调POST响应的正文中可用。

    因此,在您的情况下,这应该奏效:

    app.get(
        '/login', 
        function(req, res, next){
            // you could redirect to /login?RelayState=whatever, or set query here,
            // the value must be encoded for passing in the query string:
            req.query.RelayState = encodeURIComponent('my relay state');
        },
        passport.authenticate('saml', { 'successRedirect': '/', 'failureRedirect': '/login' }),
        function(req, res) 
        {
            res.redirect('/');
        }
    );
    
    app.post(
        '/adfs/postResponse',
        passport.authenticate('saml', { 'failureRedirect': '/', 'failureFlash': true }),
        function(req, res) 
        {
            console.log(`relay state was ${decodeURIComponent(req.body.RelayState)}`);
            res.redirect('/');
        }
    );