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) {
|
||||
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
|
||||
gmi.WithInput(w, r, "Username", func(username string) {
|
||||
fingerprint := gmi.Fingerprint(cert)
|
||||
sessions[fingerprint] = &session{
|
||||
username: username,
|
||||
}
|
||||
gmi.Redirect(w, r, "/login/password")
|
||||
})
|
||||
username, ok := gmi.Input(w, r, "Username")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
fingerprint := gmi.Fingerprint(cert)
|
||||
sessions[fingerprint] = &session{
|
||||
username: username,
|
||||
}
|
||||
gmi.Redirect(w, r, "/login/password")
|
||||
})
|
||||
}
|
||||
|
||||
@ -82,15 +84,17 @@ func loginPassword(w *gmi.ResponseWriter, r *gmi.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
gmi.WithSensitiveInput(w, r, "Password", func(password string) {
|
||||
expected := logins[session.username].password
|
||||
if password == expected {
|
||||
session.authorized = true
|
||||
gmi.Redirect(w, r, "/profile")
|
||||
} else {
|
||||
gmi.SensitiveInput(w, r, "Wrong password. Try again")
|
||||
}
|
||||
})
|
||||
password, ok := gmi.Input(w, r, "Password")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
expected := logins[session.username].password
|
||||
if password == expected {
|
||||
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 NotFoundResponder()
|
||||
return ResponderFunc(NotFound)
|
||||
}
|
||||
|
||||
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
||||
@ -259,53 +259,24 @@ type Responder interface {
|
||||
Respond(*ResponseWriter, *Request)
|
||||
}
|
||||
|
||||
// Input responds to the request with a request for input using the given prompt.
|
||||
func Input(w *ResponseWriter, r *Request, prompt string) {
|
||||
// Input returns the request query.
|
||||
// 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)
|
||||
return "", false
|
||||
}
|
||||
|
||||
// InputHandler returns a simple handler that responds to each request with
|
||||
// a request for input.
|
||||
func InputHandler(prompt string) Responder {
|
||||
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
||||
Input(w, r, prompt)
|
||||
})
|
||||
}
|
||||
|
||||
// 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
|
||||
// SensitiveInput returns the request query.
|
||||
// If no input is provided, it responds with StatusSensitiveInput.
|
||||
func SensitiveInput(w *ResponseWriter, r *Request, prompt string) (string, bool) {
|
||||
if r.URL.ForceQuery || r.URL.RawQuery != "" {
|
||||
return r.URL.RawQuery, true
|
||||
}
|
||||
f(input)
|
||||
}
|
||||
|
||||
// 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)
|
||||
w.WriteHeader(StatusInput, prompt)
|
||||
return "", false
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// 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.
|
||||
func Gone(w *ResponseWriter, r *Request) {
|
||||
w.WriteHeader(StatusGone, "Gone")
|
||||
|
Loading…
Reference in New Issue
Block a user