FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AjaxManageToken.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2019 Siemens AG
4  * Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  **********************************************************/
19 namespace Fossology\UI\Ajax;
20 
27 
33 {
34 
35  const NAME = "manage-token";
36 
39  private $dbManager;
40 
41  function __construct()
42  {
43  parent::__construct(self::NAME,
44  array(
45  self::PERMISSION => Auth::PERM_WRITE
46  ));
47  $this->dbManager = $this->getObject('db.manager');
48  }
49 
55  protected function handle(Request $request)
56  {
57  $task = GetParm('task', PARM_STRING);
58  $tokenId = GetParm('token-id', PARM_STRING);
59  $response = null;
60 
61  list($tokenPk, $userId) = explode(".", $tokenId);
62  if (Auth::getUserId() != $userId) {
63  $task = "invalid";
64  } else {
65  $verifySql = "SELECT user_fk FROM personal_access_tokens " .
66  "WHERE pat_pk = $1 LIMIT 1;";
67 
68  $row = $this->dbManager->getSingleRow($verifySql, [$tokenPk],
69  __METHOD__ . ".verifyToken");
70  if (empty($row) || $row['user_fk'] != $userId) {
71  $task = "invalid";
72  }
73  }
74  switch ($task) {
75  case "reveal":
76  $response = new JsonResponse($this->revealToken($tokenPk,
77  $request->getHost()));
78  break;
79  case "revoke":
80  $response = new JsonResponse($this->invalidateToken($tokenPk));
81  break;
82  default:
83  $response = new JsonResponse(["status" => false], 400);
84  }
85  return $response;
86  }
87 
95  private function revealToken($tokenPk, $hostname)
96  {
97  global $container;
98  $restDbHelper = $container->get("helper.dbHelper");
99  $authHelper = $container->get('helper.authHelper');
100  $user_pk = Auth::getUserId();
101  $jti = "$tokenPk.$user_pk";
102 
103  $tokenInfo = $restDbHelper->getTokenKey($tokenPk);
104  $tokenScope = array_search($tokenInfo['token_scope'], RestHelper::SCOPE_DB_MAP);
105 
106  $jwtToken = $authHelper->generateJwtToken($tokenInfo['expire_on'],
107  $tokenInfo['created_on'], $jti, $tokenScope, $tokenInfo['token_key']);
108  return array(
109  "status" => true,
110  "token" => $jwtToken
111  );
112  }
113 
120  private function invalidateToken($tokenPk)
121  {
122  global $container;
123  $restDbHelper = $container->get("helper.dbHelper");
124  $restDbHelper->invalidateToken($tokenPk);
125  return array(
126  "status" => true
127  );
128  }
129 }
130 
131 register_plugin(new AjaxManageToken());
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
Class to handle ajax calls to revoke an API token.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
handle(Request $request)
Revoke an active API token.