go-gemini/examples/auth.go

92 lines
1.8 KiB
Go
Raw Normal View History

2020-10-12 14:34:52 -06:00
// +build ignore
2020-09-27 15:39:44 -06:00
package main
import (
2020-11-25 12:24:49 -07:00
"crypto/sha512"
"crypto/tls"
2020-09-27 15:39:44 -06:00
"crypto/x509"
"crypto/x509/pkix"
2020-09-27 15:39:44 -06:00
"fmt"
"log"
"time"
2020-09-27 15:39:44 -06:00
2020-10-27 21:35:22 -06:00
"git.sr.ht/~adnano/go-gemini"
2021-01-14 19:23:13 -07:00
"git.sr.ht/~adnano/go-gemini/certificate"
2020-09-27 15:39:44 -06:00
)
2020-11-25 12:24:49 -07:00
type User struct {
Name string
2020-09-27 15:39:44 -06:00
}
var (
2020-11-25 12:24:49 -07:00
// Map of certificate hashes to users
users = map[string]*User{}
2020-09-27 15:39:44 -06:00
)
func main() {
2020-10-27 21:35:22 -06:00
var mux gemini.ServeMux
2020-11-25 12:24:49 -07:00
mux.HandleFunc("/", profile)
mux.HandleFunc("/username", changeUsername)
2020-10-21 15:47:34 -06:00
2020-10-27 21:35:22 -06:00
var server gemini.Server
2020-10-28 12:59:45 -06:00
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
2020-10-11 22:13:24 -06:00
log.Fatal(err)
}
2021-01-25 08:59:50 -07:00
server.GetCertificate = func(hostname string) (tls.Certificate, error) {
2021-01-14 19:23:13 -07:00
return certificate.Create(certificate.CreateOptions{
Subject: pkix.Name{
CommonName: hostname,
},
DNSNames: []string{hostname},
Duration: time.Hour,
})
}
2021-01-14 20:12:07 -07:00
server.Handle("localhost", &mux)
2020-09-27 15:39:44 -06:00
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
2020-11-25 12:24:49 -07:00
func fingerprint(cert *x509.Certificate) string {
b := sha512.Sum512(cert.Raw)
return string(b[:])
2020-09-27 15:39:44 -06:00
}
2020-11-25 12:24:49 -07:00
func profile(w *gemini.ResponseWriter, r *gemini.Request) {
if r.Certificate == nil {
2021-01-14 17:04:11 -07:00
w.Status(gemini.StatusCertificateRequired)
2020-10-21 15:47:34 -06:00
return
}
2020-11-25 12:24:49 -07:00
fingerprint := fingerprint(r.Certificate.Leaf)
user, ok := users[fingerprint]
2020-10-21 15:47:34 -06:00
if !ok {
2020-11-25 12:24:49 -07:00
user = &User{}
users[fingerprint] = user
2020-10-21 15:47:34 -06:00
}
2020-11-25 12:24:49 -07:00
fmt.Fprintln(w, "Username:", user.Name)
fmt.Fprintln(w, "=> /username Change username")
2020-09-27 15:39:44 -06:00
}
2020-11-25 12:24:49 -07:00
func changeUsername(w *gemini.ResponseWriter, r *gemini.Request) {
if r.Certificate == nil {
2021-01-14 17:04:11 -07:00
w.Status(gemini.StatusCertificateRequired)
2020-10-21 15:47:34 -06:00
return
}
2020-09-27 15:39:44 -06:00
2020-12-17 22:25:06 -07:00
username, err := gemini.QueryUnescape(r.URL.RawQuery)
if err != nil || username == "" {
2021-01-14 17:04:11 -07:00
w.Header(gemini.StatusInput, "Username")
2020-10-21 15:47:34 -06:00
return
}
fingerprint := fingerprint(r.Certificate.Leaf)
user, ok := users[fingerprint]
if !ok {
user = &User{}
users[fingerprint] = user
}
user.Name = username
2021-01-14 17:04:11 -07:00
w.Header(gemini.StatusRedirect, "/")
2020-09-27 15:39:44 -06:00
}