Basic auth middleware
This commit is contained in:
parent
297ec2780b
commit
84fb52ac21
@ -6,6 +6,7 @@ import (
|
||||
"github.com/Teajey/rsvp"
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/routing"
|
||||
"lishwist/http/session"
|
||||
"lishwist/http/templates"
|
||||
"lishwist/http/templates/text"
|
||||
@ -33,6 +34,15 @@ func (m *Serve) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
username, password, ok := r.BasicAuth()
|
||||
if ok {
|
||||
loggedIn := nil == routing.LoginMiddle(username, password, s, r)
|
||||
if loggedIn {
|
||||
m.Private.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
m.Public.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,6 @@ import (
|
||||
|
||||
lishwist "lishwist/core"
|
||||
"lishwist/http/props"
|
||||
"lishwist/http/response"
|
||||
"lishwist/http/session"
|
||||
|
||||
"github.com/Teajey/hmc"
|
||||
@ -43,30 +42,15 @@ func Login(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body
|
||||
return rsvp.Body{TemplateName: "login.gotmpl", Data: body}
|
||||
}
|
||||
|
||||
func LoginPost(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return response.Error("Failed to parse form").StatusBadRequest()
|
||||
}
|
||||
|
||||
username := r.Form.Get("username")
|
||||
password := r.Form.Get("password")
|
||||
referrer := r.URL.Query().Get("referrer")
|
||||
|
||||
func LoginMiddle(username, password string, session *session.Session, r *http.Request) *props.Login {
|
||||
login := props.NewLogin(username, password)
|
||||
|
||||
resp := rsvp.Data(login).StatusSeeOther(referrer)
|
||||
|
||||
valid := login.Validate()
|
||||
login.Password.Value = ""
|
||||
if !valid {
|
||||
session.FlashSet(&login)
|
||||
log.Printf("Invalid login submission: %#v\n", login)
|
||||
resp.Data = LoginPage{
|
||||
Namespace: hmc.NS(),
|
||||
Form: hmc.Form[props.Login]{Method: "POST", Elements: login},
|
||||
}
|
||||
return resp
|
||||
return &login
|
||||
}
|
||||
|
||||
appSession, err := lishwist.Login(username, password, time.Hour*24)
|
||||
@ -83,15 +67,37 @@ func LoginPost(session *session.Session, w rsvp.ResponseWriter, r *http.Request)
|
||||
log.Printf("Login error: %s\n", err)
|
||||
}
|
||||
|
||||
resp.Data = LoginPage{
|
||||
Namespace: hmc.NS(),
|
||||
Form: hmc.Form[props.Login]{Method: "POST", Elements: login},
|
||||
}
|
||||
return resp
|
||||
return &login
|
||||
}
|
||||
|
||||
session.SetID("")
|
||||
session.SetValue("sessionKey", appSession.Key)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoginPost(session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
||||
page := LoginPage{
|
||||
Namespace: hmc.NS(),
|
||||
Form: hmc.Form[props.Login]{Method: "POST"},
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
page.Form.Elements.GeneralError = "Failed to parse form"
|
||||
return rsvp.Data(page).StatusUnprocessableEntity()
|
||||
}
|
||||
|
||||
username := r.Form.Get("username")
|
||||
password := r.Form.Get("password")
|
||||
|
||||
login := LoginMiddle(username, password, session, r)
|
||||
if login != nil {
|
||||
page.Form.Elements = *login
|
||||
return rsvp.Data(page).StatusSeeOther("")
|
||||
}
|
||||
|
||||
referrer := r.URL.Query().Get("referrer")
|
||||
|
||||
return rsvp.Data("Login successful!").StatusSeeOther(referrer)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user