Answers.Body: <p><a href="https://github.com/civicrm/civicrm-core/blob/master/CRM/Contact/BAO/Contact/Utils.php#L143" rel="nofollow"><code>CRM_Contact_BAO_Contact_Utils::generateChecksum($contactId)</code></a> is the function used to generate checksums.</p>
  
  <p>Ideally your extension would call this via a corresponding API method, but I don't think one exists yet?</p>
  
  <p>There's corresponding code (see validChecksum etc below that) which is used in some CiviCRM pages to identify a request with a checksum parameter.</p>
  
  <p>Simple code to identify a checksum and persist against the <em>contact</em> is the following -</p>
  
  <pre><code>if ($cs = CRM_Utils_Request::retrieve('cs', 'String' , $this, false)) {
    if ($id = CRM_Utils_Request::retrieve('cid', 'Positive', $this, false, $userID)) {
      if (CRM_Contact_BAO_Contact_Utils::validChecksum($id, $cs)) {
        $session-&gt;set('userID', $id) ;
      }
    }
  }
  </code></pre>
  
  <p>If they are landing on your callback already, then you can just dispense with using the checksum parameter and set the session directly before redirecting them to the page they authenticated to use.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T21:30:27.140Z
Answers.LastActivityDate: 2015-06-24T04:05:20.553Z
Answers.LastEditDate: 2015-06-24T04:05:20.553Z
Answers.UserDisplayName: Chris Burgess
Answers.Body: <p><code>CRM_Contact_BAO_Contact_Utils::validChecksum</code> is the method used to validate a checksum in the url. You can see how it's used in a 3rd party module in this example from webform_civicrm:</p>
  
  <pre><code>if (!empty($_GET["cs$c"]) &amp;&amp; CRM_Contact_BAO_Contact_Utils::validChecksum($cid, $_GET["cs$c"])) {
    $filters['check_permissions'] = FALSE;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-24T01:12:13.577Z
Answers.LastActivityDate: 2015-06-24T01:12:13.577Z
Answers.UserDisplayName: Coleman
Body: <p><em>Reposted/rephrased from <a href="http://forum.civicrm.org/index.php/topic,36059.30.html_" rel="nofollow">http://forum.civicrm.org/index.php/topic,36059.30.html</em></a>.</p>
  
  <blockquote>
    <p>I am currently working on an extension to log in a user in CiviCRM using Facebook.</p>
    
    <p>The user is sent to facebook for auth request/permissions, and if successful redirected to a callback page where it receives a code which is valid for one login. Now the server gets all the details to this page and create a contact and save it. </p>
    
    <p>I am thinking of now sending back the user to the earlier page with the contact id and the checksum. Something which is described in the following link: <a href="https://aghstrategies.com/content/how-create-one-click-personalized-links-civicrm-emails" rel="nofollow">https://aghstrategies.com/content/how-create-one-click-personalized-links-civicrm-emails</a></p>
    
    <p>Now I need to keep track of if someone is logged in, is there anything I can store this constraint into or I need something, or should I directly use SESSION and store it there?</p>
  </blockquote>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-23T21:36:17.283Z
Comments.Text: Related: http://civicrm.stackexchange.com/questions/2352/personalized-drupal-login-link?s=6|0.1070
Comments.UserDisplayName: Chris Burgess
Tags: <authentication><session><checksum>
Title: How can I programmatically authenticate a contact (via checksum or other method)?
ThreadText: # How can I programmatically authenticate a contact (via checksum or other method)?
  
  (Asked by: username_0 on 2015-06-23)
  
  *Reposted/rephrased from [http://forum.civicrm.org/index.php/topic,36059.30.html](http://forum.civicrm.org/index.php/topic,36059.30.html_)*.
  
  > I am currently working on an extension to log in a user in CiviCRM using Facebook.
  >
  > The user is sent to facebook for auth request/permissions, and if successful redirected to a callback page where it receives a code which is valid for one login. Now the server gets all the details to this page and create a contact and save it.
  >
  > I am thinking of now sending back the user to the earlier page with the contact id and the checksum. Something which is described in the following link: <https://aghstrategies.com/content/how-create-one-click-personalized-links-civicrm-emails>
  >
  > Now I need to keep track of if someone is logged in, is there anything I can store this constraint into or I need something, or should I directly use SESSION and store it there?
  
  **Tags:** authentication, session, checksum
  
  > **Comments:**
  > **username_0**: Related: http://civicrm.stackexchange.com/questions/2352/personalized-drupal-login-link?s=6|0.1070  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-06-23. Score: 1
  
  CRM_Contact_BAO_Contact_Utils::generateChecksum($contactId) is the function used to generate checksums.
  Ideally your extension would call this via a corresponding API method, but I don't think one exists yet?
  There's corresponding code (see validChecksum etc below that) which is used in some CiviCRM pages to identify a request with a checksum parameter.
  Simple code to identify a checksum and persist against the contact is the following -
  if ($cs = CRM_Utils_Request::retrieve('cs', 'String' , $this, false)) {
    if ($id = CRM_Utils_Request::retrieve('cid', 'Positive', $this, false, $userID)) {
      if (CRM_Contact_BAO_Contact_Utils::validChecksum($id, $cs)) {
        $session->set('userID', $id) ;
      }
    }
  }
  
  If they are landing on your callback already, then you can just dispense with using the checksum parameter and set the session directly before redirecting them to the page they authenticated to use.
  
  
  ---
  
  ### Answer by username_1 on 2015-06-24. Score: 0
  
  CRM_Contact_BAO_Contact_Utils::validChecksum is the method used to validate a checksum in the url. You can see how it's used in a 3rd party module in this example from webform_civicrm:
  if (!empty($_GET["cs$c"]) && CRM_Contact_BAO_Contact_Utils::validChecksum($cid, $_GET["cs$c"])) {
    $filters['check_permissions'] = FALSE;
  }
  
  
  ---
  
  