20 use Firebase\JWT\CachedKeySet;
 
   27 use GuzzleHttp\Client;
 
   28 use GuzzleHttp\Psr7\HttpFactory;
 
   29 use Symfony\Component\Cache\Adapter\FilesystemAdapter;
 
   30 use Symfony\Component\HttpFoundation\Session\Session;
 
   31 use UnexpectedValueException;
 
   67     $this->dbHelper = $dbhelper;
 
   68     if (!$this->session->isStarted()) {
 
   69       $this->session->setName(
'Login');
 
   70       $this->session->start();
 
   87     $authPlugin = $GLOBALS[
"container"]->get(
"helper.restHelper")->getPlugin(
'auth');
 
   88     return $authPlugin->checkUsernameAndPassword($userName, $password);
 
  104     $jwtTokenMatch = 
null;
 
  105     $headerValid = preg_match(
 
  106       "/^bearer (([a-zA-Z0-9\-\_\+\/\=]+)\.([a-zA-Z0-9\-\_\+\/\=]+)\.([a-zA-Z0-9\-\_\+\/\=]+))$/i",
 
  107       $authHeader, $jwtTokenMatch);
 
  108     if (! $headerValid) {
 
  110         "Authorization header is malformed or empty.");
 
  112     $jwtToken           = $jwtTokenMatch[1];
 
  113     $jwtTokenPayload    = $jwtTokenMatch[3];
 
  114     $jwtTokenPayloadDecoded = JWT::jsonDecode(
 
  115       JWT::urlsafeB64Decode($jwtTokenPayload));
 
  119       property_exists($jwtTokenPayloadDecoded, 
'iss') &&
 
  120       $jwtTokenPayloadDecoded->{
'iss'} == $SysConf[
'SYSCONFIG'][
'OidcIssuer']
 
  128       ! property_exists($jwtTokenPayloadDecoded, 
'iss')
 
  132         $jwtTokenPayloadDecoded,
 
  137       throw new HttpForbiddenException(
"Invalid token type sent.");
 
  140     $isUserActive = $this->userDao->isUserIdActive($userId);
 
  141     if (!$isUserActive) {
 
  142       throw new HttpForbiddenException(
"User inactive.");
 
  157     return strtotime(
"today") > strtotime($date);
 
  169     if ($valuesFromDb[
'active'] == 
"f") {
 
  171     } elseif ($this->isDateExpired($valuesFromDb[
'expire_on']) &&
 
  172       $valuesFromDb[
'active'] == 
"t") {
 
  173       $this->dbHelper->invalidateToken($tokenId);
 
  184     return $this->session;
 
  197     $authPlugin = $GLOBALS[
"container"]->get(
"helper.restHelper")->getPlugin(
'auth');
 
  198     $user = $this->userDao->getUserByPk($userId);
 
  199     $row = $this->userDao->getUserAndDefaultGroupByUserName($user[
"user_name"]);
 
  200     if ($groupName !== 
null) {
 
  201       $row[
'group_fk'] = $this->userDao->getGroupIdByName($groupName);
 
  202       $row[
'group_name'] = $groupName;
 
  204     $authPlugin->updateSession($row);
 
  205     $this->getSession()->set(
'token_scope', $scope);
 
  222       "exp" => strtotime($expire . 
" +1 day -1 second"),  
 
  223       "nbf" => strtotime($created),
 
  224       "jti" => base64_encode($jti),
 
  227     return JWT::encode($newJwtToken, $key, 
'HS256');
 
  238     return $this->dbHelper->getMaxTokenValidity();
 
  251     $this->isGroupExisting($groupName);
 
  252     $groupMap = $this->userDao->getUserGroupMap($userId);
 
  253     $userHasGroupAccess = in_array($groupName, $groupMap, 
true);
 
  255     if (!$userHasGroupAccess) {
 
  257         "User has no access to " . $groupName . 
" group");
 
  270     if (empty($this->userDao->getGroupIdByName($groupName))) {
 
  272         "Provided group:" . $groupName . 
" does not exist");
 
  293     $jwks = $this->loadJwks();
 
  296         $jwtTokenDecoded = JWT::decode(
 
  301         throw new \UnexpectedValueException(
"JWKS: " . $e->getMessage());
 
  303       $clientId = $jwtTokenDecoded->{$SysConf[
'SYSCONFIG'][
'OidcClientIdClaim']};
 
  304       $tokenId = $this->dbHelper->getTokenIdFromClientId($clientId);
 
  305       $dbRows = $this->dbHelper->getTokenKey($tokenId);
 
  307       if (empty($dbRows)) {
 
  308         throw new \UnexpectedValueException(
"Invalid token sent.", 403);
 
  310       $this->isTokenActive($dbRows, $tokenId);
 
  311       $userId = $dbRows[
'user_fk'];
 
  312       $tokenScope = $dbRows[
'token_scope'];
 
  313       if ($tokenScope == 
"w") {
 
  314         $tokenScope = 
"write";
 
  315       } elseif ($tokenScope == 
"r") {
 
  316         $tokenScope = 
"read";
 
  318     } 
catch (\UnexpectedValueException $e) {
 
  336     $cacheDir = array_key_exists(
'CACHEDIR', $GLOBALS) ? $GLOBALS[
'CACHEDIR'] : 
null;
 
  337     $cacheDuration = 60 * 60 * 24; 
 
  338     $algInject = $SysConf[
'SYSCONFIG'][
'OidcJwkAlgInject'];
 
  339     if (empty($algInject)) {
 
  346       array_key_exists(
'http_proxy', $SysConf[
'FOSSOLOGY']) &&
 
  347       !empty($SysConf[
'FOSSOLOGY'][
'http_proxy'])
 
  349       $proxy[
'http'] = $SysConf[
'FOSSOLOGY'][
'http_proxy'];
 
  352       array_key_exists(
'https_proxy', $SysConf[
'FOSSOLOGY']) &&
 
  353       !empty($SysConf[
'FOSSOLOGY'][
'https_proxy'])
 
  355       $proxy[
'https'] = $SysConf[
'FOSSOLOGY'][
'https_proxy'];
 
  358       array_key_exists(
'no_proxy', $SysConf[
'FOSSOLOGY']) &&
 
  359       !empty($SysConf[
'FOSSOLOGY'][
'no_proxy'])
 
  361       $proxy[
'no'] = explode(
',', $SysConf[
'FOSSOLOGY'][
'no_proxy']);
 
  364     $version = $SysConf[
'BUILD'][
'VERSION'];
 
  365     $headers = [
'User-Agent' => 
"fossology/$version"];
 
  367     $guzzleClient = 
new Client([
 
  368       'http_errors' => 
false,
 
  370       'headers' => $headers
 
  373     $httpFactory = 
new HttpFactory();
 
  375     $cacheItemPool = 
new FilesystemAdapter(
'rest', $cacheDuration, $cacheDir);
 
  377     return new CachedKeySet(
 
  378       $SysConf[
'SYSCONFIG'][
'OidcJwksURL'],
 
  404                                       &$userId, &$tokenScope)
 
  406     $jwtJti = $jwtTokenPayloadDecoded->{
'jti'};
 
  407     $jwtJti = base64_decode($jwtJti, 
true);
 
  408     list ($tokenId, $userId) = explode(
".", $jwtJti);
 
  410     $dbRows = $this->dbHelper->getTokenKey($tokenId);
 
  411     if (empty($dbRows)) {
 
  414     $this->isTokenActive($dbRows, $tokenId);
 
  416       $jwtTokenDecoded = JWT::decode($jwtToken,
 
  417         new Key($dbRows[
"token_key"], 
'HS256'));
 
  418       $tokenScope = $jwtTokenDecoded->{
'scope'};
 
  419     } 
catch (\UnexpectedValueException $e) {
 
  420       throw new HttpForbiddenException($e->getMessage(), $e);
 
Contains the constants and helpers for authentication of user.
 
static getRestTokenType()
 
Provides helper methods for REST api.
 
isGroupExisting($groupName)
Verify if given Group name exists.
 
static loadJwks()
Load the JWK array.
 
updateUserSession($userId, $scope, $groupName=null)
Update the session using updateSession().
 
validateOauthLogin($jwtToken, &$userId, &$tokenScope)
Validate OAuth token.
 
userHasGroupAccess($userId, $groupName)
Verify if given User Id has access to given Group name.
 
verifyAuthToken($authHeader, &$userId, &$tokenScope)
 
__construct(UserDao $userDao, Session $session, DbHelper $dbhelper)
 
isTokenActive($valuesFromDb, $tokenId)
 
generateJwtToken($expire, $created, $jti, $scope, $key)
 
validateTokenLogin($jwtToken, $jwtTokenPayloadDecoded, &$userId, &$tokenScope)
Validate JWT token from FOSSology.
 
checkUsernameAndPassword($userName, $password)
Check the username and password against the database.
 
Provides helper methods to access database for REST api.