java_doctor_101
java_doctor_101

Reputation: 3357

Trying to understand Node callback syntax

I am reverse engineering one of the open source implementations of SAML based Single Sign on (SSO)

When the SSO is successful I get a POST from my idp(identity provider) and following function is called:

router.post('/acs/:idp?', function (req, res, next) {
  console.log('got a post from idp');
    var _idp, _sp;
    if (req.params.idp === 'onelogin') {
      console.log('the idp is onelogin or vidm in this case');
        _idp = oneLoginIdP;
        _sp = olsp;
    } else {
        _idp = idp;
        _sp = sp;
    }
    _sp.parseLoginResponse(_idp, 'post', req, function (parseResult) {
      console.log('trying to parse assertion to see if it is valid');
      console.log('name id'+parseResult.extract.nameid);

        if (parseResult.extract.nameid) {
            res.render('login', {
                title: 'Processing',
                isSSOLogin: true,
                email: parseResult.extract.nameid
            });
        } else {
            req.flash('info', 'Unexpected error');
            res.redirect('/login');
        }
    });
});

Now, as we can see this function calls a function called parseLoginResponse on the serverivceprovider object (_sp). parseLoginResponse looks like following:

ServiceProvider.prototype.parseLoginResponse = function parseLoginResponse(idp, binding, req, parseCallback) {
        return this.abstractBindingParser({
            parserFormat:
            [
            {
                localName: 'StatusCode',
                attributes: ['Value']
            },
            {
                localName: 'Conditions',
                attributes: ['NotBefore', 'NotOnOrAfter']
            },
            'Audience',
            'Issuer',
            'NameID',
            {
                localName: 'Signature',
                extractEntireBody: true
            },
            {
                localName: {
                    tag: 'Attribute',
                    key: 'Name'
                },
                valueTag: 'AttributeValue'
            }
          ],
            checkSignature: this.entityMeta.isWantAssertionsSigned(),
            from: idp,
            supportBindings: ['post'],
            parserType: 'SAMLResponse',
            actionType: 'login'
        }, binding, req, idp.entityMeta, parseCallback);
    };

My three specific Questions:

  1. How is the callback working for parseCallback method.

  2. I am new to javascript so I don't get at which exact line parseCallback is receiving it's argument i.e parseResult?

  3. I can print following line succefully in my parseCallback:

    console.log('name id'+parseResult.extract.nameid);
    

BUT I can't find a way to print attributes that contain notbefore and notonorafter time. How can I print attributes section of parseResult or the commplete parseResult argument?

Upvotes: 0

Views: 69

Answers (1)

Sergey Yarotskiy
Sergey Yarotskiy

Reputation: 4804

Method this.abstractBindingParser(); Takes 5 arguments. Last of them - parseCallback function. So you passing this function inside this.abstractBindingParser(). It will be called internally inside this.abstractBindingParser();

You don't know how exactly it is called internally, which params are passed to parseCallback. You can refer to documentation for system that you use, or you can use magic variable arguments. Place console.log(arguments); after console.log('name id' + parseResult.extract.nameid);

Upvotes: 3

Related Questions