From cc74165659377ebef96443030f33fbeafb9774ff Mon Sep 17 00:00:00 2001 From: Zeni Kim Date: Tue, 15 Oct 2024 22:00:46 -0500 Subject: [PATCH] add cookie secret as env, fix components --- cookies.go | 49 +++++++++++++++++++++----- template/components/content_table.html | 2 +- template/components/form_textarea.go | 6 ++-- template/components/form_textarea.html | 2 +- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/cookies.go b/cookies.go index 87503e7..8c03c10 100644 --- a/cookies.go +++ b/cookies.go @@ -5,10 +5,9 @@ package core import ( + "bytes" "crypto/aes" "crypto/cipher" - - "bytes" "crypto/rand" "encoding/base64" "encoding/gob" @@ -17,6 +16,8 @@ import ( "fmt" "io" "net/http" + "os" + "strconv" "strings" ) @@ -39,9 +40,24 @@ func GetCookie(r *http.Request) (UserCookie, error) { // Create a new instance of a User type. var user UserCookie - secretcookie, err = hex.DecodeString("13d6b4dff8f84a10851021ec8608f814570d562c92fe6b5ec4c9f595bcb3234b") - if err != nil { - return user, err + // check if template engine is enable + TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE") + if TemplateEnableStr == "" { + TemplateEnableStr = "false" + } + TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr) + // if enabled, + if TemplateEnable { + cookie_secret := os.Getenv("COOKIE_SECRET") + if cookie_secret == "" { + panic("cookie secret key is not set") + } + secretcookie, err = hex.DecodeString(cookie_secret) + if err != nil { + return user, err + } + } else { + panic("Templates are disabled") } gobEncodedValue, err := CookieReadEncrypted(r, "goffee", secretcookie) @@ -67,7 +83,26 @@ func SetCookie(w http.ResponseWriter, email string, token string) error { // cookie. var err error - secretcookie, err = hex.DecodeString("13d6b4dff8f84a10851021ec8608f814570d562c92fe6b5ec4c9f595bcb3234b") + // check if template engine is enable + TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE") + if TemplateEnableStr == "" { + TemplateEnableStr = "false" + } + TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr) + // if enabled, + if TemplateEnable { + cookie_secret := os.Getenv("COOKIE_SECRET") + if cookie_secret == "" { + panic("cookie secret key is not set") + } + secretcookie, err = hex.DecodeString(cookie_secret) + if err != nil { + return err + } + } else { + panic("Templates are disabled") + } + if err != nil { return err } @@ -101,8 +136,6 @@ func SetCookie(w http.ResponseWriter, email string, token string) error { return err } - fmt.Printf("Cookie set %v\n", email) - return nil } diff --git a/template/components/content_table.html b/template/components/content_table.html index 68e34f6..f7c2f89 100644 --- a/template/components/content_table.html +++ b/template/components/content_table.html @@ -6,7 +6,7 @@ -{{range .AllTD}}{{range .}}{{ .Value }}{{end}}{{end}} +{{- range .AllTD}}{{range .}}{{ .Value }}{{end}}{{- end}} {{end}} \ No newline at end of file diff --git a/template/components/form_textarea.go b/template/components/form_textarea.go index 8fbaf51..818ae02 100644 --- a/template/components/form_textarea.go +++ b/template/components/form_textarea.go @@ -1,7 +1,7 @@ package components type FormTextarea struct { - ID string - Label string - AllOptions []FormSelectOption + ID string + Label string + Value string } diff --git a/template/components/form_textarea.html b/template/components/form_textarea.html index 5372a42..23559ab 100644 --- a/template/components/form_textarea.html +++ b/template/components/form_textarea.html @@ -1,6 +1,6 @@ {{define "form_textarea"}}
- +
{{end}} \ No newline at end of file