go-gemini/examples/auth.go

137 lines
3.1 KiB
Go
Raw Normal View History

2020-10-12 20:34:52 +00:00
// +build ignore
2020-09-27 21:39:44 +00:00
package main
import (
"crypto/x509"
"fmt"
"log"
2020-09-28 22:19:59 +00:00
"git.sr.ht/~adnano/gmi"
2020-09-27 21:39:44 +00:00
)
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() {
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-10-12 03:48:18 +00:00
server := &gmi.Server{}
2020-10-12 04:13:24 +00:00
if err := server.CertificateStore.Load("/var/lib/gemini/certs"); err != nil {
log.Fatal(err)
}
2020-10-21 20:28:50 +00:00
server.Register("localhost", handler)
2020-09-27 21:39:44 +00:00
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-10-14 00:22:12 +00:00
func welcome(w *gmi.ResponseWriter, r *gmi.Request) {
w.Write([]byte("Welcome to this example.\n=> /login Login\n"))
2020-09-27 21:39:44 +00:00
}
2020-10-14 00:22:12 +00:00
func login(w *gmi.ResponseWriter, r *gmi.Request) {
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
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")
})
2020-09-27 21:39:44 +00:00
}
2020-10-14 00:22:12 +00:00
func loginPassword(w *gmi.ResponseWriter, r *gmi.Request) {
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
session, ok := getSession(cert)
2020-09-27 21:39:44 +00:00
if !ok {
2020-10-14 00:22:12 +00:00
gmi.CertificateNotAuthorized(w, r)
2020-09-27 21:39:44 +00:00
return
}
password, ok := gmi.SensitiveInput(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")
}
})
2020-09-27 21:39:44 +00:00
}
2020-10-14 00:22:12 +00:00
func logout(w *gmi.ResponseWriter, r *gmi.Request) {
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
fingerprint := gmi.Fingerprint(cert)
2020-09-27 21:39:44 +00:00
delete(sessions, fingerprint)
})
2020-10-14 00:22:12 +00:00
w.Write([]byte("Successfully logged out.\n"))
2020-09-27 21:39:44 +00:00
}
2020-10-14 00:22:12 +00:00
func profile(w *gmi.ResponseWriter, r *gmi.Request) {
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
session, ok := getSession(cert)
2020-09-27 21:39:44 +00:00
if !ok {
2020-10-14 00:22:12 +00:00
gmi.CertificateNotAuthorized(w, r)
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)
2020-10-14 00:22:12 +00:00
w.Write([]byte(profile))
})
2020-09-27 21:39:44 +00:00
}
2020-10-14 00:22:12 +00:00
func admin(w *gmi.ResponseWriter, r *gmi.Request) {
gmi.WithCertificate(w, r, func(cert *x509.Certificate) {
session, ok := getSession(cert)
2020-09-27 21:39:44 +00:00
if !ok {
2020-10-14 00:22:12 +00:00
gmi.CertificateNotAuthorized(w, r)
2020-09-27 21:39:44 +00:00
return
}
user := logins[session.username]
if !user.admin {
2020-10-14 00:22:12 +00:00
gmi.CertificateNotAuthorized(w, r)
2020-09-27 21:39:44 +00:00
return
}
2020-10-14 00:22:12 +00:00
w.Write([]byte("Welcome to the admin portal.\n"))
})
2020-09-27 21:39:44 +00:00
}