encode registration key on command line

This commit is contained in:
Iris Lightshard 2023-07-10 00:38:08 -06:00
parent 952f80dbc2
commit b7888158b8
4 changed files with 27 additions and 9 deletions

View file

@ -1,16 +1,28 @@
package cmd
import (
_ "fmt"
"fmt"
"hacklab.nilfm.cc/felt/register"
"hacklab.nilfm.cc/quartzgun/auth"
_ "strings"
"strconv"
"time"
)
func ProcessCmd(args []string, userStore auth.UserStore) bool {
func ProcessCmd(args []string, userStore auth.UserStore, crypto register.SymmetricCrypto) bool {
if len(args) == 1 {
return false
}
switch args[1] {
case "register":
now := time.Now().UnixMicro()
strNow := strconv.FormatInt(now, 10)
self, err := crypto.Encrypt(strNow)
if err == nil {
fmt.Print("This is the registration key: ")
fmt.Println(self)
} else {
fmt.Printf("%v\n", err)
}
case "adduser":
if len(args) < 4 {
return help()

View file

@ -174,6 +174,8 @@ func parseConfig(configFile string) *Config {
k := strings.TrimSpace(kvp[0])
v := strings.TrimSpace(kvp[1])
switch k {
case "registrationSecret":
cfg.RegistrationSecret = v
case "mongoURI":
cfg.MongoURI = v
case "uploads":

View file

@ -6,6 +6,7 @@ import (
"hacklab.nilfm.cc/felt/config"
"hacklab.nilfm.cc/felt/gametable"
"hacklab.nilfm.cc/felt/mongodb"
"hacklab.nilfm.cc/felt/register"
"hacklab.nilfm.cc/quartzgun/indentalUserDB"
"log"
"net"
@ -33,7 +34,8 @@ func run() error {
udb := indentalUserDB.CreateIndentalUserDB(
filepath.Join(config.GetConfigLocation(), "user.db"))
if cmd.ProcessCmd(os.Args, udb) {
crypto := &register.SymmetricCrypt{Secret: cfg.RegistrationSecret}
if cmd.ProcessCmd(os.Args, udb, crypto) {
os.Exit(0)
}

View file

@ -4,7 +4,7 @@ import (
"context"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"html/template"
"net/http"
@ -13,7 +13,7 @@ import (
"hacklab.nilfm.cc/quartzgun/router"
)
var bytes = []byte{99, 207, 33, 57, 28, 01, 50, 76, 01}
var bytes = []byte{99, 207, 33, 57, 28, 01, 50, 76, 01, 92, 33, 10, 48, 07, 00, 250}
type SymmetricCrypto interface {
Encode(b []byte) string
@ -27,11 +27,11 @@ type SymmetricCrypt struct {
}
func (self *SymmetricCrypt) Encode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
return hex.EncodeToString(b)
}
func (self *SymmetricCrypt) Decode(s string) []byte {
data, err := base64.StdEncoding.DecodeString(s)
data, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
@ -64,7 +64,9 @@ func (self *SymmetricCrypt) Decrypt(text string) (string, error) {
func WithCrypto(next http.Handler, crypto SymmetricCrypto) http.Handler {
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
*req = *req.WithContext(context.WithValue(req.Context(), "crypto", crypto))
//urlParams := req.Context().Value("params").(map[string]string)
//cipher := urlParams["cipher"]
next.ServeHTTP(w, req)
}