Add token regeneration to admin pages

This commit is contained in:
luk3yx 2020-12-21 15:34:31 +13:00
parent 1c8430b11f
commit e2491dd812
3 changed files with 44 additions and 3 deletions

View File

@ -152,7 +152,7 @@ const serverListTemplate = adminPagesHeader + `
const currencyInput = `type="text" pattern="¤?[0-9,_]+(\.[0-9,_]+)?"`
const infoTemplate = adminPagesHeader + `
<style>
#form-inner input, #message {
#form-inner input, #form-inner label, #message {
transition: ease-in-out 200ms;
text-overflow: ellipsis;
}
@ -165,7 +165,7 @@ const infoTemplate = adminPagesHeader + `
html {
scroll-behavior: smooth;
}
#edit-btn, #edit-btn ~ .button {
#edit-btn, #edit-btn ~ .button, #edit-btn ~ button {
display: none;
transition: ease-in-out 250ms;
}
@ -175,6 +175,18 @@ const infoTemplate = adminPagesHeader + `
transform-origin: top center;
max-height: 0;
}
#regenerate-token {
margin-bottom: 2rem;
}
#regenerate-token + label {
display: inline-block;
vertical-align: middle;
user-select: none;
}
#regenerate-token[disabled="disabled"],
#regenerate-token[disabled="disabled"] + label {
opacity: 0.5;
}
{{end}}
</style>
@ -209,6 +221,12 @@ const infoTemplate = adminPagesHeader + `
disabled="disabled" name="webhookURL" />
{{if .AllowEditing}}
<br/>
<input type="checkbox" id="regenerate-token"
disabled="disabled" name="regenerateToken" />
<label for="regenerate-token">
Regenerate token
</label>
<br/>
<button type="button" id="edit-btn"
class="button-primary">Edit</button>
@ -610,6 +628,20 @@ func addAdminPages(router *httprouter.Router, db lurkcoin.Database,
)
}
if r.Form.Get("regenerateToken") == "on" {
if len(msgs) == 0 {
msgs = append(msgs, "New token: "+server.RegenerateToken())
log.Printf(
"[Admin] User %#v regenerates the token of server %#v",
adminUser,
server.Name,
)
} else {
msgs = append(msgs, "Refusing to regenerate token as other"+
" settings were changed.")
}
}
// Finish the transaction
uid := server.UID
tr.Finish()

View File

@ -35,7 +35,7 @@ import (
)
const SYMBOL = "¤"
const VERSION = "3.0.7"
const VERSION = "3.0.8"
// Note that public source code is required by the AGPL
const SOURCE_URL = "https://github.com/luk3yx/lurkcoin-core"

View File

@ -323,6 +323,15 @@ func (self *Server) GetExchangeRate(amount Currency, toLurkcoin bool) (Currency,
return CurrencyFromFloat(res), exchange
}
// Regenerates the token and returns the new one.
func (self *Server) RegenerateToken() string {
self.lock.Lock()
defer self.lock.Unlock()
self.token = GenerateToken()
self.modified = true
return self.token
}
// "Encoded" servers that have all their values public
type EncodedServer struct {
// A version number for breaking changes, because of the way gob works this