Cheetah
ChOAuthModule.php
Go to the documentation of this file.
1 <?php
2 
8 ch_import('ChWsbModule');
9 ch_import('ChWsbPaginate');
10 ch_import('ChWsbAlerts');
11 
12 require_once (CH_DIRECTORY_PATH_PLUGINS . 'OAuth2/Autoloader.php');
14 
16 {
17  protected $_oStorage;
18  protected $_oServer;
19  protected $_oAPI;
20 
21  function __construct(&$aModule)
22  {
23  parent::__construct($aModule);
24 
25  $aConfig = array (
26  'client_table' => 'ch_oauth_clients',
27  'access_token_table' => 'ch_oauth_access_tokens',
28  'refresh_token_table' => 'ch_oauth_refresh_tokens',
29  'code_table' => 'ch_oauth_authorization_codes',
30  'user_table' => 'Profiles',
31  'jwt_table' => '',
32  'jti_table' => '',
33  'scope_table' => 'ch_oauth_scopes',
34  'public_key_table' => '',
35  );
36 
37  $this->_oStorage = new OAuth2\Storage\Pdo(ChWsbDb::getInstance()->getLink(), $aConfig);
38 
39  $this->_oServer = new OAuth2\Server($this->_oStorage, array (
40  'require_exact_redirect_uri' => false,
41  ));
42 
43  // Add the "Client Credentials" grant type (it is the simplest of the grant types)
44  $this->_oServer->addGrantType(new OAuth2\GrantType\ClientCredentials($this->_oStorage));
45 
46  // Add the "Authorization Code" grant type (this is where the oauth magic happens)
47  $this->_oServer->addGrantType(new OAuth2\GrantType\AuthorizationCode($this->_oStorage));
48 
49  }
50 
51  function actionToken ()
52  {
53  // Handle a request for an OAuth2.0 Access Token and send the response to the client
54  $this->_oServer->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
55  }
56 
57  function actionApi ($sAction)
58  {
59  // Handle a request to a resource and authenticate the access token
60  if (!$this->_oServer->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
61  $this->_oServer->getResponse()->send();
62  return;
63  }
64 
65  $aToken = $this->_oServer->getAccessTokenData(OAuth2\Request::createFromGlobals());
66 
67  if (!$this->_oAPI) {
68  ch_import('API', $this->_aModule);
69  $this->_oAPI = new ChOAuthAPI($this);
70  }
71 
72  if (!$sAction || !method_exists($this->_oAPI, $sAction) || 0 === strcasecmp('errorOutput', $sAction) || 0 === strcasecmp('output', $sAction)) {
73  $this->_oAPI->errorOutput(404, 'not_found', 'No such API endpoint available');
74  return;
75  }
76 
77  $sScope = $this->_oAPI->aAction2Scope[$sAction];
78  if (false === strpos($sScope, $aToken['scope'])) {
79  $this->_oAPI->errorOutput(403, 'insufficient_scope', 'The request requires higher privileges than provided by the access token');
80  return;
81  }
82 
83  $this->_oAPI->$sAction($aToken);
84 
85  //echo json_encode(array('success' => true, 'message' => 'TODO: process "' . $sAction . '" action for user "' . $aToken['user_id'] . '"'));
86  }
87 
88  function actionAuth ()
89  {
91  $oResponse = new OAuth2\Response();
92 
93  // validate the authorize request
94  if (!$this->_oServer->validateAuthorizeRequest($oRequest, $oResponse)) {
95  $o = json_decode($oResponse->getResponseBody());
96  $this->_oTemplate->pageError($o->error_description);
97  }
98 
99  if (!isLogged()) {
100  $_REQUEST['relocate'] = CH_WSB_URL_ROOT . $this->_oConfig->getBaseUri() . 'auth/?client_id=' . ch_get('client_id') . '&response_type=' . ch_get('response_type') . '&state=' . ch_get('state') . '&redirect_uri=' . ch_get('redirect_uri');
101  login_form('', 0, false, 'disable_external_auth no_join_text');
102  return;
103  }
104 
105  if (!($iProfileId = $this->_oDb->getSavedProfile(getLoggedId())) && empty($_POST)) {
106  $this->_oTemplate->pageAuth($this->_oDb->getClientTitle(ch_get('client_id')));
107  return;
108  }
109 
110  $bConfirm = $iProfileId ? true : (bool)ch_get('confirm');
112 
113  $this->_oServer->handleAuthorizeRequest($oRequest, $oResponse, $bConfirm, $iProfileId);
114 
115  $oResponse->send();
116  }
117 
119  {
120  if (!$this->isAdmin()) {
121  $this->_oTemplate->displayAccessDenied ();
122  return;
123  }
124 
125  $this->_oTemplate->pageStart();
126 
127 
128  ch_import('FormAdd', $this->_aModule);
129  $oForm = new ChOAuthFormAdd($this);
130  $oForm->initChecker();
131 
132  $sContent = '';
133  if ($oForm->isSubmittedAndValid ()) {
134  $oForm->insert ();
135  $sContent = MsgBox(_t('_Success'));
136  }
137  $sContent .= $oForm->getCode ();
138 
139  $aVars = array (
140  'content' => $sContent,
141  );
142  echo $this->_oTemplate->adminBlock ($this->_oTemplate->parseHtmlByName('default_padding', $aVars), _t('_ch_oauth_add'));
143 
144 
145  if (is_array($_POST['clients']) && $_POST['clients'])
146  $this->_oDb->deleteClients($_POST['clients']);
147  ch_import('ChTemplSearchResult');
148  $sControls = ChTemplSearchResult::showAdminActionsPanel('ch-oauth-form-add', array(
149  'ch-oauth-delete' => _t('_Delete'),
150  ), 'clients');
151 
152  $aClients = $this->_oDb->getClients();
153  $aVars = array (
154  'ch_repeat:clients' => $aClients,
155  'controls' => $sControls,
156  );
157  echo $this->_oTemplate->adminBlock ($this->_oTemplate->parseHtmlByName('clients', $aVars), _t('_ch_oauth_clients'));
158 
159 
160  $aVars = array (
161  'content' => _t('_ch_oauth_help_text', CH_WSB_URL_ROOT)
162  );
163  echo $this->_oTemplate->adminBlock ($this->_oTemplate->parseHtmlByName('default_padding', $aVars), _t('_ch_oauth_help'));
164 
165 
166  $this->_oTemplate->addCssAdmin ('forms_adv.css');
167  $this->_oTemplate->pageCodeAdmin (_t('_ch_oauth_administration'));
168  }
169 
170  function isAdmin ()
171  {
172  return $GLOBALS['logged']['admin'] ? true : false;
173  }
174 }
OAuth2\Autoloader\register
static register($dir=null)
Definition: Autoloader.php:25
OAuth2\Response
Definition: Response.php:15
true
if(!defined("TRUE_VAL")) define("TRUE_VAL" true
Definition: constants.inc.php:8
ChOAuthAPI
Definition: ChOAuthAPI.php:11
MsgBox
MsgBox($sText, $iTimer=0)
Definition: design.inc.php:175
ChWsbModule\isLogged
isLogged()
Definition: ChWsbModule.php:113
$aModule
$aModule
Definition: classifieds.php:21
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
ch_get
ch_get($sName)
Definition: utils.inc.php:1664
ChOAuthModule\actionAdministration
actionAdministration()
Definition: ChOAuthModule.php:118
php
login_form
login_form($text="", $member=0, $bAjaxMode=false, $sLoginFormParams='')
Definition: admin.inc.php:13
OAuth2\Storage\Pdo
Definition: Pdo.php:31
OAuth2\Request\createFromGlobals
static createFromGlobals()
Definition: Request.php:192
ChOAuthModule\actionApi
actionApi($sAction)
Definition: ChOAuthModule.php:57
ChOAuthModule
Definition: ChOAuthModule.php:16
$oForm
$oForm
Definition: host_tools.php:42
ChOAuthModule\isAdmin
isAdmin()
Definition: ChOAuthModule.php:170
getLoggedId
getLoggedId()
Definition: profiles.inc.php:32
$_REQUEST
$_REQUEST['action']
Definition: cmd.php:11
OAuth2\Server
Definition: Server.php:48
ChOAuthModule\$_oStorage
$_oStorage
Definition: ChOAuthModule.php:17
ChWsbModule
Definition: ChWsbModule.php:41
$sContent
$sContent
Definition: bottom_menu_compose.php:169
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
$aConfig
$aConfig
Definition: config.php:8
$aVars
$aVars
Definition: license.php:101
ChOAuthModule\$_oAPI
$_oAPI
Definition: ChOAuthModule.php:19
ChOAuthModule\actionAuth
actionAuth()
Definition: ChOAuthModule.php:88
ChOAuthFormAdd
Definition: ChOAuthFormAdd.php:11
ChBaseSearchResult\showAdminActionsPanel
static showAdminActionsPanel($sWrapperId, $aButtons, $sCheckboxName='entry', $bSelectAll=true, $bSelectAllChecked=false, $sCustomHtml='')
Definition: ChBaseSearchResult.php:81
ChOAuthModule\__construct
__construct(&$aModule)
Definition: ChOAuthModule.php:21
OAuth2
Definition: Autoloader.php:3
ChOAuthModule\actionToken
actionToken()
Definition: ChOAuthModule.php:51
$sAction
$sAction
Definition: categories.php:274
ChOAuthModule\$_oServer
$_oServer
Definition: ChOAuthModule.php:18
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
$o
$o
Definition: cmd.php:193
ChWsbDb\getInstance
static getInstance()
Definition: ChWsbDb.php:82
$iProfileId
if( $sMembersList) $iProfileId
Definition: communicator.php:29
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
getLink
getLink($sString, $sUrl)
Definition: utils.inc.php:1433