Posted by Abhishek on December 27, 2019
In this blog, we will explain the steps of enabling social sign on into salesforce enterprise org using your corporate Gmail account.
John is an employee of an enterprise called “XYZ.com”. The company uses Salesforce as their primary CRM. Just like John, there are many other employees who face issues with remembering username and password for logging into their Salesforce CRM. As per company policy, no employees are supposed to use any Password storing apps/even write down this sensitive information on their notebooks/whiteboards.
The company proposes to enable Social Sign-On capability with which every employee will be able to use their corporate Gmail login credentials to login to Salesforce CRM. With this technique, a new employee provisioned in Gmail is automatically allowed into Salesforce CRM. Any employee who leaves the organization is also denied the Salesforce CRM login.
For configuring this feature, we will be using the Open ID Connect protocol in conjunction with Authentication Providers in Salesforce. With this setup, Salesforce behaves like an Open ID Client.
global boolean canCreateUser(Auth.UserData data)
{
if(data.email!=null)
{
return true;
}
else
{
return false;
}
}
global User createUser(Id portalId, Auth.UserData data)
{
if(canCreateUser(data))
{
List users = [select Id from User where Google_ID__c=:data.identifier];
if(users.size()==1)
{
debug('#1##'+users[0]);
return users[0];
}
else
{
User u = new User();
Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
username = data.email.substring(0,data.email.indexOf('@'))+ '@xyz.com';
email = data.email;
lastName = data.lastName;
firstName = data.firstName;
String alias = data.firstName.substring(0,1)+data.lastName.substring(0,4);
if(alias.length() > 8)
{
alias = alias.substring(0, 8);
}
alias = alias;
languagelocalekey = 'en_US';
localesidkey = 'en_US';
emailEncodingKey = 'UTF-8';
timeZoneSidKey = 'America/Los_Angeles';
profileId = p.Id;
Google_ID__c = data.identifier;
insert u;
return u;
}
}
else
{
return null;
}
}
global void updateUser(Id userId, Id portalId, Auth.UserData data)
{
User u = new User(Id=userId);
//u.username = data.email.substring(0,data.email.indexOf('@'))+ '@salesforce.com';
//u.Google_ID__c = data.identifier;
update u;
}
@isTest
private class GoogleEnterpriseSignOnTest
{
static testMethod void testCreateAndUpdateUser()
{
GoogleEnterpriseSignOn handler = new GoogleEnterpriseSignOn();
Auth.UserData sampleData = new Auth.UserData('testId', 'testFirst', 'testLast',
'testFirst testLast', 'testuser@example.org',
null, 'testuserlong', 'en_US', 'google',
null, new Map<String, String>{'language' => 'en_US'});
User u = handler.createuser(null, sampleData);
System.assertEquals('testuser@xyz.com', u.userName);
System.assertEquals('testuser@example.org', u.email);
System.assertEquals('testLast', u.lastName);
System.assertEquals('testFirst', u.firstName);
System.assertEquals('ttest', u.alias);
System.assertEquals('testId', u.Google_ID__c);
String uid = u.id;
sampleData = new Auth.UserData('testNewId', 'testNewFirst', 'testNewLast',
'testNewFirst testNewLast', 'testnewuser@example.org', null, 'testnewuserlong', 'en_US', 'facebook',
null, new Map<String, String>{});
handler.updateUser(uid, null, sampleData);
//User updatedUser = [SELECT userName, email, firstName, lastName, alias FROM user WHERE id=:uid];
//System.assertEquals('testnewuserlong@salesforce.com', updatedUser.userName);
//System.assertEquals('testnewuser@example.org', updatedUser.email);
//System.assertEquals('testNewLast', updatedUser.lastName);
//System.assertEquals('testNewFirst', updatedUser.firstName);
//System.assertEquals('testnewu', updatedUser.alias);
}
}
Writing a test class is very simple as you just have to set the necessary properties of the Auth.UserData and feed that into the createUser & updateUser method of your main AuthProvider class. I have commented a few lines of the updateUser asserts because I do not have a logic written inside the updateUser method of GoogleEnterpriseSignOn AuthProvider class. With this simple test class, I was able to achieve 87% code coverage. This test class template would help you to get started. You can refine & tune the test class as per your custom logic. Hope this helps. Click here to learn more about Registration Handlers in Salesforce. I hope this blog was informative. Try it out folks and let me know if you have any questions/concerns.