Compare commits

...

3 Commits

Author SHA1 Message Date
4bf48b9538 Table filter WIP 2024-06-15 00:12:26 +05:30
2121f86a33 switched to session storage 2024-06-15 00:12:19 +05:30
1377d63347 validate token on refresh
Also added validatedtoken API
2024-06-15 00:12:02 +05:30
5 changed files with 112 additions and 7 deletions

View File

@ -28,7 +28,14 @@ import ListSecrets from "./components/ListSecrets.vue";
</el-dialog> </el-dialog>
<div class="container"> <div class="container">
<HomePage msg="You did it!" @loggedin="loggedin = true" v-if="!loggedin" /> <HomePage
msg="You did it!"
@loggedin="
loggedin = true;
showSecrets = true;
"
v-if="!loggedin"
/>
<el-button @click="showSecrets = true" v-if="loggedin"> Show secrets </el-button> <el-button @click="showSecrets = true" v-if="loggedin"> Show secrets </el-button>
<el-button @click="showSecrets = false" v-if="showSecrets && loggedin"> <el-button @click="showSecrets = false" v-if="showSecrets && loggedin">
Hide secrets Hide secrets
@ -46,11 +53,12 @@ export default {
showSecrets: false, showSecrets: false,
creationDialog: false, creationDialog: false,
listUpdated: 1, listUpdated: 1,
apiBaseUrl: "http://localhost:8000",
}; };
}, },
methods: { methods: {
logout() { logout() {
localStorage.removeItem("token"); sessionStorage.removeItem("token");
this.loggedin = false; this.loggedin = false;
}, },
@ -60,6 +68,47 @@ export default {
this.listUpdated += 1; this.listUpdated += 1;
console.log("after update", this.listUpdated); console.log("after update", this.listUpdated);
}, },
async validateToken() {
const url = `${this.apiBaseUrl}/validate-token`;
const token = sessionStorage.getItem("token");
const requestOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
};
const response = await fetch(url, requestOptions)
.then((response) => response.json())
.catch((err) => {
console.log(err);
return false;
});
if (!response) {
return false;
}
if ("message" in response) {
if (response["message"] === "authenticated") {
console.log("token validated");
return true;
}
}
return false;
},
},
async mounted() {
if ("token" in sessionStorage) {
const tokenValid = await this.validateToken();
if (tokenValid) {
this.loggedin = true;
this.showSecrets = true;
}
}
}, },
}; };
</script> </script>

View File

@ -46,7 +46,7 @@ export default {
methods: { methods: {
async createSecret() { async createSecret() {
const url = `${this.apiBaseUrl}/secret`; const url = `${this.apiBaseUrl}/secret`;
const token = localStorage.getItem("token"); const token = sessionStorage.getItem("token");
const requestOptions = { const requestOptions = {
method: "POST", method: "POST",
headers: { headers: {

View File

@ -49,7 +49,7 @@ export default {
if ("message" in response) { if ("message" in response) {
if (response.message === "authenticated") { if (response.message === "authenticated") {
const token = response.accessToken; const token = response.accessToken;
localStorage.setItem("token", token); sessionStorage.setItem("token", token);
this.$emit("loggedin", true); this.$emit("loggedin", true);
} }
} }

View File

@ -1,6 +1,20 @@
<template> <template>
<div> <div>
<el-table :data="secretsList" style="width: 100vw"> <div>
<br />
<el-button-group>
<el-button
type="success"
v-for="band in filterBands"
:key="band"
@click="filterTable(band)"
>
{{ band }}
</el-button>
<el-button type="success">Clear</el-button>
</el-button-group>
</div>
<el-table :data="filteredSecretsList" style="width: 100vw">
<el-table-column type="expand"> <el-table-column type="expand">
<template #default="props"> <template #default="props">
<div style="margin-left: 3rem"> <div style="margin-left: 3rem">
@ -39,13 +53,23 @@ export default {
message: "Hello List Secret", message: "Hello List Secret",
apiBaseUrl: "http://localhost:8000", apiBaseUrl: "http://localhost:8000",
secretsList: [], secretsList: [],
filteredSecretsList: [],
filterBands: ["A-C", "D-I", "J-O", "P-S", "T-Z"],
filterBandsVals: {
"A-C": ["A", "B", "C"],
"D-I": ["D", "E", "F", "G", "H", "I"],
"J-O": ["J", "K", "L", "M", "N", "O"],
"P-S": ["P", "Q", "R", "S"],
"T-Z": ["T", "U", "V", "W", "X", "Y", "Z"],
},
currentFilter: [],
}; };
}, },
methods: { methods: {
async listSecrets() { async listSecrets() {
const url = `${this.apiBaseUrl}/secret`; const url = `${this.apiBaseUrl}/secret`;
const token = localStorage.getItem("token"); const token = sessionStorage.getItem("token");
const requestOptions = { const requestOptions = {
method: "GET", method: "GET",
headers: { headers: {
@ -85,6 +109,28 @@ export default {
handleClick(scope) { handleClick(scope) {
console.log(scope); console.log(scope);
}, },
filterTable(band) {
const letters = this.filterBandsVals[band];
this.currentFilter = letters;
console.log(letters);
},
checkFirstLetter(row) {
const firstLetter = row.issuer[0].toUpperCase();
if (this.currentFilter.indexOf(firstLetter) > 0) {
return true;
}
return false;
},
},
watch: {
currentFilter: function (newFilter, oldFilter) {
// console.log("filter", newFilter, oldFilter);
const filteredList = this.secretsList.filter(this.checkFirstLetter);
this.filteredSecretsList = filteredList;
},
}, },
mounted() { mounted() {

10
main.py
View File

@ -168,3 +168,13 @@ async def list_secret(current_user: dict = Depends(get_current_user)):
secret['data'] = decrypted_data secret['data'] = decrypted_data
return user_secrets return user_secrets
@app.get('/validate-token')
async def validate_token(current_user: dict = Depends(get_current_user)):
user_id = current_user['id']
print("user_id: ", user_id)
if user_id is not None:
return {'message': 'authenticated'}
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)