Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoBackendAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dojo Project (HES-SO)
Projects
Backend
DojoBackendAPI
Commits
911cb6c7
Commit
911cb6c7
authored
2 years ago
by
michael.minelli
Browse files
Options
Downloads
Patches
Plain Diff
Add session class
parent
f1df7466
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ExpressAPI/src/controllers/Session.ts
+74
-0
74 additions, 0 deletions
ExpressAPI/src/controllers/Session.ts
with
74 additions
and
0 deletions
ExpressAPI/src/controllers/Session.ts
0 → 100644
+
74
−
0
View file @
911cb6c7
import
{
getReasonPhrase
}
from
'
http-status-codes
'
;
import
*
as
jwt
from
'
jsonwebtoken
'
;
import
{
JwtPayload
}
from
'
jsonwebtoken
'
;
import
Config
from
'
../config/Config
'
;
import
express
from
'
express
'
;
import
ApiRequest
from
'
../models/ApiRequest
'
;
import
User
from
'
../models/User
'
;
class
Session
{
private
_profile
:
User
=
new
User
();
get
profile
():
User
{
return
this
.
_profile
;
}
set
profile
(
newProfile
:
User
)
{
newProfile
.
currentSession
=
this
;
this
.
_profile
=
newProfile
;
}
constructor
(
req
:
ApiRequest
)
{
const
authorization
=
req
.
headers
.
authorization
;
if
(
authorization
)
{
const
jwtToken
=
authorization
.
replace
(
'
Bearer
'
,
''
);
try
{
const
jwtData
=
jwt
.
verify
(
jwtToken
,
Config
.
jwtSecretKey
)
as
JwtPayload
;
if
(
jwtData
.
profile
)
{
this
.
profile
.
importFromJsonObject
(
jwtData
.
profile
);
this
.
profile
.
currentSession
=
this
;
}
}
catch
(
err
)
{
}
}
}
private
static
getToken
(
profileJson
:
any
):
string
{
return
profileJson
.
id
===
null
?
null
:
jwt
.
sign
({
profile
:
profileJson
},
Config
.
jwtSecretKey
,
{
expiresIn
:
Config
.
sessionTimeout
});
}
private
async
getResponse
(
code
:
number
,
data
:
any
,
descriptionOverride
?:
string
):
Promise
<
any
>
{
const
profileJson
=
await
this
.
profile
.
toJsonObject
(
false
);
let
reasonPhrase
=
''
;
try
{
reasonPhrase
=
getReasonPhrase
(
code
);
}
catch
{}
return
{
timestamp
:
(
new
Date
()).
toISOString
(),
code
:
code
,
description
:
descriptionOverride
?
descriptionOverride
:
reasonPhrase
,
sessionToken
:
Session
.
getToken
(
profileJson
),
data
:
data
};
}
/*
Send a response to the client
Information: Data could be a promise or an object. If it's a promise, we wait on the data to be resolved before sending the response
*/
sendResponse
(
res
:
express
.
Response
,
code
:
number
,
data
?:
any
,
descriptionOverride
?:
string
)
{
Promise
.
resolve
(
data
).
then
((
toReturn
:
any
)
=>
{
this
.
getResponse
(
code
,
toReturn
,
descriptionOverride
).
then
(
response
=>
{
res
.
status
(
code
).
json
(
response
);
});
});
}
}
export
default
Session
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment