init - add project files
This commit is contained in:
58
handlers/app/account.go
Normal file
58
handlers/app/account.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"maxwarden/middleware"
|
||||
"maxwarden/users"
|
||||
|
||||
. "maxwarden/ui"
|
||||
|
||||
. "maragu.dev/gomponents"
|
||||
. "maragu.dev/gomponents/html"
|
||||
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func AccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||
identity := middleware.GetIdentity(r)
|
||||
session := middleware.GetSession(r)
|
||||
|
||||
|
||||
type accountTableItem struct {
|
||||
Property string
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
cols := []string {
|
||||
"Property",
|
||||
"Value",
|
||||
}
|
||||
|
||||
user, _ := users.FetchById(identity.UserID)
|
||||
|
||||
entries := []accountTableItem {
|
||||
{ "UserId", user.ID },
|
||||
{ "Username", user.Username },
|
||||
{ "Name", user.Firstname + " " + user.Lastname },
|
||||
{ "Email", user.Email },
|
||||
{ "Last Login", user.LastLogin },
|
||||
}
|
||||
|
||||
func() Node {
|
||||
return AppLayout("Account", *identity, session,
|
||||
AutoTableLite(
|
||||
cols,
|
||||
entries,
|
||||
func(item accountTableItem) Node {
|
||||
return Tr(
|
||||
Td(B(Text(item.Property))),
|
||||
Td(ToText(item.Value)),
|
||||
)
|
||||
},
|
||||
AutoTableOptions{
|
||||
BorderX: true,
|
||||
Shadow: true,
|
||||
},
|
||||
),
|
||||
)
|
||||
}().Render(w)
|
||||
}
|
||||
17
handlers/app/delete.go
Normal file
17
handlers/app/delete.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"maxwarden/entries"
|
||||
"maxwarden/middleware"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
identity := middleware.GetIdentity(r)
|
||||
|
||||
id := r.PathValue("id")
|
||||
|
||||
entries.DeleteSecret(identity.UserID, identity.MasterKey, id)
|
||||
|
||||
http.Redirect(w, r, "/app", http.StatusFound)
|
||||
}
|
||||
149
handlers/app/editor.go
Normal file
149
handlers/app/editor.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"maxwarden/entries"
|
||||
"maxwarden/security"
|
||||
. "maxwarden/ui"
|
||||
"maxwarden/users"
|
||||
|
||||
. "maragu.dev/gomponents"
|
||||
. "maragu.dev/gomponents/html"
|
||||
|
||||
"maxwarden/middleware"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
EDITOR_TYPE_EDIT = iota
|
||||
EDITOR_TYPE_ADD = iota
|
||||
)
|
||||
|
||||
func EditorHandler(w http.ResponseWriter, r *http.Request) {
|
||||
identity := middleware.GetIdentity(r)
|
||||
session := middleware.GetSession(r)
|
||||
|
||||
var editorType int
|
||||
var title string
|
||||
var btnLabel string
|
||||
|
||||
if r.URL.Path == "/app/editor/add" {
|
||||
editorType = EDITOR_TYPE_ADD
|
||||
title = "Add Credentials"
|
||||
btnLabel = "Add"
|
||||
} else {
|
||||
editorType = EDITOR_TYPE_EDIT
|
||||
title = "Edit Credentials"
|
||||
btnLabel = "Save"
|
||||
}
|
||||
|
||||
var secret entries.Secret
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
if editorType == EDITOR_TYPE_EDIT {
|
||||
id := r.PathValue("id")
|
||||
|
||||
secret, _ = entries.FetchSecretFromID(identity.UserID, identity.MasterKey, id)
|
||||
}
|
||||
}
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
r.ParseForm()
|
||||
|
||||
desc := r.FormValue("description")
|
||||
notes := r.FormValue("notes")
|
||||
username := r.FormValue("un")
|
||||
password := r.FormValue("pas")
|
||||
url := r.FormValue("url")
|
||||
|
||||
secret = entries.Secret{
|
||||
Description: desc,
|
||||
URL: url,
|
||||
Notes: notes,
|
||||
Password: password,
|
||||
Username: username,
|
||||
}
|
||||
|
||||
user, _ := users.FetchById(identity.UserID)
|
||||
|
||||
// Get current secret store
|
||||
secrets, _ := security.DecryptDataWithKey[[]entries.Secret](user.Data, identity.MasterKey)
|
||||
|
||||
if secrets == nil {
|
||||
http.Redirect(w, r, "/app", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
if editorType == EDITOR_TYPE_ADD {
|
||||
secret.ID = security.RandBase58String(32)
|
||||
*secrets = append(*secrets, secret)
|
||||
} else {
|
||||
secret.ID = r.PathValue("id")
|
||||
|
||||
// linear search and replace
|
||||
for i, v := range *secrets {
|
||||
if v.ID == secret.ID {
|
||||
(*secrets)[i] = secret
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize and encrypt modified store using master key
|
||||
enc, _ := security.EncryptDataWithKey(secrets, identity.MasterKey)
|
||||
|
||||
user.Data = enc
|
||||
|
||||
users.Update(user)
|
||||
|
||||
http.Redirect(w, r, "/app", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
AppLayout(title, *identity, session,
|
||||
If(editorType == EDITOR_TYPE_EDIT,
|
||||
Group{
|
||||
Modal(
|
||||
"warning_popup",
|
||||
Text("Warning!"),
|
||||
Text("Are you sure you want to delete this entry? This action cannot be undone."),
|
||||
[]Node{
|
||||
A(Href("/app/delete/" + secret.ID), ButtonUIDanger(Text("Delete"))),
|
||||
ButtonUIOutline(ModalCloser(), Text("Close")),
|
||||
},
|
||||
),
|
||||
Div(
|
||||
InlineStyle("$me { display: flex; flex-direction: row-reverse; align-items: center; }"),
|
||||
ModalActuator("warning_popup", ButtonUIDanger(Text("Delete"))),
|
||||
),
|
||||
},
|
||||
),
|
||||
Form(
|
||||
AutoComplete("off"),
|
||||
Method("POST"),
|
||||
FormLabel(Text("Description")),
|
||||
FormInput(Type("text"), Name("description"), Value(secret.Description)),
|
||||
Br(),
|
||||
|
||||
FormLabel(Text("Username")),
|
||||
FormInput(Type("text"), Name("un"), Value(secret.Username)),
|
||||
Br(),
|
||||
|
||||
FormLabel(Text("Password")),
|
||||
FormInput(Type("password"), Name("pas"), Value(secret.Password)),
|
||||
Br(),
|
||||
|
||||
FormLabel(Text("URL")),
|
||||
FormInput(Type("text"), Name("url"), Value(secret.URL)),
|
||||
Br(),
|
||||
|
||||
FormLabel(Text("Additional Notes")),
|
||||
FormTextarea(InlineStyle("$me { height: $32; font-family: var(--font-mono); }"), Name("notes"), Text(secret.Notes)),
|
||||
Br(),
|
||||
|
||||
Div(
|
||||
InlineStyle("$me { display: flex; flex-direction: row; align-items: center; gap: $4; }"),
|
||||
ButtonUISuccess(Text(btnLabel), Type("submit")),
|
||||
A(Href("/app"), ButtonUIOutline(Text("Close"), Type("button"))),
|
||||
),
|
||||
),
|
||||
).Render(w)
|
||||
}
|
||||
32
handlers/app/vault.go
Normal file
32
handlers/app/vault.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
. "maxwarden/ui"
|
||||
|
||||
. "maragu.dev/gomponents"
|
||||
. "maragu.dev/gomponents/html"
|
||||
|
||||
"maxwarden/middleware"
|
||||
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func VaultHandler(w http.ResponseWriter, r *http.Request) {
|
||||
identity := middleware.GetIdentity(r)
|
||||
session := middleware.GetSession(r)
|
||||
|
||||
AppLayout("Credential Vault", *identity, session,
|
||||
Div(
|
||||
InlineStyle(`
|
||||
$me {
|
||||
display: flex;
|
||||
flex-direction: row-reverse;
|
||||
align-items: center;
|
||||
margin-bottom: $5;
|
||||
}
|
||||
`),
|
||||
A(Href("/app/editor/add"), ButtonUI(Text("+ Add Item"))),
|
||||
),
|
||||
HxLoad("/app/vault-hx"),
|
||||
).Render(w)
|
||||
}
|
||||
91
handlers/app/vault_hx.go
Normal file
91
handlers/app/vault_hx.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
// "maxwarden/.jet/table"
|
||||
|
||||
"maxwarden/security"
|
||||
. "maxwarden/ui"
|
||||
|
||||
. "maragu.dev/gomponents"
|
||||
. "maragu.dev/gomponents/html"
|
||||
|
||||
"maxwarden/database"
|
||||
"maxwarden/entries"
|
||||
|
||||
"maxwarden/middleware"
|
||||
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func VaultHxHandler(w http.ResponseWriter, r *http.Request) {
|
||||
identity := middleware.GetIdentity(r)
|
||||
|
||||
filter := database.ParseFilterFromRequest(r)
|
||||
filter.Pagination.Enabled = true
|
||||
|
||||
entryFilter := entries.EntryFilter{
|
||||
Filter: filter,
|
||||
UserId: identity.UserID,
|
||||
MasterKey: identity.MasterKey,
|
||||
}
|
||||
|
||||
// fetch entities from filter function
|
||||
// this first counts the possible items before pagination
|
||||
searchFilter := entries.EntryFilter{
|
||||
Filter: database.NewFilterFromSearch(filter.Search),
|
||||
UserId: identity.UserID,
|
||||
MasterKey: identity.MasterKey,
|
||||
}
|
||||
|
||||
searchItems, _ := entries.Filter(searchFilter)
|
||||
|
||||
// this query gets the data AFTER pagination
|
||||
entryList, _ := entries.Filter(entryFilter)
|
||||
|
||||
// generate page numbers according to total length of data
|
||||
entryFilter.Filter.Pagination.GeneratePagination(len(searchItems), len(entryList))
|
||||
|
||||
// Col header names and referenced database col names
|
||||
cols := []database.ColInfo{
|
||||
{DbName: "Description", DisplayName: "Description", Sortable: true},
|
||||
{DisplayName: "Username"},
|
||||
{DisplayName: "Password"},
|
||||
{DisplayName: "URL"},
|
||||
{DisplayName: "Action"},
|
||||
}
|
||||
|
||||
// Generate HTML
|
||||
elId := "order_table"
|
||||
AutoTable(
|
||||
elId,
|
||||
r.URL.Path,
|
||||
cols,
|
||||
entryFilter.Filter,
|
||||
entryList,
|
||||
AutotableSearchGroup(
|
||||
AutotableSearch(
|
||||
Placeholder("Search Description..."),
|
||||
BindSearch(elId, "description"),
|
||||
AutoFocus(),
|
||||
),
|
||||
),
|
||||
func(entry entries.Secret) Node {
|
||||
return Tr(
|
||||
TdLeft(Text(entry.Description)),
|
||||
TdLeft(Text(entry.Username)),
|
||||
TdLeft(Text("********")),
|
||||
TdLeft(PageLink(security.SanitizationPolicy.Sanitize(entry.URL), Text(entry.URL), false)),
|
||||
TdCenter(A(Href("/app/editor/edit/" + entry.ID), ButtonUIOutline(Icon(ICON_PENCIL, 16)))),
|
||||
)
|
||||
},
|
||||
nil,
|
||||
AutoTableOptions{
|
||||
Compact: false,
|
||||
Shadow: true,
|
||||
Hover: false,
|
||||
Alternate: false,
|
||||
BorderX: true,
|
||||
BorderY: false,
|
||||
},
|
||||
).Render(w)
|
||||
}
|
||||
Reference in New Issue
Block a user