Refactor Input and SensitiveInput functions

This commit is contained in:
Adnan Maolood 2020-10-21 17:34:07 -04:00
parent 9506f69f1a
commit ef03df7664
2 changed files with 35 additions and 66 deletions

View File

@ -64,14 +64,16 @@ 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")
if !ok {
return
}
fingerprint := gmi.Fingerprint(cert) fingerprint := gmi.Fingerprint(cert)
sessions[fingerprint] = &session{ sessions[fingerprint] = &session{
username: username, username: username,
} }
gmi.Redirect(w, r, "/login/password") gmi.Redirect(w, r, "/login/password")
}) })
})
} }
func loginPassword(w *gmi.ResponseWriter, r *gmi.Request) { func loginPassword(w *gmi.ResponseWriter, r *gmi.Request) {
@ -82,7 +84,10 @@ 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")
if !ok {
return
}
expected := logins[session.username].password expected := logins[session.username].password
if password == expected { if password == expected {
session.authorized = true session.authorized = true
@ -91,7 +96,6 @@ func loginPassword(w *gmi.ResponseWriter, r *gmi.Request) {
gmi.SensitiveInput(w, r, "Wrong password. Try again") gmi.SensitiveInput(w, r, "Wrong password. Try again")
} }
}) })
})
} }
func logout(w *gmi.ResponseWriter, r *gmi.Request) { func logout(w *gmi.ResponseWriter, r *gmi.Request) {

View File

@ -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
})
} }
w.WriteHeader(StatusInput, prompt)
// WithInput either responds to the request with StatusInput if no input return "", false
// 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)
}
// 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")