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