aboutsummaryrefslogtreecommitdiff
path: root/examples/auth.go
blob: 9f3045f3d8ae5fd055a598e28a0add66aadef34f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// +build ignore

package main

import (
	"crypto/x509"
	"fmt"
	"log"

	"git.sr.ht/~adnano/gmi"
)

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() {
	handler := &gmi.ServeMux{}
	handler.HandleFunc("/", welcome)
	handler.HandleFunc("/login", login)
	handler.HandleFunc("/login/password", loginPassword)
	handler.HandleFunc("/profile", profile)
	handler.HandleFunc("/admin", admin)
	handler.HandleFunc("/logout", logout)

	server := &gmi.Server{}
	if err := server.CertificateStore.Load("/var/lib/gemini/certs"); err != nil {
		log.Fatal(err)
	}
	server.Handle("localhost", handler)

	if err := server.ListenAndServe(); err != nil {
		log.Fatal(err)
	}
}

func getSession(crt *x509.Certificate) (*session, bool) {
	fingerprint := gmi.Fingerprint(crt)
	session, ok := sessions[fingerprint]
	return session, ok
}

func welcome(rw *gmi.ResponseWriter, req *gmi.Request) {
	rw.Write([]byte("Welcome to this example.\n=> /login Login\n"))
}

func login(rw *gmi.ResponseWriter, req *gmi.Request) {
	gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
		gmi.WithInput(rw, req, "Username", func(username string) {
			fingerprint := gmi.Fingerprint(cert)
			sessions[fingerprint] = &session{
				username: username,
			}
			gmi.Redirect(rw, req, "/login/password")
		})
	})
}

func loginPassword(rw *gmi.ResponseWriter, req *gmi.Request) {
	gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
		session, ok := getSession(cert)
		if !ok {
			gmi.CertificateNotAuthorized(rw, req)
			return
		}

		gmi.WithSensitiveInput(rw, req, "Password", func(password string) {
			expected := logins[session.username].password
			if password == expected {
				session.authorized = true
				gmi.Redirect(rw, req, "/profile")
			} else {
				gmi.SensitiveInput(rw, req, "Wrong password. Try again")
			}
		})
	})
}

func logout(rw *gmi.ResponseWriter, req *gmi.Request) {
	gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
		fingerprint := gmi.Fingerprint(cert)
		delete(sessions, fingerprint)
	})
	rw.Write([]byte("Successfully logged out.\n"))
}

func profile(rw *gmi.ResponseWriter, req *gmi.Request) {
	gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
		session, ok := getSession(cert)
		if !ok {
			gmi.CertificateNotAuthorized(rw, req)
			return
		}
		user := logins[session.username]
		profile := fmt.Sprintf("Username: %s\nAdmin: %t\n=> /logout Logout", session.username, user.admin)
		rw.Write([]byte(profile))
	})
}

func admin(rw *gmi.ResponseWriter, req *gmi.Request) {
	gmi.WithCertificate(rw, req, func(cert *x509.Certificate) {
		session, ok := getSession(cert)
		if !ok {
			gmi.CertificateNotAuthorized(rw, req)
			return
		}
		user := logins[session.username]
		if !user.admin {
			gmi.CertificateNotAuthorized(rw, req)
			return
		}
		rw.Write([]byte("Welcome to the admin portal.\n"))
	})
}