Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoCLI
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
UI
DojoCLI
Commits
c8442524
Commit
c8442524
authored
1 year ago
by
michael.minelli
Browse files
Options
Downloads
Patches
Plain Diff
HttpManager => Detect gitlab token expiration and call refreshTokens
parent
d1804273
No related branches found
No related tags found
No related merge requests found
Pipeline
#26749
failed
1 year ago
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
NodeApp/src/managers/HttpManager.ts
+29
-8
29 additions, 8 deletions
NodeApp/src/managers/HttpManager.ts
with
29 additions
and
8 deletions
NodeApp/src/managers/HttpManager.ts
+
29
−
8
View file @
c8442524
import
axios
,
{
AxiosRequestHeaders
}
from
'
axios
'
;
import
axios
,
{
AxiosRequestHeaders
}
from
'
axios
'
;
import
SessionManager
from
'
./SessionManager
'
;
import
SessionManager
from
'
./SessionManager
'
;
import
FormData
from
'
form-data
'
;
import
FormData
from
'
form-data
'
;
import
GitlabManager
from
'
./GitlabManager
'
;
import
{
StatusCodes
}
from
'
http-status-codes
'
;
import
{
StatusCodes
}
from
'
http-status-codes
'
;
import
ClientsSharedConfig
from
'
../sharedByClients/config/ClientsSharedConfig
'
;
import
ClientsSharedConfig
from
'
../sharedByClients/config/ClientsSharedConfig
'
;
import
{
version
}
from
'
../config/Version
'
;
import
{
version
}
from
'
../config/Version
'
;
...
@@ -35,15 +34,15 @@ class HttpManager {
...
@@ -35,15 +34,15 @@ class HttpManager {
}
}
if
(
SessionManager
.
isLogged
)
{
if
(
SessionManager
.
isLogged
)
{
config
.
headers
.
Authorization
=
`Bearer
${
SessionManager
.
t
oken
}
`
;
config
.
headers
.
Authorization
=
`Bearer
${
SessionManager
.
apiT
oken
}
`
;
}
}
config
.
headers
[
'
client
'
]
=
'
DojoCLI
'
;
config
.
headers
[
'
client
'
]
=
'
DojoCLI
'
;
config
.
headers
[
'
client-version
'
]
=
version
;
config
.
headers
[
'
client-version
'
]
=
version
;
}
}
if
(
Gitlab
Manager
.
isLogged
&&
config
.
url
&&
config
.
url
.
indexOf
(
SharedConfig
.
gitlab
.
apiURL
)
!==
-
1
)
{
if
(
Session
Manager
.
gitlabCredentials
.
accessToken
&&
config
.
url
&&
config
.
url
.
indexOf
(
SharedConfig
.
gitlab
.
apiURL
)
!==
-
1
)
{
config
.
headers
[
'
PRIVATE-TOKEN
'
]
=
GitlabManager
.
t
oken
;
config
.
headers
.
Authorization
=
`Bearer
${
SessionManager
.
gitlabCredentials
.
accessT
oken
}
`
;
}
}
return
config
;
return
config
;
...
@@ -66,13 +65,35 @@ class HttpManager {
...
@@ -66,13 +65,35 @@ class HttpManager {
private
registerResponseInterceptor
()
{
private
registerResponseInterceptor
()
{
axios
.
interceptors
.
response
.
use
((
response
)
=>
{
axios
.
interceptors
.
response
.
use
((
response
)
=>
{
if
(
response
.
data
&&
response
.
data
.
sessionToken
)
{
if
(
response
.
data
&&
response
.
data
.
sessionToken
)
{
SessionManager
.
t
oken
=
response
.
data
.
sessionToken
;
SessionManager
.
apiT
oken
=
response
.
data
.
sessionToken
;
}
}
return
response
;
return
response
;
},
(
error
)
=>
{
},
async
(
error
)
=>
{
if
(
error
.
response
)
{
if
(
error
.
response
)
{
if
(
error
.
response
.
status
===
StatusCodes
.
METHOD_NOT_ALLOWED
&&
error
.
response
.
data
)
{
const
originalConfig
=
error
.
config
;
const
isFromApi
=
error
.
response
.
config
.
url
&&
error
.
response
.
config
.
url
.
indexOf
(
ClientsSharedConfig
.
apiURL
)
!==
-
1
;
const
isFromGitlab
=
error
.
response
.
config
.
url
&&
error
.
response
.
config
.
url
.
indexOf
(
SharedConfig
.
gitlab
.
URL
)
!==
-
1
;
// Try to refresh the Gitlab tokens if the request have failed with a 401 error
if
(
error
.
response
.
status
===
StatusCodes
.
UNAUTHORIZED
&&
isFromGitlab
&&
!
originalConfig
.
_retry
)
{
originalConfig
.
_retry
=
true
;
try
{
await
SessionManager
.
refreshTokens
();
return
axios
(
originalConfig
);
}
catch
(
_error
:
any
)
{
if
(
_error
.
response
&&
_error
.
response
.
data
)
{
return
Promise
.
reject
(
_error
.
response
.
data
);
}
return
Promise
.
reject
(
_error
);
}
}
if
(
error
.
response
.
status
===
StatusCodes
.
METHOD_NOT_ALLOWED
&&
isFromApi
&&
error
.
response
.
data
)
{
const
data
:
DojoBackendResponse
<
{}
>
=
error
.
response
.
data
;
const
data
:
DojoBackendResponse
<
{}
>
=
error
.
response
.
data
;
switch
(
data
.
code
)
{
switch
(
data
.
code
)
{
...
@@ -88,7 +109,7 @@ class HttpManager {
...
@@ -88,7 +109,7 @@ class HttpManager {
}
}
if
(
this
.
handleAuthorizationCommandErrors
)
{
if
(
this
.
handleAuthorizationCommandErrors
)
{
if
(
error
.
response
.
url
&&
error
.
response
.
url
.
indexOf
(
ClientsSharedConfig
.
apiURL
)
!==
-
1
)
{
if
(
isFromApi
)
{
switch
(
error
.
response
.
status
)
{
switch
(
error
.
response
.
status
)
{
case
StatusCodes
.
UNAUTHORIZED
:
case
StatusCodes
.
UNAUTHORIZED
:
this
.
requestError
(
'
Session expired or does not exist. Please login again.
'
);
this
.
requestError
(
'
Session expired or does not exist. Please login again.
'
);
...
...
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