We are trying to implement custom auth provider to facilitate users from a different domain can log into Salesforce with domain credentials.


1.User will login to web application with domain credentials
2.To access Community user will click on Community url (Community Single Sign-On Initialization URL)
3.SSO link will redirect to domain authorization endpoint with code and state
4.Authorization link will redirect to Community Callback URL with access token 
5.Custom Authprovider callback handler will use token to get user details
6.Reghandler will query users based on received response

I tried to implement custom auth with java web application.I referenced Salesforce documentation for Create a Custom External Authentication Provider

AuthProvider code:

   global PageReference initiate(Map<string,string> authProviderConfiguration, String stateToPropagate) {
   authUrl = authProviderConfiguration.get('Auth_URL__c');
   System.debug('authUrl'+ authUrl);
   key = authProviderConfiguration.get('key__c');
   System.debug('key'+ key);
   redirectUrl = authProviderConfiguration.get('redirectUrl__c');
   System.debug('redirectUrl'+ redirectUrl);
   String url = authUrl + '?client_id='+ key +'&response_type=code&redirect_uri='+ redirectUrl + '&state=' + stateToPropagate ;
   System.debug('Routing to My Site with details'+ url);
   return new PageReference(url);

Handler code:

global Auth.AuthProviderTokenResponse handleCallback(Map<string,string> authProviderConfiguration, Auth.AuthProviderCallbackState state ) {
   System.debug('Call back handler' + state);
     System.debug('Call back handler' + state.queryParameters);
   key = authProviderConfiguration.get('key__c');
   secret = authProviderConfiguration.get('secret__c');
   accessTokenUrl = authProviderConfiguration.get('Access_Token_Url__c');
   redirectURL = authProviderConfiguration.get('redirectUrl__c');

   Map<String,String> queryParams = state.queryParameters;
   String code = queryParams.get('code');
   String sfdcState = queryParams.get('state');
   String myUser_Id = queryParams.get('myUser_Id');

   Http http = new Http();
   HttpRequest req = new HttpRequest();
   String url = accessTokenUrl;
     String body ='client_id=' + EncodingUtil.urlEncode(key,'UTF-8') + '&client_secret=' + EncodingUtil.urlEncode(secret,'UTF-8') + '&redirect_uri=' + EncodingUtil.urlEncode(redirectURL,'UTF-8') + '&code=' + EncodingUtil.urlEncode(code,'UTF-8') + '&myUser_Id='+EncodingUtil.urlEncode(myUser_Id,'UTF-8');

   HTTPResponse res = new HTTPResponse();
    res = http.send(req);
   }catch(System.CalloutException e){System.debug('Callout error:'+ e);
   String responseBody = res.getBody();
   Map<String, Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(responseBody);
   String token = (String)responseMap.get('access_token');
   System.debug('Call back handler'+ token +'<<Token'+'state>>'+sfdcState);
   return new Auth.AuthProviderTokenResponse('Incapsula', token, null, sfdcState);

I am using auto generated Reghandler:

global class AutocreatedRegHandler1432826053915 implements Auth.RegistrationHandler{
global User createUser(Id portalId, Auth.UserData data){
    User u = [SELECT ID FROM User Where email = : data.username];
    return u;
global void updateUser(Id userId, Id portalId, Auth.UserData data){



Tested from Community login page using Custom auth: Result:I can login to web application and after successful login re-directing to Community callback url. I am not sure why Community callback is redirecting to "https://test.salesforce.com/_nc_external/identity/sso/ui/AuthorizationError?ErrorCode=B

Tested from web application: After successful login I am getting 503 Service Unavailable on Community callback url

Auth Provider: enter image description here

Community links (My domain is deployed for all users): enter image description here

  • 1
    Hi, the level of information provided here is not sufficient to be able to provide any resolution. Please take a moment to visit How to Ask and then edit your question. You are missing here key information as how is your SSO setup, what is the IdP, how is the callback URL configured, etc. – Jayant Das Oct 25 '18 at 1:16
  • Hi Jayant I have added all information – Abhi Oct 25 '18 at 3:18
  • Have you updated the callback URL on your external authentication provider that you are using? – Jayant Das Oct 25 '18 at 18:27
  • yes I have updated the callback on Identity provider. – Abhi Oct 26 '18 at 3:46
  • Working Flow:-1. Customer SSO link is redirecting to identity provider login link 2.After successful login identity provider is redirecting to Community Callback url with State and Code.3.On community callback url I am getting authentication error and callback handler is not executing – Abhi Oct 26 '18 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.