Refactor Input and SensitiveInput functions
This commit is contained in:
parent
9506f69f1a
commit
ef03df7664
@ -64,13 +64,15 @@ func welcome(w *gmi.ResponseWriter, r *gmi.Request) {
|
|||||||
|
|
||||||
func login(w *gmi.ResponseWriter, r *gmi.Request) {
|
func login(w *gmi.ResponseWriter, r *gmi.Request) {
|
||||||
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
|
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
|
||||||
gmi.WithInput(w, r, "Username", func(username string) {
|
username, ok := gmi.Input(w, r, "Username")
|
||||||
fingerprint := gmi.Fingerprint(cert)
|
if !ok {
|
||||||
sessions[fingerprint] = &session{
|
return
|
||||||
username: username,
|
}
|
||||||
}
|
fingerprint := gmi.Fingerprint(cert)
|
||||||
gmi.Redirect(w, r, "/login/password")
|
sessions[fingerprint] = &session{
|
||||||
})
|
username: username,
|
||||||
|
}
|
||||||
|
gmi.Redirect(w, r, "/login/password")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,15 +84,17 @@ func loginPassword(w *gmi.ResponseWriter, r *gmi.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gmi.WithSensitiveInput(w, r, "Password", func(password string) {
|
password, ok := gmi.Input(w, r, "Password")
|
||||||
expected := logins[session.username].password
|
if !ok {
|
||||||
if password == expected {
|
return
|
||||||
session.authorized = true
|
}
|
||||||
gmi.Redirect(w, r, "/profile")
|
expected := logins[session.username].password
|
||||||
} else {
|
if password == expected {
|
||||||
gmi.SensitiveInput(w, r, "Wrong password. Try again")
|
session.authorized = true
|
||||||
}
|
gmi.Redirect(w, r, "/profile")
|
||||||
})
|
} else {
|
||||||
|
gmi.SensitiveInput(w, r, "Wrong password. Try again")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
65
server.go
65
server.go
@ -184,7 +184,7 @@ func (s *Server) responder(r *Request) Responder {
|
|||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NotFoundResponder()
|
return ResponderFunc(NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
||||||
@ -259,53 +259,24 @@ type Responder interface {
|
|||||||
Respond(*ResponseWriter, *Request)
|
Respond(*ResponseWriter, *Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input responds to the request with a request for input using the given prompt.
|
// Input returns the request query.
|
||||||
func Input(w *ResponseWriter, r *Request, prompt string) {
|
// If no input is provided, it responds with StatusInput.
|
||||||
|
func Input(w *ResponseWriter, r *Request, prompt string) (string, bool) {
|
||||||
|
if r.URL.ForceQuery || r.URL.RawQuery != "" {
|
||||||
|
return r.URL.RawQuery, true
|
||||||
|
}
|
||||||
w.WriteHeader(StatusInput, prompt)
|
w.WriteHeader(StatusInput, prompt)
|
||||||
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
// InputHandler returns a simple handler that responds to each request with
|
// SensitiveInput returns the request query.
|
||||||
// a request for input.
|
// If no input is provided, it responds with StatusSensitiveInput.
|
||||||
func InputHandler(prompt string) Responder {
|
func SensitiveInput(w *ResponseWriter, r *Request, prompt string) (string, bool) {
|
||||||
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
if r.URL.ForceQuery || r.URL.RawQuery != "" {
|
||||||
Input(w, r, prompt)
|
return r.URL.RawQuery, true
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithInput either responds to the request with StatusInput if no input
|
|
||||||
// is provided, or calls f with the input when provided.
|
|
||||||
func WithInput(w *ResponseWriter, r *Request, prompt string, f func(string)) {
|
|
||||||
input := r.URL.RawQuery
|
|
||||||
if input == "" {
|
|
||||||
Input(w, r, prompt)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
f(input)
|
w.WriteHeader(StatusInput, prompt)
|
||||||
}
|
return "", false
|
||||||
|
|
||||||
// Sensitive responds to the request with a request for sensitive input
|
|
||||||
// using the given prompt.
|
|
||||||
func SensitiveInput(w *ResponseWriter, r *Request, prompt string) {
|
|
||||||
w.WriteHeader(StatusSensitiveInput, prompt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SensitiveInputHandler returns a simpler handler that responds to each request
|
|
||||||
// with a request for sensitive input.
|
|
||||||
func SensitiveInputHandler(prompt string) Responder {
|
|
||||||
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
|
||||||
SensitiveInput(w, r, prompt)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithSensitiveInput either responds to the request with StatusSensitiveInput
|
|
||||||
// if no input is provided, or calls f with the input when provided.
|
|
||||||
func WithSensitiveInput(w *ResponseWriter, r *Request, prompt string, f func(string)) {
|
|
||||||
input := r.URL.RawQuery
|
|
||||||
if input == "" {
|
|
||||||
SensitiveInput(w, r, prompt)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
f(input)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect replies to the request with a redirect to the given URL.
|
// Redirect replies to the request with a redirect to the given URL.
|
||||||
@ -323,12 +294,6 @@ func NotFound(w *ResponseWriter, r *Request) {
|
|||||||
w.WriteHeader(StatusNotFound, "Not found")
|
w.WriteHeader(StatusNotFound, "Not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFoundResponder returns a simple responder that responds to each request with
|
|
||||||
// the status code NotFound.
|
|
||||||
func NotFoundResponder() Responder {
|
|
||||||
return ResponderFunc(NotFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gone replies to the request with the Gone status code.
|
// Gone replies to the request with the Gone status code.
|
||||||
func Gone(w *ResponseWriter, r *Request) {
|
func Gone(w *ResponseWriter, r *Request) {
|
||||||
w.WriteHeader(StatusGone, "Gone")
|
w.WriteHeader(StatusGone, "Gone")
|
||||||
|
Loading…
Reference in New Issue
Block a user