BxOAuthModule.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup OAuth2 OAuth2 server
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth2/Autoloader.php');
  12. OAuth2\Autoloader::register();
  13. class BxOAuthModule extends BxDolModule
  14. {
  15. protected $_oStorage;
  16. protected $_oServer;
  17. protected $_oAPI;
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. $aConfig = array (
  22. 'client_table' => 'bx_oauth_clients',
  23. 'access_token_table' => 'bx_oauth_access_tokens',
  24. 'refresh_token_table' => 'bx_oauth_refresh_tokens',
  25. 'code_table' => 'bx_oauth_authorization_codes',
  26. 'user_table' => 'Profiles',
  27. 'jwt_table' => '',
  28. 'jti_table' => '',
  29. 'scope_table' => 'bx_oauth_scopes',
  30. 'public_key_table' => '',
  31. );
  32. $this->_oStorage = new OAuth2\Storage\Pdo(BxDolDb::getLink(), $aConfig);
  33. $this->_oServer = new OAuth2\Server($this->_oStorage, array(
  34. 'require_exact_redirect_uri' => false,
  35. 'refresh_token_lifetime' => 7776000, // set lifetime to 90 days
  36. ));
  37. // Add the "Client Credentials" grant type (it is the simplest of the grant types)
  38. $this->_oServer->addGrantType(new OAuth2\GrantType\ClientCredentials($this->_oStorage));
  39. // Add the "Authorization Code" grant type (this is where the oauth magic happens)
  40. $this->_oServer->addGrantType(new OAuth2\GrantType\AuthorizationCode($this->_oStorage));
  41. }
  42. function actionToken ()
  43. {
  44. // Handle a request for an OAuth2.0 Access Token and send the response to the client
  45. $this->_oServer->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
  46. }
  47. function actionApi ($sAction)
  48. {
  49. // Handle a request to a resource and authenticate the access token
  50. if (!$this->_oServer->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
  51. $this->_oServer->getResponse()->send();
  52. return;
  53. }
  54. $aToken = $this->_oServer->getAccessTokenData(OAuth2\Request::createFromGlobals());
  55. if (!$this->_oAPI) {
  56. bx_import('API', $this->_aModule);
  57. $this->_oAPI = new BxOAuthAPI($this);
  58. }
  59. if (!$sAction || !method_exists($this->_oAPI, $sAction) || 0 == strcasecmp('errorOutput', $sAction) || 0 == strcasecmp('output', $sAction)) {
  60. $this->_oAPI->errorOutput(404, 'not_found', 'No such API endpoint available');
  61. return;
  62. }
  63. $sScope = $this->_oAPI->aAction2Scope[$sAction];
  64. if (false === strpos($sScope, $aToken['scope'])) {
  65. $this->_oAPI->errorOutput(403, 'insufficient_scope', 'The request requires higher privileges than provided by the access token');
  66. return;
  67. }
  68. $this->_oAPI->$sAction($aToken);
  69. }
  70. function actionAuth ()
  71. {
  72. $oRequest = OAuth2\Request::createFromGlobals();
  73. $oResponse = new OAuth2\Response();
  74. // validate the authorize request
  75. if (!$this->_oServer->validateAuthorizeRequest($oRequest, $oResponse)) {
  76. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  77. $o = json_decode($oResponse->getResponseBody());
  78. $this->_oTemplate->getPage(false, MsgBox($o->error_description));
  79. }
  80. if (!isLogged()) {
  81. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  82. $sForceRelocate = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'auth/?client_id=' . bx_get('client_id') . '&response_type=' . bx_get('response_type') . '&scope=' . bx_get('scope') . '&state=' . bx_get('state') . '&redirect_uri=' . bx_get('redirect_uri');
  83. bx_login_form(false, false, $sForceRelocate);
  84. return;
  85. }
  86. $aProfiles = BxDolAccount::getInstance()->getProfiles();
  87. if (!($iProfileId = $this->_oDb->getSavedProfile($aProfiles)) && empty($_POST)) {
  88. $oPage = BxDolPage::getObjectInstanceByURI('oauth-authorization');
  89. $this->_oTemplate->getPage(false, $oPage->getCode());
  90. return;
  91. }
  92. if (!$iProfileId)
  93. $iProfileId = bx_get('profile_id');
  94. $this->_oServer->handleAuthorizeRequest($oRequest, $oResponse, (bool)$iProfileId, $iProfileId);
  95. $oResponse->send();
  96. }
  97. function serviceAuthorization ()
  98. {
  99. $sTitle = $this->_oDb->getClientTitle(bx_get('client_id'));
  100. $this->_oTemplate->addCss('main.css');
  101. return $this->_oTemplate->parseHtmlByName('page_auth.html', array(
  102. 'text' => _t('_bx_oauth_authorize_app', htmlspecialchars_adv($sTitle)),
  103. 'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'auth',
  104. 'client_id' => bx_get('client_id'),
  105. 'response_type' => bx_get('response_type'),
  106. 'redirect_uri' => bx_get('redirect_uri'),
  107. 'scope' => bx_get('scope'),
  108. 'state' => bx_get('state'),
  109. 'profiles' => BxDolService::call('system', 'account_profile_switcher', array(getLoggedId(), false, "javascript: $('#bx-auth-profile-id').val('{profile_id}'); $('#bx-auth-form form').submit(); void(0);"), 'TemplServiceProfiles')['content'],
  110. ));
  111. }
  112. function serviceGetClientsBy ($aParams = array())
  113. {
  114. return $this->_oDb->getClientsBy($aParams);
  115. }
  116. function serviceAddClient ($aClient)
  117. {
  118. return $this->_oDb->addClient($aClient);
  119. }
  120. function serviceUpdateClientsBy ($aParamsSet, $aParamsWhere)
  121. {
  122. return $this->_oDb->updateClientsBy($aParamsSet, $aParamsWhere);
  123. }
  124. function serviceDeleteClientsBy ($aParams)
  125. {
  126. return $this->_oDb->deleteClientsBy($aParams);
  127. }
  128. function studioSettings ()
  129. {
  130. if (!isAdmin()) {
  131. $this->_oTemplate->displayAccessDenied ();
  132. return;
  133. }
  134. $oGrid = BxDolGrid::getObjectInstance('bx_oauth', BxDolStudioTemplate::getInstance());
  135. if ($oGrid)
  136. return $oGrid->getCode();
  137. return '';
  138. }
  139. }
  140. /** @} */