finish user registration / login

This commit is contained in:
2018-06-23 21:17:53 +02:00
parent 39404caa6e
commit 24b74a1b60
15 changed files with 316 additions and 72 deletions

View File

@@ -3,6 +3,10 @@ package webcontroller
import (
"html/template"
"net/http"
"time"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
)
// TemplateData is a struct that every template expects when being rendered. In
@@ -20,12 +24,32 @@ type TemplateData struct {
Other interface{}
}
func (wc *WebController) newTemplateData(r *http.Request) *TemplateData {
func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) *TemplateData {
var t = &TemplateData{
Authenticated: false,
Username: "Fornax",
APIEndpoint: template.URL(wc.conf.APIURLExternal),
}
if key, err := wc.getAPIKey(r); err == nil {
var api = pixelapi.New(wc.conf.APIURLInternal, key)
uinf, err := api.UserInfo()
if err != nil {
// This session key doesn't work, delete it
log.Debug("Invalid API key '%s' passed", key)
http.SetCookie(w, &http.Cookie{
Name: "pd_auth_key",
Value: "",
Path: "/",
Expires: time.Unix(0, 0),
})
return t
}
// Authentication succeeded
t.Authenticated = true
t.Username = uinf.Username
}
return t
}

View File

@@ -1 +1,25 @@
package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
func (wc *WebController) serveLogout(
w http.ResponseWriter,
r *http.Request,
p httprouter.Params,
) {
if key, err := wc.getAPIKey(r); err == nil {
var api = pixelapi.New(wc.conf.APIURLInternal, key)
_, err1 := api.UserSessionDestroy(key)
if err1 != nil {
log.Warn("logout failed for session '%s': %s", key, err1)
}
}
http.Redirect(w, r, "/", 302)
}

View File

@@ -1,8 +1,11 @@
package webcontroller
import (
"errors"
"net/http"
"github.com/google/uuid"
"fornaxian.com/pixeldrain-web/init/conf"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
@@ -12,7 +15,7 @@ import (
type WebController struct {
conf *conf.PixelWebConfig
api *pixelapi.PixelAPI
api *pixelapi.PixelAPI // Shared instance, only used for unauthenticated requests
templates *templates.TemplateManager
staticResourceDir string
}
@@ -22,7 +25,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
conf: conf,
staticResourceDir: conf.StaticResourceDir,
}
wc.api = pixelapi.New(conf.APIURLInternal)
wc.api = pixelapi.New(conf.APIURLInternal, "")
wc.templates = templates.New(
conf.TemplateDir,
conf.APIURLExternal,
@@ -45,6 +48,8 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
r.GET(prefix+"/register" /* */, wc.serveTemplate("register"))
r.GET(prefix+"/login" /* */, wc.serveTemplate("login"))
r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout"))
r.POST(prefix+"/logout" /* */, wc.serveLogout)
r.NotFound = http.HandlerFunc(wc.serveNotFound)
@@ -61,7 +66,7 @@ func (wc *WebController) serveTemplate(tpl string) httprouter.Handle {
r *http.Request,
p httprouter.Params,
) {
err := wc.templates.Get().ExecuteTemplate(w, tpl, wc.newTemplateData(r))
err := wc.templates.Get().ExecuteTemplate(w, tpl, wc.newTemplateData(w, r))
if err != nil {
log.Error("Error executing template '%s': %s", tpl, err)
}
@@ -80,5 +85,14 @@ func (wc *WebController) serveFile(path string) httprouter.Handle {
func (wc *WebController) serveNotFound(w http.ResponseWriter, r *http.Request) {
log.Debug("Not Found: %s", r.URL)
wc.templates.Get().ExecuteTemplate(w, "error", wc.newTemplateData(r))
wc.templates.Get().ExecuteTemplate(w, "error", wc.newTemplateData(w, r))
}
func (wc *WebController) getAPIKey(r *http.Request) (key string, err error) {
if cookie, err := r.Cookie("pd_auth_key"); err == nil {
if _, err := uuid.Parse(cookie.Value); err == nil {
return cookie.Value, nil
}
}
return "", errors.New("not a valid pixeldrain authentication cookie")
}