diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-05 19:26:29 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-05 19:26:29 +0200 |
commit | 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc (patch) | |
tree | 5cd04e84f8aa427d274e16dd8cf507ff48468e07 /pkg/view/auth.go | |
parent | 24c121a9ef229870f54de487de01c15a15ebbff1 (diff) | |
download | lens-5168a9476f0e83264ecafc85bc9145e8bdcbb8dc.tar.gz lens-5168a9476f0e83264ecafc85bc9145e8bdcbb8dc.tar.bz2 lens-5168a9476f0e83264ecafc85bc9145e8bdcbb8dc.zip |
ref: Move net/http
I was young and naive, fasthttp does not fit my needs and make
development slower. I'll move to net/http since it has a wider support
and will spare some time on implementation detail things (like CSRF). It
will allow me to reduce a bit of the code since there may be lib for
handling cookie encryption and auth in general.
Diffstat (limited to 'pkg/view/auth.go')
-rw-r--r-- | pkg/view/auth.go | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/pkg/view/auth.go b/pkg/view/auth.go index 631cfb3..2a4b95e 100644 --- a/pkg/view/auth.go +++ b/pkg/view/auth.go @@ -2,8 +2,7 @@ package view import ( "encoding/base64" - - "github.com/valyala/fasthttp" + "net/http" "git.sr.ht/~gabrielgio/img/pkg/ext" "git.sr.ht/~gabrielgio/img/pkg/service" @@ -20,71 +19,77 @@ func NewAuthView(userController *service.AuthController) *AuthView { } } -func (v *AuthView) LoginView(ctx *fasthttp.RequestCtx) error { - templates.WritePageTemplate(ctx, &templates.LoginPage{}) +func (v *AuthView) LoginView(w http.ResponseWriter, r *http.Request) error { + templates.WritePageTemplate(w, &templates.LoginPage{}) return nil } -func (v *AuthView) Logout(ctx *fasthttp.RequestCtx) error { - cook := fasthttp.Cookie{} - cook.SetKey("auth") - cook.SetValue("") - cook.SetMaxAge(-1) - cook.SetHTTPOnly(true) - cook.SetSameSite(fasthttp.CookieSameSiteDefaultMode) - ctx.Response.Header.SetCookie(&cook) +func (v *AuthView) Logout(w http.ResponseWriter, r *http.Request) error { + cook := http.Cookie{ + Name: "auth", + Value: "", + MaxAge: -1, + HttpOnly: true, + SameSite: http.SameSiteDefaultMode, + } + http.SetCookie(w, &cook) - ctx.Redirect("/", 307) + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) return nil } -func (v *AuthView) Login(ctx *fasthttp.RequestCtx) error { - username := ctx.FormValue("username") - password := ctx.FormValue("password") +func (v *AuthView) Login(w http.ResponseWriter, r *http.Request) error { + var ( + username = []byte(r.FormValue("username")) + password = []byte(r.FormValue("password")) + ) - auth, err := v.userController.Login(ctx, username, password) + auth, err := v.userController.Login(r.Context(), username, password) if err != nil { return err } base64Auth := base64.StdEncoding.EncodeToString(auth) - cook := fasthttp.Cookie{} - cook.SetKey("auth") - cook.SetValue(base64Auth) - cook.SetHTTPOnly(true) - cook.SetSameSite(fasthttp.CookieSameSiteDefaultMode) - ctx.Response.Header.SetCookie(&cook) + cook := http.Cookie{ + Name: "auth", + Value: base64Auth, + HttpOnly: true, + SameSite: http.SameSiteDefaultMode, + } + http.SetCookie(w, &cook) - redirect := string(ctx.FormValue("redirect")) + redirect := r.FormValue("redirect") if redirect == "" { - ctx.Redirect("/", 307) + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) } else { - ctx.Redirect(redirect, 307) + http.Redirect(w, r, redirect, http.StatusTemporaryRedirect) } return nil } -func Index(ctx *fasthttp.RequestCtx) { - ctx.Redirect("/login", 307) +func Index(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) } -func (v *AuthView) InitialRegisterView(ctx *fasthttp.RequestCtx) error { - templates.WritePageTemplate(ctx, &templates.RegisterPage{}) +func (v *AuthView) InitialRegisterView(w http.ResponseWriter, r *http.Request) error { + templates.WritePageTemplate(w, &templates.RegisterPage{}) return nil } -func (v *AuthView) InitialRegister(ctx *fasthttp.RequestCtx) error { - username := ctx.FormValue("username") - password := ctx.FormValue("password") - path := ctx.FormValue("path") +func (v *AuthView) InitialRegister(w http.ResponseWriter, r *http.Request) error { + var ( + username = []byte(r.FormValue("username")) + password = []byte(r.FormValue("password")) + path = []byte(r.FormValue("path")) + ) - err := v.userController.InitialRegister(ctx, username, password, path) + err := v.userController.InitialRegister(r.Context(), username, password, path) if err != nil { return err } - ctx.Redirect("/login", 307) + http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) return nil } |