Skip to content

Commit

Permalink
bugfix options ; await for interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy76 committed Jan 8, 2025
1 parent 298d8c1 commit 1b5a36b
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 105 deletions.
2 changes: 1 addition & 1 deletion client/src/lib/TokenStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var TokenStorage = {
// console.log(JSON.parse(jsonPayload))
return JSON.parse(jsonPayload)
}catch(err){
console.log("Error in getPayload: " + err)
// console.log("Error in getPayload: " + err)
return {}
}
},
Expand Down
98 changes: 47 additions & 51 deletions client/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,66 +28,62 @@ import "./../public/assets/main.scss"
axios.interceptors.response.use( (response) => {
// Return a successful response back to the calling service
return response;
}, (error) => {
}, async (error) => {
// Return any error which is not due to authentication back to the calling service
if (error.response?.status !== 401) {
return new Promise((resolve, reject) => {
reject(error);
});
throw error;
}else{
// Logout user if token refresh didn't work or user is disabled
if (error.config.url == `${process.env.BASE_URL}api/v1/token` || error.response.message == 'Account is disabled.') {
// temp vue to have toast
console.log("Unauthorized detected")
console.log("Error : " + error)

var originConfig = error?.config || {};
const originUrl = error?.config?.url || "";
const originResponseMessage = error?.response?.message || "";
const originResponseErrorMessage = error?.response?.data?.error || "";

try{
if(!originConfig || !originUrl){
throw new Error("Unauthorized detected, redirecting to login (no origin config or url)")
}

console.log("Origin url : " + originUrl)
console.log("Origin response message : " + originResponseMessage)
console.log("Origin response error message : " + originResponseErrorMessage)

// Logout user if token refresh didn't work or user is disabled or there was no access to the resource, not token related
if (originUrl == `/api/v1/token` || originResponseMessage == 'Account is disabled.' || originResponseMessage.includes('No Access')) {

// clear our tokens from browser and push to login
TokenStorage.clear();
router.push({ name: 'Login', query: {from: this?.$route?.fullPath || ""} }).catch(err => {});
throw new Error("Unauthorized detected, redirecting to login (no access)")
}

// Try request again with new token
console.log("Trying to refresh tokens")
const newToken = await TokenStorage.getNewToken();

console.log("Retrying previous call with new tokens")
// New request with new token
originConfig.headers['Authorization'] = `Bearer ${newToken}`;
const retryResponse = await axios.request(originConfig);
if(retryResponse.error){
throw retryResponse.error;
}else{
return retryResponse;
}

}catch(e){

const vm = new Vue({})
var message = "Unauthorized. Access denied."
if(error.response.data && error.response.data.message){
message += "\r\n" + error.response.data.message
if(originResponseMessage){
message = [message,originResponseMessage,originResponseErrorMessage,e.message].join("\r\n")
}
vm.$toast.warning(message)
// clear our tokens from browser and push to login
TokenStorage.clear();
router.push({ name: 'Login', query: {from: this.$route.fullPath} }).catch(err => {});

return new Promise((resolve, reject) => {
reject(message);
});
}

// Try request again with new token
//console.log("Trying to refresh tokens")
try{
return TokenStorage.getNewToken()
.then((token) => {
// console.log("Refresh done")
//console.log("Retrying previous call with new tokens")
// New request with new token
const config = error.config;
config.headers['Authorization'] = `Bearer ${token}`;
throw e

return new Promise((resolve, reject) => {
axios.request(config).then(response => {
if(response.error){
Promise.reject(error);
reject(error);
router.push({ name: 'Login', query: {from: this.$route.fullPath} }).catch(err => {});
}else{
resolve(response);
}
}).catch((error) => {
Promise.reject(error);
reject(error);
router.push({ name: 'Login', query: {from: this.$route.fullPath} }).catch(err => {});
})
});
})
.catch((from) => {
// token refresh error -> let's login, unless we are already there
if(!from.includes("/login")){
router.push({ name: 'Login', query:{from:from}}).catch(err => {});
}
});
}catch(err){
console.log(err)
}
}
});
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Awx.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="id-card" /> Awx</h1>
<div class="columns">
Expand Down Expand Up @@ -48,7 +48,8 @@
name: "AfAwx",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaCheckbox,BulmaTextArea,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/AzureAd.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="address-card" /> MS Entra ID</h1>
<div class="columns">
Expand Down Expand Up @@ -58,7 +58,8 @@
name: "AfAzureAd",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaCheckbox,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Credentials.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<BulmaModal v-if="showDelete && credential.name" title="Delete" action="Delete" @click="deleteCredential();showDelete=false" @close="showDelete=false" @cancel="showDelete=false">Are you sure you want to delete Credential '{{ credential.name}}'</BulmaModal>
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="lock" /> Credentials</h1>
Expand Down Expand Up @@ -70,7 +70,8 @@
name:"AfCredentials",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaModal,BulmaAdminTable,BulmaCheckbox,BulmaSelect,BulmaSettingsMenu},
setup(){
Expand Down
11 changes: 8 additions & 3 deletions client/src/views/Designer.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin && loaded" class="section">
<section v-if="(profile?.options?.showDesigner ?? isAdmin) && loaded" class="section">
<BulmaQuickView class="quickview" v-if="warnings && showWarnings" title="Form warnings" footer="" @close="showWarnings=false">
<p v-for="w,i in warnings" :key="'warning'+i" class="mb-3" v-html="w"></p>
</BulmaQuickView>
Expand Down Expand Up @@ -297,7 +297,8 @@
name: "Designer",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{VueCodeEditor,BulmaModal,BulmaQuickView,BulmaAdvancedSelect,BulmaCheckRadio},
data(){
Expand Down Expand Up @@ -652,7 +653,11 @@
},
save(close=false) {
var ref= this;
this.$refs.saveButton.focus()
try{
this.$refs.saveButton.focus()
}catch{
// do nothing
}
this.$nextTick(()=>{
if(ref.warnings.length==0 && ref.formConfig && ref.formDirty){
axios.post(`${process.env.BASE_URL}api/v1/config/`,{forms:ref.formConfig},TokenStorage.getAuthentication())
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Groups.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<BulmaModal v-if="showDelete && group.name" title="Delete" action="Delete" @click="deleteGroup();showDelete=false" @close="showDelete=false" @cancel="showDelete=false">Are you sure you want to delete Group '{{ group.name}}'</BulmaModal>
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="users" /> Groups</h1>
Expand Down Expand Up @@ -75,7 +75,8 @@
name:"Groups",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaModal,BulmaAdminTable,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/KnownHosts.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<BulmaModal v-if="showDelete" title="Delete" action="Delete" @click="removeHost();showDelete=false" @close="showDelete=false" @cancel="showDelete=false">Are you sure you want to delete knownhostssitory '{{ knownhostsItem}}'</BulmaModal>
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon :icon="['fab','git-square']" /> Known Hosts</h1>
Expand Down Expand Up @@ -57,7 +57,8 @@
name: "AfKnownHosts",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaModal,BulmaAdminTable,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Ldap.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="id-card" /> Ldap</h1>
<div class="columns">
Expand Down Expand Up @@ -58,7 +58,8 @@
name: "AfLdap",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaCheckbox,BulmaTextArea,BulmaSettingsMenu},
setup(){
Expand Down
7 changes: 3 additions & 4 deletions client/src/views/Logs.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showDesigner ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="file-lines" /> Logs</h1>
<nav class="level">
Expand Down Expand Up @@ -49,9 +49,7 @@
</section>
</template>
<script>
import Vue from 'vue'
import axios from 'axios'
import Copy from 'copy-to-clipboard'
import LogViewer from '@femessage/log-viewer'
import BulmaCheckRadio from './../components/BulmaCheckRadio.vue'
import TokenStorage from './../lib/TokenStorage'
Expand All @@ -61,7 +59,8 @@
name: "AfLogs",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{LogViewer,BulmaCheckRadio},
data(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/MailSettings.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="cogs" /> Settings</h1>
<div class="columns">
Expand Down Expand Up @@ -54,7 +54,8 @@
name: "AfMailSettings",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaCheckbox,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/OIDC.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="address-card" /> Open ID Connect</h1>
<div class="columns">
Expand Down Expand Up @@ -48,7 +48,8 @@
name: "AfOidc",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaCheckbox,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Repos.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<BulmaQuickView class="quickview" v-if="showOutput" title="Last execution result" footer="" @close="showOutput=false">
<p class="is-family-code" v-html="output.split('\n').join('<br>')"></p>
</BulmaQuickView>
Expand Down Expand Up @@ -84,7 +84,8 @@
name:"AfRepositories",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,BulmaModal,BulmaQuickView,BulmaAdminTable,BulmaCheckbox,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Settings.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="cogs" /> Settings</h1>
<div class="columns">
Expand Down Expand Up @@ -92,7 +92,8 @@
name: "AfSettings",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaInput,VueCodeEditor,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Sshkey.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="key" /> SSH Key</h1>
<div class="columns">
Expand Down Expand Up @@ -38,7 +38,8 @@
name: "AfSshkey",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaTextArea,BulmaSettingsMenu},
setup(){
Expand Down
5 changes: 3 additions & 2 deletions client/src/views/Users.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<section v-if="isAdmin" class="section">
<section v-if="profile?.options?.showSettings ?? isAdmin" class="section">
<BulmaModal v-if="showDelete && user.username" title="Delete" action="Delete" @click="deleteUser();showDelete=false" @close="showDelete=false" @cancel="showDelete=false">Are you sure you want to delete User '{{ user.username}}'</BulmaModal>
<div class="container">
<h1 class="title has-text-info"><font-awesome-icon icon="user" /> Users</h1>
Expand Down Expand Up @@ -81,7 +81,8 @@
name: "AfUsers",
props:{
authenticated:{type:Boolean},
isAdmin:{type:Boolean}
isAdmin:{type:Boolean},
profile:{type:Object}
},
components:{BulmaButton,BulmaSelect,BulmaInput,BulmaModal,BulmaAdminTable,BulmaSettingsMenu},
setup(){
Expand Down
Loading

0 comments on commit 1b5a36b

Please sign in to comment.