1
0

BxOAuthModule.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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(array(
  33. 'dsn' => $this->_buildDSN(),
  34. 'username' => BX_DATABASE_USER,
  35. 'password' => BX_DATABASE_PASS,
  36. 'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"),
  37. ), $aConfig);
  38. $this->_oServer = new OAuth2\Server($this->_oStorage, array (
  39. 'require_exact_redirect_uri' => false,
  40. ));
  41. // Add the "Client Credentials" grant type (it is the simplest of the grant types)
  42. $this->_oServer->addGrantType(new OAuth2\GrantType\ClientCredentials($this->_oStorage));
  43. // Add the "Authorization Code" grant type (this is where the oauth magic happens)
  44. $this->_oServer->addGrantType(new OAuth2\GrantType\AuthorizationCode($this->_oStorage));
  45. }
  46. protected function _buildDSN ()
  47. {
  48. $sDSN = 'mysql:';
  49. if (!empty(BX_DATABASE_HOST))
  50. $sDSN .= 'host=' . BX_DATABASE_HOST . ';';
  51. if (!empty(BX_DATABASE_PORT))
  52. $sDSN .= 'port=' . BX_DATABASE_PORT . ';';
  53. if (!empty(BX_DATABASE_SOCK))
  54. $sDSN .= 'unix_socket=' . BX_DATABASE_SOCK . ';';
  55. $sDSN .= 'dbname=' . BX_DATABASE_NAME . ';charset=UTF8';
  56. return $sDSN;
  57. }
  58. function actionToken ()
  59. {
  60. // Handle a request for an OAuth2.0 Access Token and send the response to the client
  61. $this->_oServer->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
  62. }
  63. function actionApi ($sAction)
  64. {
  65. // Handle a request to a resource and authenticate the access token
  66. if (!$this->_oServer->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
  67. $this->_oServer->getResponse()->send();
  68. return;
  69. }
  70. $aToken = $this->_oServer->getAccessTokenData(OAuth2\Request::createFromGlobals());
  71. if (!$this->_oAPI) {
  72. bx_import('API', $this->_aModule);
  73. $this->_oAPI = new BxOAuthAPI($this);
  74. }
  75. if (!$sAction || !method_exists($this->_oAPI, $sAction) || 0 == strcasecmp('errorOutput', $sAction) || 0 == strcasecmp('output', $sAction)) {
  76. $this->_oAPI->errorOutput(404, 'not_found', 'No such API endpoint available');
  77. return;
  78. }
  79. $sScope = $this->_oAPI->aAction2Scope[$sAction];
  80. if (false === strpos($sScope, $aToken['scope'])) {
  81. $this->_oAPI->errorOutput(403, 'insufficient_scope', 'The request requires higher privileges than provided by the access token');
  82. return;
  83. }
  84. $this->_oAPI->$sAction($aToken);
  85. }
  86. function actionAuth ()
  87. {
  88. $oRequest = OAuth2\Request::createFromGlobals();
  89. $oResponse = new OAuth2\Response();
  90. // validate the authorize request
  91. if (!$this->_oServer->validateAuthorizeRequest($oRequest, $oResponse)) {
  92. $o = json_decode($oResponse->getResponseBody());
  93. $this->_oTemplate->pageError($o->error_description);
  94. }
  95. if (!isLogged()) {
  96. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  97. $sForceRelocate = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'auth/?client_id=' . bx_get('client_id') . '&response_type=' . bx_get('response_type') . '&state=' . bx_get('state') . '&redirect_uri=' . bx_get('redirect_uri');
  98. bx_login_form(false, false, $sForceRelocate);
  99. return;
  100. }
  101. if (empty($_POST)) {
  102. $oPage = BxDolPage::getObjectInstanceByURI('oauth-authorization');
  103. $this->_oTemplate->getPage(false, $oPage->getCode());
  104. return;
  105. }
  106. $this->_oServer->handleAuthorizeRequest($oRequest, $oResponse, (bool)bx_get('profile_id'), bx_get('profile_id'));
  107. $oResponse->send();
  108. }
  109. function serviceAuthorization ()
  110. {
  111. $sTitle = $this->_oDb->getClientTitle(bx_get('client_id'));
  112. $this->_oTemplate->addCss('main.css');
  113. return $this->_oTemplate->parseHtmlByName('page_auth.html', array(
  114. 'text' => _t('_bx_oauth_authorize_app', htmlspecialchars_adv($sTitle)),
  115. 'url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'auth',
  116. 'client_id' => bx_get('client_id'),
  117. 'response_type' => bx_get('response_type'),
  118. 'redirect_uri' => bx_get('redirect_uri'),
  119. 'state' => bx_get('state'),
  120. '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'],
  121. ));
  122. }
  123. function studioSettings ()
  124. {
  125. if (!isAdmin()) {
  126. $this->_oTemplate->displayAccessDenied ();
  127. return;
  128. }
  129. $oGrid = BxDolGrid::getObjectInstance('bx_oauth', BxDolStudioTemplate::getInstance());
  130. if ($oGrid)
  131. return $oGrid->getCode();
  132. $this->_oTemplate->displayPageNotFound();
  133. }
  134. }
  135. /** @} */