github.com/kotovmak/go-admin@v1.1.1/plugins/admin/controller/auth.go (about) 1 package controller 2 3 import ( 4 "bytes" 5 template2 "html/template" 6 "net/http" 7 "net/url" 8 9 "github.com/kotovmak/go-admin/context" 10 "github.com/kotovmak/go-admin/modules/auth" 11 "github.com/kotovmak/go-admin/modules/config" 12 "github.com/kotovmak/go-admin/modules/db" 13 "github.com/kotovmak/go-admin/modules/logger" 14 "github.com/kotovmak/go-admin/modules/system" 15 "github.com/kotovmak/go-admin/plugins/admin/models" 16 "github.com/kotovmak/go-admin/plugins/admin/modules/captcha" 17 "github.com/kotovmak/go-admin/plugins/admin/modules/response" 18 "github.com/kotovmak/go-admin/template" 19 "github.com/kotovmak/go-admin/template/types" 20 ) 21 22 // Auth check the input password and username for authentication. 23 func (h *Handler) Auth(ctx *context.Context) { 24 25 var ( 26 user models.UserModel 27 ok bool 28 errMsg = "fail" 29 s, exist = h.services.GetOrNot(auth.ServiceKey) 30 ) 31 32 if capDriver, ok := h.captchaConfig["driver"]; ok { 33 capt, ok := captcha.Get(capDriver) 34 35 if ok { 36 if !capt.Validate(ctx.FormValue("token")) { 37 response.BadRequest(ctx, "wrong captcha") 38 return 39 } 40 } 41 } 42 43 if !exist { 44 password := ctx.FormValue("password") 45 username := ctx.FormValue("username") 46 47 if password == "" || username == "" { 48 response.BadRequest(ctx, "wrong password or username") 49 return 50 } 51 user, ok = auth.Check(password, username, h.conn) 52 } else { 53 user, ok, errMsg = auth.GetService(s).P(ctx) 54 } 55 56 if !ok { 57 response.BadRequest(ctx, errMsg) 58 return 59 } 60 61 err := auth.SetCookie(ctx, user, h.conn) 62 63 if err != nil { 64 response.Error(ctx, err.Error()) 65 return 66 } 67 68 if ref := ctx.Referer(); ref != "" { 69 if u, err := url.Parse(ref); err == nil { 70 v := u.Query() 71 if r := v.Get("ref"); r != "" { 72 rr, _ := url.QueryUnescape(r) 73 response.OkWithData(ctx, map[string]interface{}{ 74 "url": rr, 75 }) 76 return 77 } 78 } 79 } 80 81 response.OkWithData(ctx, map[string]interface{}{ 82 "url": h.config.GetIndexURL(), 83 }) 84 } 85 86 // Logout delete the cookie. 87 func (h *Handler) Logout(ctx *context.Context) { 88 err := auth.DelCookie(ctx, db.GetConnection(h.services)) 89 if err != nil { 90 logger.Error("logout error", err) 91 } 92 ctx.AddHeader("Location", config.GetLoginUrl()) 93 ctx.SetStatusCode(302) 94 } 95 96 // ShowLogin show the login page. 97 func (h *Handler) ShowLogin(ctx *context.Context) { 98 99 tmpl, name := template.GetComp("login").GetTemplate() 100 buf := new(bytes.Buffer) 101 if err := tmpl.ExecuteTemplate(buf, name, struct { 102 UrlPrefix string 103 Title string 104 Logo template2.HTML 105 CdnUrl string 106 System types.SystemInfo 107 }{ 108 UrlPrefix: h.config.AssertPrefix(), 109 Title: h.config.LoginTitle, 110 Logo: h.config.LoginLogo, 111 System: types.SystemInfo{ 112 Version: system.Version(), 113 }, 114 CdnUrl: h.config.AssetUrl, 115 }); err == nil { 116 ctx.HTML(http.StatusOK, buf.String()) 117 } else { 118 logger.Error(err) 119 ctx.HTML(http.StatusOK, "parse template error (;′⌒`)") 120 } 121 }