OAuthOAuth2ServiceSalesforce

The Gravity Forms Salesforce Add-On OAuth OAuth2 Service Salesforce class.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/src/OAuth/OAuth2/Service/Salesforce.php  
  1. class Salesforce extends AbstractService 
  2. /** 
  3. * Scopes 
  4. * @var string 
  5. */ 
  6. const SCOPE_API = 'api',  
  7. SCOPE_REFRESH_TOKEN = 'refresh_token'; 
  8.  
  9. /** 
  10. * Are we connecting to Salesforce? 
  11. * @var boolean 
  12. */ 
  13. var $sandbox = false; 
  14.  
  15. /** 
  16. * {@inheritdoc} 
  17. */ 
  18. public function getAuthorizationEndpoint() 
  19. if ( $this->sandbox ) { 
  20. return new Uri('https://test.salesforce.com/services/oauth2/authorize'); 
  21. } else { 
  22. return new Uri('https://login.salesforce.com/services/oauth2/authorize'); 
  23.  
  24. /** 
  25. * Define whether using Salesforce Sandbox or not. 
  26. * @param boolean $is_sandbox Should the connection use Salesforce? 
  27. */ 
  28. public function setSandbox( $is_sandbox = false ) { 
  29. $this->sandbox = !empty( $is_sandbox ); 
  30.  
  31. /** 
  32. * {@inheritdoc} 
  33. */ 
  34. public function getAccessTokenEndpoint() 
  35. if ( $this->sandbox ) { 
  36. return new Uri('https://test.salesforce.com/services/oauth2/token'); 
  37. } else { 
  38. return new Uri('https://na1.salesforce.com/services/oauth2/token'); 
  39.  
  40. /** 
  41. * {@inheritdoc} 
  42. */ 
  43. protected function parseRequestTokenResponse($responseBody) 
  44. parse_str($responseBody, $data); 
  45.  
  46. if (null === $data || !is_array($data)) { 
  47. throw new TokenResponseException('Unable to parse response.'); 
  48. } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') { 
  49. throw new TokenResponseException('Error in retrieving token.'); 
  50.  
  51. return $this->parseAccessTokenResponse($responseBody); 
  52.  
  53. /** 
  54. * {@inheritdoc} 
  55. */ 
  56. protected function parseAccessTokenResponse($responseBody) 
  57. $data = json_decode($responseBody, true); 
  58.  
  59. if (null === $data || !is_array($data)) { 
  60. throw new TokenResponseException('Unable to parse response.'); 
  61. } elseif (isset($data['error'])) { 
  62. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); 
  63.  
  64. $token = new StdOAuth2Token(); 
  65. $token->setAccessToken($data['access_token']); 
  66.  
  67. // Salesforce access tokens depend on the session timeout settings. 
  68. // The session timeout for an access token can be configured in Salesforce from Setup by clicking Security Controls | Session Settings. 
  69. $token->setEndOfLife(StdOAuth2Token::EOL_UNKNOWN); 
  70.  
  71. unset($data['access_token']); 
  72.  
  73. if (isset($data['refresh_token'])) { 
  74. $token->setRefreshToken($data['refresh_token']); 
  75. // Save Refresh Token persistently until it is cleared manually 
  76. update_option( 'gf_salesforce_refreshtoken', $data['refresh_token'] ); 
  77. unset($data['refresh_token']); 
  78. } else { 
  79. $refresh_token = get_option( 'gf_salesforce_refreshtoken' ); 
  80. if( !empty( $refresh_token ) ) { 
  81. $token->setRefreshToken( $refresh_token ); 
  82.  
  83. $token->setExtraParams($data); 
  84.  
  85. return $token; 
  86.  
  87. /** 
  88. * {@inheritdoc} 
  89. */ 
  90. protected function getExtraOAuthHeaders() 
  91. return array('Accept' => 'application/json');