Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projet-cdaw
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Quentin Vrel
projet-cdaw
Commits
92de9aa7
Commit
92de9aa7
authored
Nov 25, 2020
by
Zohten
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor auth (2/2)
parent
f8092631
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
15 deletions
+21
-15
Request.class.php
BackEnd/src/classes/Request.class.php
+2
-3
LoginController.class.php
BackEnd/src/controller/LoginController.class.php
+1
-2
UserController.class.php
BackEnd/src/controller/UserController.class.php
+18
-10
No files found.
BackEnd/src/classes/Request.class.php
View file @
92de9aa7
...
...
@@ -101,8 +101,7 @@ class Request
{
return
$this
->
data
;
}
// returns JWT token in Authorization header or throw an exception
// Return JWT token (string)
// Return JWT token (string) in Authorization header or throw an exception
public
function
getJwtToken
()
{
// Field names are case-insensitive : https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
...
...
@@ -119,11 +118,11 @@ class Request
return
$jwt_token
;
}
// Return array with decodedJWT or error message if decoding fails
public
function
verifyJwtToken
()
{
try
{
$jwt_token
=
$this
->
getJwtToken
();
//print_r($jwt_token);die();
$decodedJWT
=
JWT
::
decode
(
$jwt_token
,
JWT_BACKEND_KEY
,
array
(
'HS256'
));
$arrayResult
=
[
...
...
BackEnd/src/controller/LoginController.class.php
View file @
92de9aa7
...
...
@@ -40,8 +40,7 @@ class LoginController extends Controller
"iss"
=>
JWT_ISSUER
,
"data"
=>
array
(
"id"
=>
$user
->
id
(),
"firstname"
=>
$user
->
firstname
(),
"lastname"
=>
$user
->
lastname
(),
"role"
=>
$user
->
role
(),
"email"
=>
$user
->
email
()
)
);
...
...
BackEnd/src/controller/UserController.class.php
View file @
92de9aa7
...
...
@@ -38,6 +38,20 @@ class UserController extends Controller
return
Response
::
errorResponse
(
$message
);
}
public
function
authUser
(
$id
,
$allowAdmin
=
True
){
// Token phase
$verifyArray
=
$this
->
request
->
verifyJwtToken
();
if
(
$verifyArray
[
'message'
]
!==
"Valid token."
)
{
return
Response
::
unauthorizedResponse
(
$jsonResult
);
}
// Auth phase
$data
=
$verifyArray
[
'decodedJWT'
]
->
data
;
if
((
$data
->
id
!=
$id
)
&&
(
$data
->
role
!=
2
))
{
$message
=
json_encode
([
"message"
=>
"You don't have access to this account."
]);
return
Response
::
unauthorizedResponse
(
$message
);
}
}
/**
* Get all users in USER table
*
...
...
@@ -69,17 +83,11 @@ class UserController extends Controller
*/
protected
function
updateUser
(
$array
)
{
//
T
oken phase
$
verifyArray
=
$this
->
request
->
verifyJwtToken
(
);
if
(
$verifyArray
[
'message'
]
!==
"Valid token."
)
{
return
Response
::
unauthorizedResponse
(
$jsonResult
)
;
//
Auth with t
oken phase
$
authError
=
$this
->
authUser
(
$array
[
'id'
]
);
if
(
$authError
)
{
return
authError
;
}
// Auth phase
if
(
$verifyArray
[
'decodedJWT'
]
->
data
->
id
!=
$array
[
'id'
])
{
$message
=
json_encode
([
"message"
=>
"You don't have access to this account."
]);
return
Response
::
unauthorizedResponse
(
$message
);
}
// Update phase
User
::
updateUser
(
$array
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment