Browse Source

redirect to sign in page if auth as expired or is invalid

use-env-var-docker
kenshin-samourai 5 years ago
parent
commit
1b005818ad
  1. 12
      static/admin/dmt/addresses-tools/addresses-tools.js
  2. 3
      static/admin/dmt/blocks-rescan/blocks-rescan.js
  3. 1
      static/admin/dmt/index.html
  4. 3
      static/admin/dmt/pairing/pairing.js
  5. 6
      static/admin/dmt/pushtx/pushtx.js
  6. 6
      static/admin/dmt/status/status.js
  7. 3
      static/admin/dmt/txs-tools/txs-tools.js
  8. 12
      static/admin/dmt/xpubs-tools/xpubs-tools.js
  9. 1
      static/admin/index.html
  10. 3
      static/admin/index.js
  11. 19
      static/admin/lib/auth-utils.js
  12. 24
      static/admin/lib/errors-utils.js
  13. 9
      static/admin/lib/messages.js

12
static/admin/dmt/addresses-tools/addresses-tools.js

@ -30,8 +30,7 @@ const screenAddressesToolsScript = {
lib_api.getExplorerPairingInfo().then(explorerInfo => { lib_api.getExplorerPairingInfo().then(explorerInfo => {
this.explorerInfo = explorerInfo this.explorerInfo = explorerInfo
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },
@ -68,8 +67,7 @@ const screenAddressesToolsScript = {
this.showImportForm(false) this.showImportForm(false)
} }
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
throw e throw e
}) })
}, },
@ -83,8 +81,7 @@ const screenAddressesToolsScript = {
lib_msg.displayInfo('Import complete') lib_msg.displayInfo('Import complete')
}) })
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },
@ -97,8 +94,7 @@ const screenAddressesToolsScript = {
lib_msg.displayInfo('Rescan complete') lib_msg.displayInfo('Rescan complete')
}) })
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },

3
static/admin/dmt/blocks-rescan/blocks-rescan.js

@ -32,8 +32,7 @@ const screenBlocksRescanScript = {
const msg = `successfully rescanned blocks between height ${fromHeightRes} and height ${toHeightRes}` const msg = `successfully rescanned blocks between height ${fromHeightRes} and height ${toHeightRes}`
lib_msg.displayInfo(msg) lib_msg.displayInfo(msg)
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}).then(() => { }).then(() => {
$('#rescan-from-height').val('') $('#rescan-from-height').val('')
$('#rescan-to-height').val('') $('#rescan-to-height').val('')

1
static/admin/dmt/index.html

@ -15,6 +15,7 @@
<script src="../lib/auth-utils.js"></script> <script src="../lib/auth-utils.js"></script>
<script src="../lib/format-utils.js"></script> <script src="../lib/format-utils.js"></script>
<script src="../lib/messages.js"></script> <script src="../lib/messages.js"></script>
<script src="../lib/errors-utils.js"></script>
<script src="index.js"></script> <script src="index.js"></script>
</head> </head>

3
static/admin/dmt/pairing/pairing.js

@ -27,8 +27,7 @@ const screenPairingScript = {
lib_msg.cleanMessagesUi() lib_msg.cleanMessagesUi()
return result return result
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
return result return result
}) })
}, },

6
static/admin/dmt/pushtx/pushtx.js

@ -29,8 +29,7 @@ const pushtxScript = {
$('#pushed-uptime').text('-') $('#pushed-uptime').text('-')
$('#pushed-count').text('-') $('#pushed-count').text('-')
$('#pushed-amount').text('-') $('#pushed-amount').text('-')
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },
@ -48,8 +47,7 @@ const pushtxScript = {
lib_msg.cleanMessagesUi() lib_msg.cleanMessagesUi()
} }
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },

6
static/admin/dmt/status/status.js

@ -27,8 +27,7 @@ const statusScript = {
$('#tracker-status-ind').css('color', '#f77c7c') $('#tracker-status-ind').css('color', '#f77c7c')
$('#tracker-uptime').text('-') $('#tracker-uptime').text('-')
$('#tracker-chaintip').text('-') $('#tracker-chaintip').text('-')
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },
@ -58,8 +57,7 @@ const statusScript = {
$('#node-network').text('-') $('#node-network').text('-')
$('#node-conn').text('-') $('#node-conn').text('-')
$('#node-relay-fee').text('-') $('#node-relay-fee').text('-')
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },

3
static/admin/dmt/txs-tools/txs-tools.js

@ -24,8 +24,7 @@ const screenTxsToolsScript = {
lib_api.getExplorerPairingInfo().then(explorerInfo => { lib_api.getExplorerPairingInfo().then(explorerInfo => {
this.explorerInfo = explorerInfo this.explorerInfo = explorerInfo
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },

12
static/admin/dmt/xpubs-tools/xpubs-tools.js

@ -32,8 +32,7 @@ const screenXpubsToolsScript = {
lib_api.getExplorerPairingInfo().then(explorerInfo => { lib_api.getExplorerPairingInfo().then(explorerInfo => {
this.explorerInfo = explorerInfo this.explorerInfo = explorerInfo
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },
@ -70,8 +69,7 @@ const screenXpubsToolsScript = {
this.showImportForm(false) this.showImportForm(false)
} }
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
throw e throw e
}) })
}, },
@ -101,8 +99,7 @@ const screenXpubsToolsScript = {
lib_msg.displayInfo('Import complete') lib_msg.displayInfo('Import complete')
}) })
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },
@ -119,8 +116,7 @@ const screenXpubsToolsScript = {
lib_msg.displayInfo('Rescan complete') lib_msg.displayInfo('Rescan complete')
}) })
}).catch(e => { }).catch(e => {
lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) lib_errors.processError(e)
console.log(e)
}) })
}, },

