2

Use Case:

Coming from the client website, the external users need to be log into their customer communities account without having to log again.

I know that I could use SAML for this use case, but unfortunately I couldn't use that with the customer...

So we opted for JWT authentication since that's what they use between their website and Zendesk. What I don't understand is how can I map the user that come from the external website to the right community user.

From what I understand, I need to generate the JWT token and then give it to the identity provider. But then, what else do I need to do for the SSO ? I think that I need to do something more in Salesforce, but don't even know where to begin...

I made a Registration Handler class :

global class CommunityUsersCreation implements Auth.RegistrationHandler {

global User createUser(Id portalId, Auth.UserData data){
    String communityProfile = Label.Customer_Community_Plus_User; 

    List<Account> accounts =  [SELECT Id, PersonContactId FROM Account WHERE Id IN (SELECT AccountId FROM Order)];
        Profile p = [SELECT Id FROM profile WHERE name=:communityProfile LIMIT 1] ;

    User u = new User();
    
    for(Contact c : [SELECT AccountId,Id FROM Contact WHERE AccountId = :accounts]){
        u.username = data.username + '.community';
        u.email = data.email;
        u.lastName = data.lastName;
        u.firstName = data.firstName;
        String alias = data.username;
        //Alias must be 8 characters or less
        if(alias.length() > 8) {
            alias = alias.substring(0, 8);
        }
        u.alias = alias;
        u.languagelocalekey = UserInfo.getLocale();
        u.localesidkey = UserInfo.getLocale();
        u.emailEncodingKey = 'UTF-8';
        u.timeZoneSidKey = 'Europe/Paris';
        u.profileId = p.Id;
        u.contactId = c.Id;
    }
    
    Database.DMLOptions dlo = new Database.DMLOptions();
    dlo.EmailHeader.triggerUserEmail= false;
    Database.saveresult sr = Database.insert(u,dlo);
    
    return u;
} 

global void updateUser(Id userId, Id portalId, Auth.UserData data){
    User u = new User(id=userId);
    u.email = data.email;
    u.lastName = data.lastName;
    u.firstName = data.firstName;
    update(u);
}

Can someone help me with this please ?

2
  • 1
    A Connected Apps trailhead would be a good start. JWT Bearer flow as an oAuth grant type and oAuth as a protocol is for authorization, not authentication. That is, the problem solved by oAuth is granting an external app/service access to Salesforce APIs/data. While it can be (ab)used to implement SSO, it's not intended for that use case. Use SAML or OpenId Connect for SSO.
    – identigral
    Commented Oct 25, 2020 at 20:06
  • Ok thanks for your reply. That's what I thought. I really hope that OpenId Connect is compatible with Magento 1.9...If not, is there another way to configure SSO ? I heard about frontdoor.jsp but can't find a lot of information online.
    – chloe
    Commented Oct 26, 2020 at 8:39

1 Answer 1

2

JWT authentication in Salesforce can be achieved by creating a Connected App in Salesforce. Note that with this approach you will need to use the username(this is the map) of the community user and the private key and certificate

I did a Trailhead Live Session that you might find helpful (Although it does not have details around communities).

Some of the caveats you will need to note

  • If you need to provisions the users on the fly then you will need to use Open ID Connect or SAML. If your IDP is an OpenID provider then you can easily configure this in Salesforce using the Auth Provider and you can use the Registration Handler class.
  • The JWT token bearer flow is designed for the Authorization(OAuth 2.0 protocol) and not for authentication. It is recommended that you use SAML along with the OAuth.

Update

Note you can also use frontdoor jsp to log in your community users, one key thing is this session is not api enabled so anything that involves api you won’t be able to perform. However you can use to land them on to the community url!

Again this will not help you register your users! You will need an openID or SAML for that!

https://help.salesforce.com/articleView?id=000332032&language=en_US&mode=1&sfdcIFrameOrigin=null&type=1

7
  • Ok thanks for your reply. That's what I thought. I really hope that OpenId Connect is compatible with Magento 1.9...If not, is there another way to configure SSO ? I heard about frontdoor.jsp ?
    – chloe
    Commented Oct 26, 2020 at 8:38
  • Responded with update on frontdoor jsp Commented Oct 26, 2020 at 14:32
  • Thanks again ! If I understand correctly, if I use frontdoor.jsp without openID or SAML, the user that comes from Magento will land on the standard community url but not the page linked to their account ? I mean that there will be no way to map the Community User to the Magento User right ?
    – chloe
    Commented Oct 26, 2020 at 15:42
  • Correct! You have to maintain the map yourself! Commented Oct 26, 2020 at 15:48
  • 1
    Yes, I just did a Handler for that. For each Person Account with at least an order, I have to create a community user. There is a cron job that is already set up to create a person account (every 5min) when a Magento account is created.
    – chloe
    Commented Oct 26, 2020 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .