summaryrefslogtreecommitdiffstats
path: root/utils.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2017-06-03 18:00:51 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2017-06-03 18:00:51 -0400
commitf154ae1ec88146017abf3de9d14d119facb5fc4c (patch)
treecd857864dd52b088ccc8943b64fe9bbd59c04dc8 /utils.go
parent3f3cb7c7cede379914eed51c57e58f66ffdd1856 (diff)
downloadlastfm-api-f154ae1ec88146017abf3de9d14d119facb5fc4c.tar.gz
Basic web app
Diffstat (limited to 'utils.go')
-rw-r--r--utils.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/utils.go b/utils.go
index cb29662..49fdc5b 100644
--- a/utils.go
+++ b/utils.go
@@ -4,6 +4,7 @@ import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
+ "net/http"
)
func randomToken(length int) string {
@@ -12,7 +13,33 @@ func randomToken(length int) string {
return hex.EncodeToString(b)
}
+func genKey(length int) []byte {
+ b := make([]byte, length)
+ rand.Read(b)
+ return b
+}
+
func md5hex(s string) string {
hash := md5.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}
+
+func (app *App) GetCookie(r *http.Request, name string, dst interface{}) error {
+ cookie, err := r.Cookie(name)
+ if err != nil {
+ return err
+ }
+ return app.CookieHandler.Decode(name, cookie.Value, dst)
+}
+
+func (app *App) SetCookie(w http.ResponseWriter, name string, v interface{}, exp int) {
+ encoded, _ := app.CookieHandler.Encode(name, v)
+ cookie := &http.Cookie{
+ Name: name,
+ Value: encoded,
+ Path: "/",
+ HttpOnly: true,
+ MaxAge: exp,
+ }
+ http.SetCookie(w, cookie)
+}