Add WithInput helper functions
This commit is contained in:
parent
aec4a5d7c9
commit
70c5d8b9ce
|
@ -46,11 +46,11 @@ func main() {
|
||||||
|
|
||||||
handler := &gmi.ServeMux{}
|
handler := &gmi.ServeMux{}
|
||||||
handler.HandleFunc("/", welcome)
|
handler.HandleFunc("/", welcome)
|
||||||
handler.HandleFunc("/login", login)
|
handler.HandleFunc("/login/", login)
|
||||||
handler.HandleFunc("/login/password", loginPassword)
|
handler.HandleFunc("/login/password/", loginPassword)
|
||||||
handler.HandleFunc("/profile", profile)
|
handler.HandleFunc("/profile/", profile)
|
||||||
handler.HandleFunc("/admin", admin)
|
handler.HandleFunc("/admin/", admin)
|
||||||
handler.HandleFunc("/logout", logout)
|
handler.HandleFunc("/logout/", logout)
|
||||||
|
|
||||||
server := &gmi.Server{
|
server := &gmi.Server{
|
||||||
Certificate: cert,
|
Certificate: cert,
|
||||||
|
@ -74,15 +74,13 @@ func welcome(rw *gmi.ResponseWriter, req *gmi.Request) {
|
||||||
|
|
||||||
func login(rw *gmi.ResponseWriter, req *gmi.Request) {
|
func login(rw *gmi.ResponseWriter, req *gmi.Request) {
|
||||||
gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
|
gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
|
||||||
if username := req.URL.RawQuery; username == "" {
|
gmi.WithInput(rw, req, "Username", func(username string) {
|
||||||
gmi.Input(rw, req, "Username")
|
|
||||||
} else {
|
|
||||||
fingerprint := gmi.Fingerprint(cert)
|
fingerprint := gmi.Fingerprint(cert)
|
||||||
sessions[fingerprint] = &session{
|
sessions[fingerprint] = &session{
|
||||||
username: username,
|
username: username,
|
||||||
}
|
}
|
||||||
gmi.Redirect(rw, req, "/login/password")
|
gmi.Redirect(rw, req, "/login/password")
|
||||||
}
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,9 +92,7 @@ func loginPassword(rw *gmi.ResponseWriter, req *gmi.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if password := req.URL.RawQuery; password == "" {
|
gmi.WithSensitiveInput(rw, req, "Password", func(password string) {
|
||||||
gmi.SensitiveInput(rw, req, "Password")
|
|
||||||
} else {
|
|
||||||
expected := logins[session.username].password
|
expected := logins[session.username].password
|
||||||
if password == expected {
|
if password == expected {
|
||||||
session.authorized = true
|
session.authorized = true
|
||||||
|
@ -104,7 +100,7 @@ func loginPassword(rw *gmi.ResponseWriter, req *gmi.Request) {
|
||||||
} else {
|
} else {
|
||||||
gmi.SensitiveInput(rw, req, "Wrong password. Try again")
|
gmi.SensitiveInput(rw, req, "Wrong password. Try again")
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
74
server.go
74
server.go
|
@ -213,6 +213,42 @@ func InputHandler(prompt string) Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithInput either responds to the request with StatusInput if no input
|
||||||
|
// is provided, or calls f with the input when provided.
|
||||||
|
func WithInput(rw *ResponseWriter, req *Request, prompt string, f func(string)) {
|
||||||
|
input := req.URL.RawQuery
|
||||||
|
if input == "" {
|
||||||
|
Input(rw, req, prompt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sensitive responds to the request with a request for sensitive input
|
||||||
|
// using the given prompt.
|
||||||
|
func SensitiveInput(rw *ResponseWriter, req *Request, prompt string) {
|
||||||
|
rw.WriteHeader(StatusSensitiveInput, prompt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SensitiveInputHandler returns a simpler handler that responds to each request
|
||||||
|
// with a request for sensitive input.
|
||||||
|
func SensitiveInputHandler(prompt string) Handler {
|
||||||
|
return HandlerFunc(func(rw *ResponseWriter, req *Request) {
|
||||||
|
SensitiveInput(rw, req, prompt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithSensitiveInput either responds to the request with StatusSensitiveInput
|
||||||
|
// if no input is provided, or calls f with the input when provided.
|
||||||
|
func WithSensitiveInput(rw *ResponseWriter, req *Request, prompt string, f func(string)) {
|
||||||
|
input := req.URL.RawQuery
|
||||||
|
if input == "" {
|
||||||
|
SensitiveInput(rw, req, 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.
|
||||||
func Redirect(rw *ResponseWriter, req *Request, url string) {
|
func Redirect(rw *ResponseWriter, req *Request, url string) {
|
||||||
rw.WriteHeader(StatusRedirect, url)
|
rw.WriteHeader(StatusRedirect, url)
|
||||||
|
@ -241,32 +277,6 @@ func PermanentRedirectHandler(url string) Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sensitive responds to the request with a request for sensitive input
|
|
||||||
// using the given prompt.
|
|
||||||
func SensitiveInput(rw *ResponseWriter, req *Request, prompt string) {
|
|
||||||
rw.WriteHeader(StatusSensitiveInput, prompt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SensitiveInputHandler returns a simpler handler that responds to each request
|
|
||||||
// with a request for sensitive input.
|
|
||||||
func SensitiveInputHandler(prompt string) Handler {
|
|
||||||
return HandlerFunc(func(rw *ResponseWriter, req *Request) {
|
|
||||||
SensitiveInput(rw, req, prompt)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// CertificateRequired responds to the request with the CertificateRequired
|
|
||||||
// status code.
|
|
||||||
func CertificateRequired(rw *ResponseWriter, req *Request) {
|
|
||||||
rw.WriteHeader(StatusCertificateRequired, "Certificate required")
|
|
||||||
}
|
|
||||||
|
|
||||||
// CertificateNotAuthorized responds to the request with
|
|
||||||
// the CertificateNotAuthorized status code.
|
|
||||||
func CertificateNotAuthorized(rw *ResponseWriter, req *Request) {
|
|
||||||
rw.WriteHeader(StatusCertificateNotAuthorized, "Certificate not authorized")
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotFound replies to the request with the NotFound status code.
|
// NotFound replies to the request with the NotFound status code.
|
||||||
func NotFound(rw *ResponseWriter, req *Request) {
|
func NotFound(rw *ResponseWriter, req *Request) {
|
||||||
rw.WriteHeader(StatusNotFound, "Not found")
|
rw.WriteHeader(StatusNotFound, "Not found")
|
||||||
|
@ -289,6 +299,18 @@ func GoneHandler() Handler {
|
||||||
return HandlerFunc(Gone)
|
return HandlerFunc(Gone)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CertificateRequired responds to the request with the CertificateRequired
|
||||||
|
// status code.
|
||||||
|
func CertificateRequired(rw *ResponseWriter, req *Request) {
|
||||||
|
rw.WriteHeader(StatusCertificateRequired, "Certificate required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CertificateNotAuthorized responds to the request with
|
||||||
|
// the CertificateNotAuthorized status code.
|
||||||
|
func CertificateNotAuthorized(rw *ResponseWriter, req *Request) {
|
||||||
|
rw.WriteHeader(StatusCertificateNotAuthorized, "Certificate not authorized")
|
||||||
|
}
|
||||||
|
|
||||||
// WithCertificate responds with CertificateRequired if the client did not
|
// WithCertificate responds with CertificateRequired if the client did not
|
||||||
// provide a certificate, and calls f with the first ceritificate if they did.
|
// provide a certificate, and calls f with the first ceritificate if they did.
|
||||||
func WithCertificate(rw *ResponseWriter, req *Request, f func(*x509.Certificate)) {
|
func WithCertificate(rw *ResponseWriter, req *Request, f func(*x509.Certificate)) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user