1
static/admin/index.html

@ -14,6 +14,7 @@
<script src="lib/api-wrapper.js"></script> <script src="lib/api-wrapper.js"></script>
<script src="lib/auth-utils.js"></script> <script src="lib/auth-utils.js"></script>
<script src="lib/messages.js"></script> <script src="lib/messages.js"></script>
<script src="lib/errors-utils.js"></script>
<script src="index.js"></script> <script src="index.js"></script>
</head> </head>

3
static/admin/index.js

@ -33,8 +33,7 @@ function login() {
} }
}, },
function (jqxhr) { function (jqxhr) {
let msg = lib_msg.extractJqxhrErrorMsg(jqxhr) lib_errors.processError(jqxhr)
lib_msg.displayErrors(msg)
} }
) )
} }

19
static/admin/lib/auth-utils.js

@ -9,6 +9,9 @@ const lib_auth = {
/* SessionStorage Key used for refresh token */ /* SessionStorage Key used for refresh token */
SESSION_STORE_REFRESH_TOKEN: 'refresh_token', SESSION_STORE_REFRESH_TOKEN: 'refresh_token',
/* SessionStorage Key used for the timestamp of the refresh token */
SESSION_STORE_REFRESH_TOKEN_TS: 'refresh_token_ts',
/* JWT Scheme */ /* JWT Scheme */
JWT_SCHEME: 'Bearer', JWT_SCHEME: 'Bearer',
@ -43,6 +46,8 @@ const lib_auth = {
* Stores refresh token in session storage * Stores refresh token in session storage
*/ */
setRefreshToken: function(token) { setRefreshToken: function(token) {
const now = new Date();
sessionStorage.setItem(this.SESSION_STORE_REFRESH_TOKEN_TS, now.getTime())
sessionStorage.setItem(this.SESSION_STORE_REFRESH_TOKEN, token) sessionStorage.setItem(this.SESSION_STORE_REFRESH_TOKEN, token)
}, },
@ -56,17 +61,23 @@ const lib_auth = {
const now = new Date(); const now = new Date();
const atts = sessionStorage.getItem(this.SESSION_STORE_ACCESS_TOKEN_TS) const atts = sessionStorage.getItem(this.SESSION_STORE_ACCESS_TOKEN_TS)
const timeElapsed = (now.getTime() - atts) / 1000 let timeElapsed = (now.getTime() - atts) / 1000
// Refresh the access token if more than 5mn // Refresh the access token if more than 5mn
if (timeElapsed > 300) { if (timeElapsed > 300) {
const dataJson = { // Check if refresh token has expired or is about to expire
'rt': this.getRefreshToken() const rtts = sessionStorage.getItem(this.SESSION_STORE_REFRESH_TOKEN_TS)
if ((now.getTime() - rtts) / 1000 > 7200 - 60) {
// Force user to sign in again
this.logout()
return
} }
let self = this let self = this
let deferred = lib_api.refreshToken(dataJson) let deferred = lib_api.refreshToken({
'rt': this.getRefreshToken()
})
deferred.then( deferred.then(
function (result) { function (result) {

24
static/admin/lib/errors-utils.js

@ -0,0 +1,24 @@
const lib_errors = {
// Extract jqxhr error message
extractJqxhrErrorMsg: function(jqxhr) {
let hasErrorMsg = ('responseJSON' in jqxhr) &&
(jqxhr['responseJSON'] != null) &&
('error' in jqxhr['responseJSON'])
return hasErrorMsg ? jqxhr['responseJSON']['error'] : jqxhr.statusText
},
// Manage errors
processError: function(e) {
const errorMsg = this.extractJqxhrErrorMsg(e)
// Redirect to sign in page if authentication error
if (errorMsg == 'Invalid JSON Web Token' || errorMsg == 'Missing JSON Web Token') {
lib_auth.logout()
} else {
lib_msg.displayErrors(errorMsg)
console.log(e)
}
},
}

9
static/admin/lib/messages.js

@ -1,14 +1,5 @@
const lib_msg = { const lib_msg = {
// Extracts jqxhr error message
extractJqxhrErrorMsg: function(jqxhr) {
let hasErrorMsg = ('responseJSON' in jqxhr) &&
(jqxhr['responseJSON'] != null) &&
('error' in jqxhr['responseJSON'])
return hasErrorMsg ? jqxhr['responseJSON']['error'] : jqxhr.statusText
},
// UI functions // UI functions
addTextinID: function(text, id){ addTextinID: function(text, id){
$(id).html(text.toUpperCase()) $(id).html(text.toUpperCase())

Loading…
Cancel
Save