mirror of https://github.com/velour/catbase.git
admin: use htmx and templ for app pass
This commit is contained in:
parent
b8e6e0595d
commit
f6b1712eda
|
@ -5,8 +5,8 @@ templ (b *bot) index() {
|
|||
<html lang="en">
|
||||
<head>
|
||||
<!-- Load required Bootstrap and BootstrapVue CSS -->
|
||||
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
|
||||
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.css" />
|
||||
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta charset="UTF-8" />
|
||||
<title>catbase</title>
|
||||
</head>
|
||||
|
@ -16,6 +16,7 @@ templ (b *bot) index() {
|
|||
@b.Nav("", b.GetWebNavigation())
|
||||
</div>
|
||||
|
||||
<script src="//unpkg.com/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
}
|
|
@ -23,7 +23,7 @@ func (b *bot) index() templ.Component {
|
|||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html /><html lang=\"en\"><head><!-- Load required Bootstrap and BootstrapVue CSS --><link type=\"text/css\" rel=\"stylesheet\" href=\"//unpkg.com/bootstrap/dist/css/bootstrap.min.css\"><link type=\"text/css\" rel=\"stylesheet\" href=\"//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.css\"><meta charset=\"UTF-8\"><title>catbase</title></head><body><div id=\"app\">")
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html /><html lang=\"en\"><head><!-- Load required Bootstrap and BootstrapVue CSS --><link type=\"text/css\" rel=\"stylesheet\" href=\"//unpkg.com/bootstrap/dist/css/bootstrap.min.css\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta charset=\"UTF-8\"><title>catbase</title></head><body><div id=\"app\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ func (b *bot) index() templ.Component {
|
|||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div></body></html>")
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div><script src=\"//unpkg.com/bootstrap/dist/js/bootstrap.bundle.min.js\"></script></body></html>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
package admin
|
||||
|
||||
import "fmt"
|
||||
|
||||
templ (a *AdminPlugin) page() {
|
||||
<!DOCTYPE html />
|
||||
<html lang="en">
|
||||
<head>
|
||||
<!-- Load required Bootstrap and BootstrapVue CSS -->
|
||||
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Vars</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div hx-get="/navHTML/Variables" hx-trigger="load" hx-swap="outerHTML"></div>
|
||||
|
||||
<div class="container">
|
||||
<form>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<label for="password">Password:
|
||||
<input type="text" name="password"></input>
|
||||
</label>
|
||||
</div>
|
||||
<div class="col">
|
||||
<label for="secret">Secret:
|
||||
<input type="text" name="secret"></input>
|
||||
</label>
|
||||
</div>
|
||||
<div class="col">
|
||||
<button hx-post="/apppass/api" hx-target="#data" class="btn btn-primary">List</button>
|
||||
<button hx-put="/apppass/api" hx-target="#data" class="btn btn-secondary">New</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="row">
|
||||
<div id="data"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="//unpkg.com/htmx.org@1.9.10" integrity="sha384-D1Kt99CQMDuVetoL1lrYwg5t+9QdHe7NLX/SoJYkXDFfX37iInKRy5xLSi8nO7UC" crossorigin="anonymous"></script>
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
|
||||
templ (a *AdminPlugin) showPassword(entry PassEntry) {
|
||||
<div><span>ID</span><span>{ fmt.Sprintf("%d", entry.ID) }</span></div>
|
||||
<div><span>Password</span><span>{ entry.Secret }:{ entry.Pass }</span></div>
|
||||
}
|
||||
|
||||
templ (a *AdminPlugin) entries(items []PassEntry) {
|
||||
<div>
|
||||
if len(items) == 0 {
|
||||
<span>No items</span>
|
||||
}
|
||||
<ul>
|
||||
for _, entry := range items {
|
||||
<li>
|
||||
<button href="#"
|
||||
class="btn btn-danger"
|
||||
hx-delete="/apppass/api"
|
||||
hx-confirm={ fmt.Sprintf("Are you sure you want to delete %d?", entry.ID) }
|
||||
hx-target="#data"
|
||||
hx-include="this,[name='password'],[name='secret']"
|
||||
name="id" value={ fmt.Sprintf("%d", entry.ID) }>X</button>
|
||||
{ fmt.Sprintf("%d", entry.ID) }
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ renderError(err error) {
|
||||
<div>{ err.Error() }</div>
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.2.543
|
||||
package admin
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import "context"
|
||||
import "io"
|
||||
import "bytes"
|
||||
|
||||
import "fmt"
|
||||
|
||||
func (a *AdminPlugin) page() templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
templ_7745c5c3_Buffer = templ.GetBuffer()
|
||||
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html /><html lang=\"en\"><head><!-- Load required Bootstrap and BootstrapVue CSS --><link type=\"text/css\" rel=\"stylesheet\" href=\"//unpkg.com/bootstrap/dist/css/bootstrap.min.css\"><meta charset=\"UTF-8\"><title>Vars</title></head><body><div hx-get=\"/navHTML/Variables\" hx-trigger=\"load\" hx-swap=\"outerHTML\"></div><div class=\"container\"><form><div class=\"row\"><div class=\"col-auto\"><label for=\"password\">Password: <input type=\"text\" name=\"password\"></label></div><div class=\"col-auto\"><label for=\"secret\">Secret: <input type=\"text\" name=\"secret\"></label></div><div class=\"col-auto\"><button hx-post=\"/apppass/api\" hx-target=\"#data\" class=\"btn btn-primary\">List</button> <button hx-put=\"/apppass/api\" hx-target=\"#data\" class=\"btn btn-secondary\">New</button></div></div></form><div class=\"row\"><div id=\"data\"></div></div></div><script src=\"//unpkg.com/htmx.org@1.9.10\" integrity=\"sha384-D1Kt99CQMDuVetoL1lrYwg5t+9QdHe7NLX/SoJYkXDFfX37iInKRy5xLSi8nO7UC\" crossorigin=\"anonymous\"></script></body></html>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
|
||||
}
|
||||
return templ_7745c5c3_Err
|
||||
})
|
||||
}
|
||||
|
||||
func (a *AdminPlugin) showPassword(entry PassEntry) templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
templ_7745c5c3_Buffer = templ.GetBuffer()
|
||||
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var2 == nil {
|
||||
templ_7745c5c3_Var2 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div><span>ID</span><span>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", entry.ID))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/admin/apppass.templ`, Line: 48, Col: 59}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span></div><div><span>Password</span><span>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var4 string
|
||||
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(entry.Secret)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/admin/apppass.templ`, Line: 49, Col: 50}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(":")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var5 string
|
||||
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(entry.Pass)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/admin/apppass.templ`, Line: 49, Col: 65}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
|
||||
}
|
||||
return templ_7745c5c3_Err
|
||||
})
|
||||
}
|
||||
|
||||
func (a *AdminPlugin) entries(items []PassEntry) templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
templ_7745c5c3_Buffer = templ.GetBuffer()
|
||||
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var6 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var6 == nil {
|
||||
templ_7745c5c3_Var6 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<span>No items</span>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<ul>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
for _, entry := range items {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<li><button href=\"#\" class=\"btn btn-danger\" hx-delete=\"/apppass/api\" hx-confirm=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(fmt.Sprintf("Are you sure you want to delete %d?", entry.ID)))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" hx-target=\"#data\" hx-include=\"this,[name='password'],[name='secret']\" name=\"id\" value=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(fmt.Sprintf("%d", entry.ID)))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">X</button> ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var7 string
|
||||
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", entry.ID))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/admin/apppass.templ`, Line: 67, Col: 57}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</li>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</ul></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
|
||||
}
|
||||
return templ_7745c5c3_Err
|
||||
})
|
||||
}
|
||||
|
||||
func renderError(err error) templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
templ_7745c5c3_Buffer = templ.GetBuffer()
|
||||
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var8 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var8 == nil {
|
||||
templ_7745c5c3_Var8 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var9 string
|
||||
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(err.Error())
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `plugins/admin/apppass.templ`, Line: 75, Col: 22}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
|
||||
}
|
||||
return templ_7745c5c3_Err
|
||||
})
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>vars</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
|
||||
<script src="https://unpkg.com/htmx.org@1.9.4"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div hx-get="/navHTML/Variables" hx-trigger="load" hx-swap="outerHTML"></div>
|
||||
|
||||
<div class="container">
|
||||
<table class="table-responsive table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Key</th>
|
||||
<th>Value</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Items}}
|
||||
<tr>
|
||||
<td>{{.Key}}</td><td>{{.Value}}</td>
|
||||
</tr>
|
||||
{{else}}
|
||||
<tr>
|
||||
<td colspan="2">No data</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
|
||||
</body>
|
||||
</html>
|
|
@ -1,18 +1,20 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
//go:embed *.html
|
||||
|
@ -20,7 +22,6 @@ var embeddedFS embed.FS
|
|||
|
||||
func (p *AdminPlugin) registerWeb() {
|
||||
r := chi.NewRouter()
|
||||
r.HandleFunc("/api", p.handleVarsAPI)
|
||||
r.HandleFunc("/", p.handleVars)
|
||||
p.bot.RegisterWebName(r, "/vars", "Variables")
|
||||
r = chi.NewRouter()
|
||||
|
@ -31,8 +32,7 @@ func (p *AdminPlugin) registerWeb() {
|
|||
}
|
||||
|
||||
func (p *AdminPlugin) handleAppPass(w http.ResponseWriter, r *http.Request) {
|
||||
index, _ := embeddedFS.ReadFile("apppass.html")
|
||||
w.Write(index)
|
||||
p.page().Render(r.Context(), w)
|
||||
}
|
||||
|
||||
type PassEntry struct {
|
||||
|
@ -76,26 +76,41 @@ func (p *AdminPlugin) handleAppPassCheck(w http.ResponseWriter, r *http.Request)
|
|||
}
|
||||
|
||||
func (p *AdminPlugin) handleAppPassAPI(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
query, _ := url.ParseQuery(string(b))
|
||||
secret := r.FormValue("secret")
|
||||
password := r.FormValue("password")
|
||||
id, _ := strconv.ParseInt(r.FormValue("id"), 10, 64)
|
||||
if !r.Form.Has("secret") && query.Has("secret") {
|
||||
secret = query.Get("secret")
|
||||
}
|
||||
if !r.Form.Has("password") && query.Has("password") {
|
||||
password = query.Get("password")
|
||||
}
|
||||
if !r.Form.Has("id") && query.Has("id") {
|
||||
id, _ = strconv.ParseInt(query.Get("id"), 10, 64)
|
||||
}
|
||||
req := struct {
|
||||
Password string `json:"password"`
|
||||
PassEntry PassEntry `json:"passEntry"`
|
||||
}{}
|
||||
body, _ := ioutil.ReadAll(r.Body)
|
||||
_ = json.Unmarshal(body, &req)
|
||||
}{
|
||||
password,
|
||||
PassEntry{
|
||||
ID: id,
|
||||
Secret: secret,
|
||||
},
|
||||
}
|
||||
if req.PassEntry.Secret == "" {
|
||||
writeErr(w, fmt.Errorf("missing secret"))
|
||||
writeErr(r.Context(), w, fmt.Errorf("missing secret"))
|
||||
return
|
||||
}
|
||||
if req.Password == "" || !p.bot.CheckPassword(req.PassEntry.Secret, req.Password) {
|
||||
writeErr(w, fmt.Errorf("missing or incorrect password"))
|
||||
writeErr(r.Context(), w, fmt.Errorf("missing or incorrect password"))
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodPut:
|
||||
if req.PassEntry.Secret == "" {
|
||||
writeErr(w, fmt.Errorf("missing secret"))
|
||||
return
|
||||
}
|
||||
if string(req.PassEntry.Pass) == "" {
|
||||
c := 10
|
||||
b := make([]byte, c)
|
||||
|
@ -120,27 +135,27 @@ func (p *AdminPlugin) handleAppPassAPI(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
res, err := p.db.Exec(q, req.PassEntry.Secret, req.PassEntry.encodedPass, req.PassEntry.Cost)
|
||||
if err != nil {
|
||||
writeErr(w, err)
|
||||
writeErr(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
writeErr(w, err)
|
||||
writeErr(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
req.PassEntry.ID = id
|
||||
j, _ := json.Marshal(req.PassEntry)
|
||||
fmt.Fprint(w, string(j))
|
||||
p.showPassword(req.PassEntry).Render(r.Context(), w)
|
||||
return
|
||||
case http.MethodDelete:
|
||||
|
||||
if req.PassEntry.ID <= 0 {
|
||||
writeErr(w, fmt.Errorf("missing ID"))
|
||||
writeErr(r.Context(), w, fmt.Errorf("missing ID"))
|
||||
return
|
||||
}
|
||||
q := `delete from apppass where id = ?`
|
||||
_, err := p.db.Exec(q, req.PassEntry.ID)
|
||||
if err != nil {
|
||||
writeErr(w, err)
|
||||
writeErr(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -148,22 +163,15 @@ func (p *AdminPlugin) handleAppPassAPI(w http.ResponseWriter, r *http.Request) {
|
|||
passEntries := []PassEntry{}
|
||||
err := p.db.Select(&passEntries, q, req.PassEntry.Secret)
|
||||
if err != nil {
|
||||
writeErr(w, err)
|
||||
writeErr(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
j, _ := json.Marshal(passEntries)
|
||||
_, _ = fmt.Fprint(w, string(j))
|
||||
p.entries(passEntries).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func writeErr(w http.ResponseWriter, err error) {
|
||||
func writeErr(ctx context.Context, w http.ResponseWriter, err error) {
|
||||
log.Error().Err(err).Msg("apppass error")
|
||||
j, _ := json.Marshal(struct {
|
||||
Err string `json:"err"`
|
||||
}{
|
||||
err.Error(),
|
||||
})
|
||||
w.WriteHeader(400)
|
||||
fmt.Fprint(w, string(j))
|
||||
renderError(err).Render(ctx, w)
|
||||
}
|
||||
|
||||
type configEntry struct {
|
||||
|
@ -186,29 +194,3 @@ func (p *AdminPlugin) handleVars(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
vars(configEntries).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func (p *AdminPlugin) handleVarsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
var configEntries []struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
q := `select key, value from config`
|
||||
err := p.db.Select(&configEntries, q)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Msg("Error getting config entries.")
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprint(w, err)
|
||||
return
|
||||
}
|
||||
for i, e := range configEntries {
|
||||
if strings.Contains(e.Value, ";;") {
|
||||
e.Value = strings.ReplaceAll(e.Value, ";;", ", ")
|
||||
e.Value = fmt.Sprintf("[%s]", e.Value)
|
||||
configEntries[i] = e
|
||||
}
|
||||
}
|
||||
j, _ := json.Marshal(configEntries)
|
||||
fmt.Fprintf(w, "%s", j)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue