From 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 5 Aug 2023 19:26:29 +0200 Subject: 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. --- pkg/view/auth.go | 77 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) (limited to 'pkg/view/auth.go') 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 } -- cgit v1.2.3