We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    I'm trying to create an SSO login for a couple of modx sites using the hybridAuth library and authenticate against google, facebook, linkedin etc. Got the library working and authenticating users fine and passing info back to my modx instance just fine. But now I'm running into issues creating sessions for the user, consider this:

    // this gets the modx user by email address from the returned social profile
    $modx_user = get_modx_user_from_email($modx, $social_profile);
    
    // create a user object from the internal key [added to the social profile earlier]
    $user = $modx->getObject('modUser', $modx_user['internalKey']);
    
    // this seems to give the user a session, but no permissions to do anything.
    $user -> addSessionContext('web');
    
    // if the user successfully authenticates against google, this will return "is authenticated"
    if (!$modx->user->isAuthenticated('web')) {
         echo 'not authenticated';
    }else{
         echo 'is authenticated';
    }


    This works up to a point, it seems to create a session for the user & cookies in the browser, but the user has no permissions, this:

    [[+modx.user.id]]
    [[+modx.user.username]]


    will return 0 (anonymous)

    so it ~seems~ like the user is authenticated, but can;t do anything. What is the next step here to get the user able to interact with the site??

    Keep in mind, that I do not have a username or password so I can't pass them to the actual login processor. There is however an interesting looking line in the login script,

    $modx->invokeEvent("OnWebLogin", $postLoginAttributes);


    is there something in the OnWebLogin event that is setting the user permissions?
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      If there is no MODX user, then how can MODX get the user's ID and other non-existent properties? You'll have to use OnWebLogin to add the properties you want to the user's SESSION.

      That won't let you use the userinfo output modifier, though, since that uses a getProfile() on the user and your remote users don't have profiles in the MODX database.

      You can make your own output modifiers, though, to pull information from the SESSION - they are just specially crafted snippets.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        You might try adding this above your isAuthenticated() call:

        $modx->user =& $user;


        addSessionContext() should give the user their permissions, but I think they're tested on $modx->user.

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 26503
          • 620 Posts
          @Bob - gave that a try, but didn't seem to do anything.

          @sottwell - I can create a user object and get whatever properties I need from that. The user was authenticated against google [or whatever] and I use the email to find the actual modx user ~ most users will already exist in modx, if they don't it's pretty trivial to create them & log them in using $modx->runProcessor('security/login', array()); but that requires a password ~ which we don't have [unless anyone has any ideas on how to decrypt a users password!] so that process can't be used for existing users. sad

          The process *appears* to be get some user info, set some session variables and update the sessions table. though there are items in the session table that I can't identify what they are, why they are there or how they are used.
            *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

            Sean Kimball CLP, CLS.
            Technical Director / Sr. Developer | BigBlock Studios
            ._______________________________________________.
            Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
            27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
            phone/fax: 905-426-5525
            • 26503
            • 620 Posts
            I think I have a clue?

            Loading modx externally, this is a separate instance of modx I am creating here, right?

            $modx = new modX();

            the instance of modx I'm expecting to see the new user session knows nothing about what is going on in the external instance.

            correct?
              *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

              Sean Kimball CLP, CLS.
              Technical Director / Sr. Developer | BigBlock Studios
              ._______________________________________________.
              Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
              27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
              phone/fax: 905-426-5525
              • 3749
              • 24,544 Posts
              That makes sense. I don't really understand your process. Are you logging the users in somewhere else and then forwarding them to MODX?

              Could you have the users login to a page on the site and then use the OnBeforeWebLogin to authenticate them yourself in some way? That would avoid the session issue and bypass the MODX login code.

              I'm pretty sure that Subscribe logs users in without using their passwords, but I can't find the code for it.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 26503
                • 620 Posts
                Hi Bob;

                addSessionContext() will actually log a user in ~ after bit of testing I found that out, it does seem to be the sessions that are giving me an issue, the hybridauth lib requires it's own session - I can get it to sort of work if I forward the user to a new page after authenticating, but have some security issues with that.



                Quote from: BobRay at May 15, 2015, 06:30 PM
                That makes sense. I don't really understand your process. Are you logging the users in somewhere else and then forwarding them to MODX?

                Could you have the users login to a page on the site and then use the OnBeforeWebLogin to authenticate them yourself in some way? That would avoid the session issue and bypass the MODX login code.

                I'm pretty sure that Subscribe logs users in without using their passwords, but I can't find the code for it.



                  *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                  Sean Kimball CLP, CLS.
                  Technical Director / Sr. Developer | BigBlock Studios
                  ._______________________________________________.
                  Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                  27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                  phone/fax: 905-426-5525
                  • 26503
                  • 620 Posts

                  Hi Bob;

                  addSessionContext() will actually log a user in ~ after bit of testing I found that out, it does seem to be the sessions that are giving me an issue, the hybridauth lib requires it's own session - I can get it to sort of work if I forward the user to a new page after authenticating, but have some security issues with that.



                  Quote from: BobRay at May 15, 2015, 06:30 PM
                  That makes sense. I don't really understand your process. Are you logging the users in somewhere else and then forwarding them to MODX?

                  Could you have the users login to a page on the site and then use the OnBeforeWebLogin to authenticate them yourself in some way? That would avoid the session issue and bypass the MODX login code.

                  I'm pretty sure that Subscribe logs users in without using their passwords, but I can't find the code for it.


                    *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                    Sean Kimball CLP, CLS.
                    Technical Director / Sr. Developer | BigBlock Studios
                    ._______________________________________________.
                    Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                    27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                    phone/fax: 905-426-5525
                    • 3749
                    • 24,544 Posts
                    Can you instantiate MODX before hybridauth creates its session? Maybe you could get hybridauth to use MODX's session.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 26503
                      • 620 Posts

                      I can certainly try that!

                      Quote from: BobRay at May 19, 2015, 05:24 PM
                      Can you instantiate MODX before hybridauth creates its session? Maybe you could get hybridauth to use MODX's session.
                        *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                        Sean Kimball CLP, CLS.
                        Technical Director / Sr. Developer | BigBlock Studios
                        ._______________________________________________.
                        Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                        27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                        phone/fax: 905-426-5525