BxGitHubApp.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defdroup GitHub GitHub
  7. * @indroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. *
  13. * Register App
  14. * https://docs.github.com/en/apps/creating-github-apps/registering-a-github-app/registering-a-github-app
  15. *
  16. * Create "Login with GitHub" web flow.
  17. * https://docs.github.com/en/apps/creating-github-apps/writing-code-for-a-github-app/building-a-login-with-github-button-with-a-github-app
  18. *
  19. */
  20. class BxGitHubApp extends BxDol
  21. {
  22. protected $_oModule;
  23. protected $_sClientId;
  24. protected $_sClientSecret;
  25. protected $_sUrlAuthorize;
  26. protected $_sUrlAccessToken;
  27. protected $_bInit;
  28. public function __construct(&$oModule)
  29. {
  30. $this->_oModule = $oModule;
  31. $this->_sUrlAuthorize = 'https://github.com/login/oauth/authorize';
  32. $this->_sUrlAccessToken = 'https://github.com/login/oauth/access_token';
  33. $this->_bInit = false;
  34. }
  35. public function init($iAppId)
  36. {
  37. $aApp = $this->_oModule->_oDb->getApps(['sample' => 'id', 'id' => $iAppId]);
  38. if(empty($aApp) || !is_array($aApp))
  39. return false;
  40. $this->_sClientId = $aApp['client_id'] ?? false;
  41. $this->_sClientSecret = $aApp['client_secret'] ?? false;
  42. $this->_bInit = $this->_sClientId && $this->_sClientSecret;
  43. return true;
  44. }
  45. public function getUrlAuthorize()
  46. {
  47. return bx_append_url_params($this->_sUrlAuthorize, [
  48. 'client_id' => $this->_sClientId
  49. ]);
  50. }
  51. public function getAccessToken($sCode)
  52. {
  53. $mixedResponse = $this->_apiCallAccessToken($this->_sUrlAccessToken, [
  54. 'code' => $sCode
  55. ]);
  56. if($mixedResponse === false || empty($mixedResponse['access_token'])) {
  57. $this->_oModule->log(['Get Access Token:', $mixedResponse], BX_LOG_ERR);
  58. return false;
  59. }
  60. $aResult = [
  61. 'access_token' => $mixedResponse['access_token']
  62. ];
  63. if(isset($mixedResponse['expires_in'], $mixedResponse['refresh_token'], $mixedResponse['refresh_token_expires_in']))
  64. $aResult = array_merge($aResult, [
  65. 'at_expires_in' => $mixedResponse['expires_in'],
  66. 'refresh_token' => $mixedResponse['refresh_token'],
  67. 'rt_expires_in' => $mixedResponse['refresh_token_expires_in']
  68. ]);
  69. return $aResult;
  70. }
  71. public function refreshAccessToken($sRefreshToken)
  72. {
  73. $mixedResponse = $this->_apiCallAccessToken($this->_sUrlAccessToken, [
  74. 'grant_type' => 'refresh_token',
  75. 'refresh_token' => $sRefreshToken
  76. ]);
  77. if($mixedResponse === false || !isset($mixedResponse['access_token'], $mixedResponse['refresh_token'])) {
  78. $this->_oModule->log(['Refresh Access Token:', $mixedResponse], BX_LOG_ERR);
  79. return false;
  80. }
  81. return [
  82. 'access_token' => $mixedResponse['access_token'],
  83. 'at_expires_in' => $mixedResponse['expires_in'],
  84. 'refresh_token' => $mixedResponse['refresh_token'],
  85. 'rt_expires_in' => $mixedResponse['refresh_token_expires_in']
  86. ];
  87. }
  88. protected function _apiCallAccessToken($sEndpoint, $aParams = [])
  89. {
  90. $sResponse = $this->_apiCall($sEndpoint, array_merge([
  91. 'client_id' => $this->_sClientId,
  92. 'client_secret' => $this->_sClientSecret,
  93. ], $aParams));
  94. return !empty($sResponse) ? json_decode($sResponse, true) : false;
  95. }
  96. protected function _apiCall($sEndpoint, $aParams = [], $aHeaders = [], $sMethod = 'post', $aBasicAuth = [], &$sHttpCode = null)
  97. {
  98. $aHeaders = array_merge([
  99. 'Accept: application/json',
  100. ], $aHeaders);
  101. return bx_file_get_contents($sEndpoint, $aParams, $sMethod, $aHeaders, $sHttpCode, $aBasicAuth);
  102. }
  103. }