-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...rvices-swagger/src/main/resources/META-INF/resources/webjars/swagger-ui/custom-swagger.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
function buildFormData(data) { | ||
let formArr = []; | ||
|
||
for (let name in data) { | ||
let val = data[name]; | ||
if (val !== undefined && val !== '') { | ||
formArr.push( | ||
[name, '=', encodeURIComponent(val).replace(/%20/g, '+')].join(''), | ||
); | ||
} | ||
} | ||
return formArr.join('&'); | ||
} | ||
|
||
function getAuth() { | ||
return window.ui.authSelectors.authorized().toJS()?.oauth; | ||
} | ||
|
||
function tryRefreshOauth2Token() { | ||
const auth = getAuth(); | ||
if (!auth) { | ||
console.log(`Swagger is not authorized. Can't refresh token.`); | ||
return; | ||
} | ||
|
||
const {schema, clientId, token} = auth; | ||
const errors = []; | ||
|
||
if (schema == null) { | ||
errors.push('Invalid auth: missing schema'); | ||
} | ||
if (schema?.tokenUrl == null) { | ||
errors.push('Invalid auth schema: missing tokenUrl'); | ||
} | ||
if (clientId == null) { | ||
errors.push('Invalid auth: missing clientId'); | ||
} | ||
|
||
if (token == null) { | ||
errors.push('Invalid auth: missing token'); | ||
} | ||
if (token?.refresh_token == null) { | ||
errors.push('Invalid auth: missing refresh token'); | ||
} | ||
if (token?.scope == null) { | ||
errors.push('Invalid auth: missing scope'); | ||
} | ||
if (errors.length) { | ||
console.log("Can't refresh token due to the following issues:"); | ||
errors.forEach(console.log); | ||
return; | ||
} | ||
|
||
const form = { | ||
grant_type: 'refresh_token', | ||
refresh_token: token.refresh_token, | ||
client_id: clientId, | ||
scope: token.scope, | ||
}; | ||
|
||
console.log(`Refreshing token...`); | ||
window.ui.authActions.authorizeRequest({ | ||
body: buildFormData(form), | ||
name, | ||
url: schema.tokenUrl, | ||
auth, | ||
}); | ||
} | ||
|
||
function startClock() { | ||
function tick() { | ||
const remainingRefreshTime = window.tokenRefreshTime - Date.now(); | ||
|
||
if (remainingRefreshTime < 0) { | ||
clearInterval(window.tokenClockInterval); | ||
tryRefreshOauth2Token(); | ||
} | ||
} | ||
|
||
if (window.tokenClockInterval) clearInterval(window.tokenClockInterval); | ||
|
||
window.tokenClockInterval = setInterval(tick, 500); | ||
tick(); | ||
} | ||
|
||
let patchTries = 10; | ||
|
||
function patchRefreshHook() { | ||
if (!window?.ui?.authActions?.authorizeOauth2) { | ||
if (patchTries) { | ||
patchTries--; | ||
setTimeout(patchRefreshHook, 1000); | ||
console.log( | ||
'Missing patch target function "window.ui.authActions.authorizeOauth2", retrying in 1s...', | ||
); | ||
return; | ||
} | ||
console.log( | ||
'Cannot patch OAuth token refresh hook. Missing patch target function "window.ui.authActions.authorizeOauth2"', | ||
); | ||
return; | ||
} | ||
|
||
console.log('Patching OAuth token refresh hook...'); | ||
const origAuthorizeOauth2 = window.ui.authActions.authorizeOauth2; | ||
|
||
window.ui.authActions.authorizeOauth2 = (payload) => { | ||
// If the token can expire, schedule a token refresh and update the timer | ||
if (payload.token.expires_in) { | ||
const tokenRefreshTimeout = payload.token.expires_in * 750; | ||
console.log( | ||
`Refreshable token detected. Scheduling token refresh in ${( | ||
tokenRefreshTimeout / | ||
1000 / | ||
60 | ||
).toFixed(1)}min (expires in ${(payload.token.expires_in / 60).toFixed( | ||
1, | ||
)}min)...`, | ||
); | ||
window.tokenRefreshTime = Date.now() + payload.token.expires_in * 750; | ||
|
||
// Start the clock | ||
startClock(); | ||
} | ||
|
||
return origAuthorizeOauth2(payload); | ||
}; | ||
|
||
startClock(); | ||
} | ||
|
||
patchRefreshHook(); |
21 changes: 21 additions & 0 deletions
21
...loud-services-swagger/src/main/resources/META-INF/resources/webjars/swagger-ui/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Swagger UI</title> | ||
<link rel="stylesheet" type="text/css" href="./swagger-ui.css"/> | ||
<link rel="stylesheet" type="text/css" href="index.css"/> | ||
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32"/> | ||
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16"/> | ||
</head> | ||
|
||
<body> | ||
<div id="swagger-ui"></div> | ||
|
||
<script src="./swagger-ui-bundle.js" charset="UTF-8"></script> | ||
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"></script> | ||
<script src="./swagger-initializer.js" charset="UTF-8"> </script> | ||
<script src="./custom-swagger.js" charset="UTF-8"> </script> | ||
|
||
</body> | ||
</html> |