2020-09-27 21:39:44 +00:00
|
|
|
// +build example
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.sr.ht/~adnano/go-gemini"
|
|
|
|
)
|
|
|
|
|
|
|
|
type user struct {
|
|
|
|
password string // TODO: use hashes
|
|
|
|
admin bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type session struct {
|
|
|
|
username string
|
|
|
|
authorized bool // whether or not the password was supplied
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Map of usernames to user data
|
|
|
|
logins = map[string]user{
|
|
|
|
"admin": {"p@ssw0rd", true}, // NOTE: These are bad passwords!
|
|
|
|
"user1": {"password1", false},
|
|
|
|
"user2": {"password2", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map of certificate fingerprints to sessions
|
|
|
|
sessions = map[string]*session{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Configure a certificate.
|
|
|
|
// To generate a TLS key pair, run:
|
|
|
|
//
|
|
|
|
// go run -tags=example ../cert
|
2020-09-27 23:56:33 +00:00
|
|
|
//
|
2020-09-28 00:20:59 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair("examples/server/localhost.crt", "examples/server/localhost.key")
|
2020-09-27 21:39:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
handler := &gmi.ServeMux{}
|
2020-09-28 02:03:09 +00:00
|
|
|
handler.HandleFunc("/", welcome)
|
2020-09-28 20:02:32 +00:00
|
|
|
handler.HandleFunc("/login", login)
|
|
|
|
handler.HandleFunc("/login/password", loginPassword)
|
|
|
|
handler.HandleFunc("/profile", profile)
|
|
|
|
handler.HandleFunc("/admin", admin)
|
|
|
|
handler.HandleFunc("/logout", logout)
|
2020-09-27 21:39:44 +00:00
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
server := &gmi.Server{
|
2020-09-27 21:39:44 +00:00
|
|
|
Certificate: cert,
|
|
|
|
Handler: handler,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.ListenAndServe(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSession(crt *x509.Certificate) (*session, bool) {
|
2020-09-28 00:20:59 +00:00
|
|
|
fingerprint := gmi.Fingerprint(crt)
|
2020-09-27 21:39:44 +00:00
|
|
|
session, ok := sessions[fingerprint]
|
|
|
|
return session, ok
|
|
|
|
}
|
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
func welcome(rw *gmi.ResponseWriter, req *gmi.Request) {
|
2020-09-27 21:39:44 +00:00
|
|
|
rw.Write([]byte("Welcome to this example.\n=> /login Login\n"))
|
|
|
|
}
|
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
func login(rw *gmi.ResponseWriter, req *gmi.Request) {
|
2020-09-28 05:14:29 +00:00
|
|
|
gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
|
2020-09-28 06:05:37 +00:00
|
|
|
gmi.WithInput(rw, req, "Username", func(username string) {
|
2020-09-28 05:14:29 +00:00
|
|
|
fingerprint := gmi.Fingerprint(cert)
|
2020-09-27 21:39:44 +00:00
|
|
|
sessions[fingerprint] = &session{
|
|
|
|
username: username,
|
|
|
|
}
|
2020-09-28 20:07:51 +00:00
|
|
|
gmi.Redirect(rw, req, "/login#password")
|
2020-09-28 06:05:37 +00:00
|
|
|
})
|
2020-09-28 05:14:29 +00:00
|
|
|
})
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
func loginPassword(rw *gmi.ResponseWriter, req *gmi.Request) {
|
2020-09-28 05:14:29 +00:00
|
|
|
gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
|
|
|
|
session, ok := getSession(cert)
|
2020-09-27 21:39:44 +00:00
|
|
|
if !ok {
|
2020-09-28 02:03:09 +00:00
|
|
|
gmi.CertificateNotAuthorized(rw, req)
|
2020-09-27 21:39:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:05:37 +00:00
|
|
|
gmi.WithSensitiveInput(rw, req, "Password", func(password string) {
|
2020-09-27 21:39:44 +00:00
|
|
|
expected := logins[session.username].password
|
|
|
|
if password == expected {
|
|
|
|
session.authorized = true
|
2020-09-28 02:06:08 +00:00
|
|
|
gmi.Redirect(rw, req, "/profile")
|
2020-09-27 21:39:44 +00:00
|
|
|
} else {
|
2020-09-28 02:03:09 +00:00
|
|
|
gmi.SensitiveInput(rw, req, "Wrong password. Try again")
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|
2020-09-28 06:05:37 +00:00
|
|
|
})
|
2020-09-28 05:14:29 +00:00
|
|
|
})
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
func logout(rw *gmi.ResponseWriter, req *gmi.Request) {
|
2020-09-28 05:14:29 +00:00
|
|
|
gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
|
|
|
|
fingerprint := gmi.Fingerprint(cert)
|
2020-09-27 21:39:44 +00:00
|
|
|
delete(sessions, fingerprint)
|
2020-09-28 05:14:29 +00:00
|
|
|
})
|
2020-09-27 21:39:44 +00:00
|
|
|
rw.Write([]byte("Successfully logged out.\n"))
|
|
|
|
}
|
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
func profile(rw *gmi.ResponseWriter, req *gmi.Request) {
|
2020-09-28 05:14:29 +00:00
|
|
|
gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
|
|
|
|
session, ok := getSession(cert)
|
2020-09-27 21:39:44 +00:00
|
|
|
if !ok {
|
2020-09-28 02:03:09 +00:00
|
|
|
gmi.CertificateNotAuthorized(rw, req)
|
2020-09-27 21:39:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user := logins[session.username]
|
|
|
|
profile := fmt.Sprintf("Username: %s\nAdmin: %t\n=> /logout Logout", session.username, user.admin)
|
|
|
|
rw.Write([]byte(profile))
|
2020-09-28 05:14:29 +00:00
|
|
|
})
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 00:20:59 +00:00
|
|
|
func admin(rw *gmi.ResponseWriter, req *gmi.Request) {
|
2020-09-28 05:14:29 +00:00
|
|
|
gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
|
|
|
|
session, ok := getSession(cert)
|
2020-09-27 21:39:44 +00:00
|
|
|
if !ok {
|
2020-09-28 02:03:09 +00:00
|
|
|
gmi.CertificateNotAuthorized(rw, req)
|
2020-09-27 21:39:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user := logins[session.username]
|
|
|
|
if !user.admin {
|
2020-09-28 02:03:09 +00:00
|
|
|
gmi.CertificateNotAuthorized(rw, req)
|
2020-09-27 21:39:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rw.Write([]byte("Welcome to the admin portal.\n"))
|
2020-09-28 05:14:29 +00:00
|
|
|
})
|
2020-09-27 21:39:44 +00:00
|
|
|
}
|