FastAuth/frontend/src/components/ListSecrets.vue

235 lines
5.4 KiB
Vue
Raw Normal View History

2024-06-11 18:15:29 +00:00
<template>
<div>
2024-06-14 18:42:26 +00:00
<div>
<br />
2024-06-15 15:49:04 +00:00
<div class="filter-buttons">
<el-button-group>
<el-button
type="success"
v-for="band in filterBands"
:key="band"
@click="filterTable(band)"
>
{{ band }}
2024-06-13 17:54:48 +00:00
</el-button>
2024-06-15 15:49:04 +00:00
<el-button type="success" @click="clearFilter" v-if="showClear"
>Clear</el-button
>
</el-button-group>
<div>
<el-form-item label="Show Secrets" style="float: right">
<el-switch v-model="showSecrets" />
</el-form-item>
</div>
<div class="sort-options">
<span>
<el-form-item label="Sort">
<el-select
v-model="currentSort"
filterable
placeholder="Select"
style="width: 80px"
@change="sortItems"
>
<el-option
v-for="item in sortOptions"
:key="item.key"
:label="item.name"
:value="item.key"
/>
</el-select>
</el-form-item>
</span>
</div>
2024-06-15 15:49:04 +00:00
</div>
</div>
<div id="cards" v-if="loadCards">
<span v-for="secret in filteredSecretsList" :key="secret.id">
<SecretCard
:issuer="secret.issuer"
:username="secret.username"
:secret="secret.secret"
:id="secret.id"
:hideOtps="showSecrets"
:key="showSecrets"
2024-06-15 15:49:04 +00:00
@edit="editSecret"
/>
</span>
</div>
</div>
2024-06-11 18:15:29 +00:00
</template>
<script>
import { TOTP } from "totp-generator";
2024-06-15 15:49:04 +00:00
import SecretCard from "./SecretCard.vue";
2024-06-11 18:15:29 +00:00
export default {
2024-06-15 15:49:04 +00:00
components: { SecretCard },
2024-06-11 18:15:29 +00:00
data() {
return {
message: "Hello List Secret",
apiBaseUrl: "http://localhost:8000",
secretsList: [],
2024-06-14 18:42:26 +00:00
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: [],
2024-06-15 15:49:04 +00:00
loadCards: false,
showClear: false,
showSecrets: false,
currentSort: "none",
sortOptions: [
{
name: "None",
key: "none",
},
{
name: "A-Z",
key: "asc",
},
{
name: "Z-A",
key: "desc",
},
],
2024-06-11 18:15:29 +00:00
};
},
methods: {
async listSecrets() {
const url = `${this.apiBaseUrl}/secret`;
2024-06-14 18:42:26 +00:00
const token = sessionStorage.getItem("token");
const requestOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
};
console.log(requestOptions);
const response = await fetch(url, requestOptions)
.then((response) => response.json())
.then((data) => {
2024-06-15 15:49:04 +00:00
// console.log(data);
return data;
})
.catch((err) => console.log(err));
response.forEach((element) => {
2024-06-13 17:54:48 +00:00
// console.log(element);
const row = this.parseSecret(element.data);
2024-06-13 17:54:48 +00:00
row["id"] = element["id"];
this.secretsList.push(row);
});
2024-06-15 15:49:04 +00:00
this.filteredSecretsList = this.secretsList;
this.loadCards = true;
},
parseSecret(gibberish) {
const jsonString = atob(gibberish);
const secret = JSON.parse(jsonString);
return secret;
},
generateTotp(secret) {
const { otp, expires } = TOTP.generate(secret);
console.log(expires);
return otp;
},
2024-06-13 17:54:48 +00:00
2024-06-15 15:49:04 +00:00
editSecret(id) {
2024-06-15 18:12:12 +00:00
const editingSecret = this.secretsList.filter((element) => {
return element.id === id;
});
// console.log(editingSecret);
this.$emit("edit", editingSecret[0]);
2024-06-13 17:54:48 +00:00
},
2024-06-14 18:42:26 +00:00
filterTable(band) {
const letters = this.filterBandsVals[band];
this.currentFilter = letters;
2024-06-15 15:49:04 +00:00
this.showClear = true;
2024-06-14 18:42:26 +00:00
},
checkFirstLetter(row) {
const firstLetter = row.issuer[0].toUpperCase();
2024-06-15 15:49:04 +00:00
const index = this.currentFilter.indexOf(firstLetter);
if (index >= 0) {
2024-06-14 18:42:26 +00:00
return true;
}
return false;
},
2024-06-15 15:49:04 +00:00
clearFilter() {
this.filteredSecretsList = this.secretsList;
this.showClear = false;
},
sortItems() {
console.log(this.currentSort);
if (this.currentSort === "asc") {
this.filteredSecretsList.sort((a, b) => a.issuer.localeCompare(b.issuer));
} else if (this.currentSort === "desc") {
this.filteredSecretsList.sort((a, b) => b.issuer.localeCompare(a.issuer));
}
},
2024-06-14 18:42:26 +00:00
},
2024-06-15 15:49:04 +00:00
computed: {},
2024-06-14 18:42:26 +00:00
watch: {
2024-06-15 15:49:04 +00:00
currentFilter: function () {
2024-06-14 18:42:26 +00:00
const filteredList = this.secretsList.filter(this.checkFirstLetter);
this.filteredSecretsList = filteredList;
},
},
mounted() {
this.listSecrets();
},
2024-06-11 18:15:29 +00:00
};
</script>
2024-06-15 15:49:04 +00:00
<style>
#cards {
display: flex;
flex-direction: row;
flex-wrap: wrap;
max-width: 90vw;
}
.filter-buttons {
width: 90%;
display: flex;
margin-bottom: 20px;
justify-content: space-between;
flex-wrap: wrap;
}
.sort-options {
display: flex;
justify-content: space-between;
min-width: 80px;
}
/*
#sort {
border: none;
border-bottom: 1px solid green;
margin: 0 4px 0 4px;
font-size: 1rem;
text-align: center;
2024-06-15 15:49:04 +00:00
}
#sort:focus {
outline: none;
} */
2024-06-15 15:49:04 +00:00
</style>