69 $this->dbHelper = $dbhelper;
70 if (!$this->session->isStarted()) {
71 $this->session->setName(
'Login');
72 $this->session->start();
88 $authPlugin = $GLOBALS[
"container"]->get(
"helper.restHelper")->getPlugin(
'auth');
89 return $authPlugin->checkUsernameAndPassword($userName, $password);
103 $jwtTokenMatch = null;
104 $headerValid = preg_match(
105 "/^bearer (([a-zA-Z0-9\-\_\+\/\=]+)\.([a-zA-Z0-9\-\_\+\/\=]+)\.([a-zA-Z0-9\-\_\+\/\=]+))$/i",
106 $authHeader, $jwtTokenMatch);
108 if (! $headerValid) {
109 $returnValue =
new Info(400,
"Authorization header is malformed or empty.",
112 $jwtToken = $jwtTokenMatch[1];
113 $jwtTokenPayload = $jwtTokenMatch[3];
114 $jwtTokenPayloadDecoded = JWT::jsonDecode(
115 JWT::urlsafeB64Decode($jwtTokenPayload));
117 if ($jwtTokenPayloadDecoded->{
'jti'} === null) {
118 return new Info(403,
"Invalid token sent.", InfoType::ERROR);
120 $jwtJti = $jwtTokenPayloadDecoded->{
'jti'};
121 $jwtJti = base64_decode($jwtJti,
true);
122 list ($tokenId, $userId) = explode(
".", $jwtJti);
124 $dbRows = $this->dbHelper->getTokenKey($tokenId);
126 if (empty($dbRows)) {
127 $returnValue =
new Info(403,
"Invalid token sent.", InfoType::ERROR);
128 } elseif ($isTokenActive !==
true) {
129 $returnValue = $isTokenActive;
132 $jwtTokenDecoded = JWT::decode($jwtToken, $dbRows[
"token_key"], [
'HS256']);
133 $tokenScope = $jwtTokenDecoded->{
'scope'};
134 }
catch (\UnexpectedValueException $e) {
135 $returnValue =
new Info(403, $e->getMessage(), InfoType::ERROR);
150 return strtotime(
"today") > strtotime($date);
163 $isPayloadValid =
true;
164 if ($valuesFromDb[
'active'] ==
"f") {
165 $isPayloadValid =
new Info(403,
"Token expired.", InfoType::ERROR);
166 } elseif ($this->
isDateExpired($valuesFromDb[
'expire_on']) &&
167 $valuesFromDb[
'active'] ==
"t") {
168 $this->dbHelper->invalidateToken($tokenId);
169 $isPayloadValid =
new Info(403,
"Token expired.", InfoType::ERROR);
171 return $isPayloadValid;
193 $authPlugin = $GLOBALS[
"container"]->get(
"helper.restHelper")->getPlugin(
'auth');
194 $user = $this->userDao->getUserByPk($userId);
195 $row = $this->userDao->getUserAndDefaultGroupByUserName($user[
"user_name"]);
196 if ($groupName !== null) {
197 $row[
'group_fk'] = $this->userDao->getGroupIdByName($groupName);
198 $row[
'group_name'] = $groupName;
200 $authPlugin->updateSession($row);
201 $this->
getSession()->set(
'token_scope', $scope);
217 "exp" => strtotime($expire .
" +1 day -1 second"),
218 "nbf" => strtotime($created),
219 "jti" => base64_encode($jti),
222 return JWT::encode($newJwtToken, $key,
'HS256');
233 return $this->dbHelper->getMaxTokenValidity();
247 if ($isGroupExisting ===
true) {
248 $groupMap = $this->userDao->getUserGroupMap($userId);
249 $userHasGroupAccess = in_array($groupName, $groupMap,
true);
251 return $isGroupExisting;
254 if (!$userHasGroupAccess) {
255 $userHasGroupAccess =
new Info(403,
"User has no access to " . $groupName .
" group", InfoType::ERROR);
257 return $userHasGroupAccess;
269 if (! empty($this->userDao->getGroupIdByName($groupName))) {
272 return new Info(403,
"Provided group:" . $groupName .
" does not exist", InfoType::ERROR);
updateUserSession($userId, $scope, $groupName=null)
Update the session using updateSession().
verifyAuthToken($authHeader, &$userId, &$tokenScope)
isGroupExisting($groupName)
Verify if given Group name exists.
generateJwtToken($expire, $created, $jti, $scope, $key)
Provides helper methods for REST api.
__construct(UserDao $userDao, Session $session, DbHelper $dbhelper)
userHasGroupAccess($userId, $groupName)
Verify if given User Id has access to given Group name.
Info model to contain general error and return values.
Provides helper methods to access database for REST api.
isTokenActive($valuesFromDb, $tokenId)
checkUsernameAndPassword($userName, $password)
Check the username and password against the database.