0

I have a requirement where I want to logout of salesforce when certain statement inside an @invocable method is executed. For this purpose, I am trying to use PageReference to redirect to "/secur/logout.jsp", but unable to logout. Please suggest !

Added more information:

  • Salesforce is acting as both Identity provider as well as Service Provider and Login flow is leveraged during reverse direction IDP to Salesforce.
  • The login flow has apex action which calls @Invocable method. This method checks if valid cookies are present. If not, then logout the user.

1st Approach tried was to kill the session Id, but I get error "INVALID_SESSION_ID: This session is not valid for use with the API"

2nd Approach tried was to redirect to "/secur/logout.jsp". This again doesn't seems to work.

public class myInvocableClass{

@InvocableMethod
public static void myInvocableMethod(){
    if(Conditions met){
        //Execute Statements
    }

    else{

    //logout of salesforce
    MyPgRefClass mp = new MyPgRefClass();
    mp.logout();

    }
}
}


public class MyPgRefClass{

    public PageReference logout(){

        System.debug('Inside logout()'); 

        PageReference pageRef = new PageReference('/secur/logout.jsp');
        pageRef.setRedirect(true);

        System.debug('@@@@@@Running Page Ref now');
        return pageRef;

    }
}
  • What you're trying to do is not possible. As such, this is likely an X-Y problem. Please describe what you're trying to do (the "X" problem), and we can try to help you with the solution. – sfdcfox Mar 5 '19 at 15:48
  • We are using IDP login flow. The flow has an apex action(@Invocable method). @Invocable method checks for some valid cookies. If those are not present then the session should be logged out. In order to fulfill this, thought of killing the session Id, but I get "INVALID_SESSION_ID: This session is not valid for use with the API" error. Hence thought of redirecting to "/secur/logout.jsp", but that also goes in vain. – Lord Voldemort Mar 5 '19 at 17:46
  • Might worth redirecting to revokeEndpoint, it will destroy the current access token – Pranay Jaiswal Mar 5 '19 at 18:25
1

Just get the SessionId of the User and redirect them to the revokeURL, it will log out from the browser.

      public PageReference logout(){
        HttpRequest hreq = new HttpRequest();

        String token = UserInfo.getSessionId();
        hreq.setEndpoint('https://login.salesforce.com/services/oauth2/revoke?token=' + token);
        hreq.setMethod('GET');

        new Http().send(hreq);

}
  • Session Id approach doesn't seems to work. Debug logs shows that SESSION_ID_REMOVED. Hence the user is still not logged out. Another approach I tried, was to make SOAP Api logout call (<urn:logout/>) to end point /services/Soap/c/45.0/OrgIdm but I get error response "INVALID_SESSION_ID: This session is not valid for use with the API" – Lord Voldemort Mar 5 '19 at 18:55
  • Debug logs wont show session id for security reason. I tried same with my session ID in browserr. And it destroyed my session – Pranay Jaiswal Mar 5 '19 at 18:56
  • Are you also calling PageReference from Invocable method. If No, then please start with Invocable method first. Also, as pointed earlier by @Sfdcfox Invocable --to--> PageReference won't work. But at the same time, our use case is such that, it starts with Invocable method & eventually we want to log out user. Thanks for responding. – Lord Voldemort Mar 5 '19 at 19:03
  • @LordVoldemort converted pagereference to callout – Pranay Jaiswal Mar 5 '19 at 19:10
0

Thanks for everyone's inputs. For the sake of viewers who might come across this post in future, we can leverage "LoginFlow_ForceLogout" attribute in the flow. When this variable is set to true, the user is immediately logged out. Documentation available here SecurityLoginFlow

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.