FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
RestAuthMiddleware.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************
3  Copyright (C) 2018-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  ***************************************************************/
27 
33 
39 {
49  public function __invoke($request, $response, $next)
50  {
51  $requestUri = $request->getUri();
52  if (stristr($requestUri->getPath(), "/auth") !== false) {
53  $response = $next($request, $response);
54  } elseif (stristr($requestUri->getPath(), "/version") !== false) {
55  $response = $next($request, $response);
56  } elseif (stristr($requestUri->getPath(), "/tokens") !== false &&
57  stristr($request->getMethod(), "post") !== false) {
58  $response = $next($request, $response);
59  } else {
60  $authHelper = $GLOBALS['container']->get('helper.authHelper');
61  $jwtToken = $request->getHeader('Authorization')[0];
62  $userId = -1;
63  $tokenScope = false;
64  $tokenValid = $authHelper->verifyAuthToken($jwtToken, $userId,
65  $tokenScope);
66  if ($tokenValid === true && (stristr($request->getMethod(), "get") === false &&
67  stristr($tokenScope, "write") === false)) {
68  /*
69  * If the request method is not GET and token scope is not write,
70  * do not allow the request to pass through.
71  */
72  $tokenValid = new Info(403, "Do not have required scope.", InfoType::ERROR);
73  }
74  if ($tokenValid === true) {
75  $groupName = "";
76  $groupName = strval($request->getHeaderLine('groupName'));
77  if (!empty($groupName)) { // if request contains groupName
78  $userHasGroupAccess = $authHelper->userHasGroupAccess($userId, $groupName);
79  if ($userHasGroupAccess === true) {
80  $authHelper->updateUserSession($userId, $tokenScope, $groupName);
81  $response = $next($request, $response);
82  } else { // no group access or group does not exist
83  $response = $response->withJson($userHasGroupAccess->getArray(),
84  $userHasGroupAccess->getCode());
85  }
86  } else { // no groupName passed, use defult groupId saved in DB
87  $authHelper->updateUserSession($userId, $tokenScope);
88  $response = $next($request, $response);
89  }
90  } else {
91  $response = $response->withJson($tokenValid->getArray(),
92  $tokenValid->getCode());
93  }
94  }
95  return $response;
96  }
97 }
Info model to contain general error and return values.
Definition: Info.php:29
Authentication middleware for Slim framework.